phpescpos

PHP EscPOS - save raw data & execute later


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:

enter image description here

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:

  1. Can I save the raw data in a txt file?
  2. 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();

Solution

  • 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:

    copy($file, "//shared-pc/shared-printer");
    

    Also, instead of using the DummyPrintConnector class we can use the FilePrintConnector which is a better approach for those, like me, who need to save the file.

    Solution 1)

    $filename = 'example.txt'; // You can save as 'example.bin' if you wish
    $connector = new FilePrintConnector($filename);
    $printer = new Printer($connector);
    $printer->text("Hello world!\n");
    $printer->cut();
    $printer->close();
    
    // Send bin data directly to the printer
    copy($filename, "//your-pc//your-shared-printer");
    

    Solution 2)

    $filename = 'example.txt';
    $connector = new DummyPrintConnector();
    $printer = new Printer($connector);
    $printer->text("Hello world!\n");
    $printer->cut();
    
    file_put_contents($filename, $connector->getData());
    
    $printer->close();
    
    // Send bin data directly to the printer
    copy($filename, "//your-pc//your-shared-printer");