I'm able to execute the commands directly to the printer using the WindowsPrintConnector but I need to save the raw data of EscPOS so I can use / execute it later.
This works great:
$connector = new WindowsPrintConnector('Generic');
$printer = new Printer($connector);
$printer->text("Hello world\n");
$printer->close();
But I want to save the raw data and execute it later.
I started by using the DummyPrintConnector class and save the raw data into a txt file.
$connector = new DummyPrintConnector();
$printer = new Printer($connector);
$printer->text("Hello world!\n");
$printer->cut();
$data = $connector->getData();
file_put_contents('example.txt', $data);
$printer->close();
This saves in the txt file the following data:
Then I tried to read the txt file and execute it using the WindowsPrintConnector:
$data = file_get_contents('example.txt');
$connector = new WindowsPrintConnector('Generic');
$printer = new Printer($connector);
$printer->text($data);
$printer->close();
But the paper output is ?VA??@Hello world! instead of Hello world!.
Questions:
- Can I save the raw data in a txt file?
- How do I make the printer to execute the commands properly?
Edit 1)
Trying to print directly the data obtained also doesn't work. For example:
$connector = new DummyPrintConnector();
$printer = new Printer($connector);
$printer->text("Hello world!\n");
$data = $connector->getData();
$printer->close();
$connector = new WindowsPrintConnector('Generic');
$printer = new Printer($connector);
$printer->text($data); // Wrong chars
$printer->textRaw($data); // Wrong chars
$printer->close();

Solved.
The data returned from
->getData()is OK, but we cannot use->text()nor->textRaw()functions to print the contents of saved data.Instead we should copy the contents directly to the printer, like such:
Also, instead of using the
DummyPrintConnectorclass we can use theFilePrintConnectorwhich is a better approach for those, like me, who need to save the file.Solution 1)
Solution 2)