receipt-print-hq / escpos-tools

Utilities to read ESC/POS print data
MIT License
199 stars 72 forks source link

Error with some png image #49

Closed mikysetiawan closed 5 years ago

mikysetiawan commented 6 years ago

Hi I got error when I using some .png image when I want to convert to html (not all png image, but I don't know what different between the image that error and the image that not) . This the errors look like

php esc2html.php temp_print.bin > receipt-with-logo.html PHP Fatal error: Uncaught ImagickException: UnableToReadImageData `' @ error/pnm.c/ReadPNMImage/1319 in C:\xampp\htdocs\MyProject\public\escpos-tools-master\src\Parser\Command\StoreRasterFmtDataToPrintBufferGraphicsSubCmd.php:81 Stack trace:

0 C:\xampp\htdocs\MyProject\public\escpos-tools-master\src\Parser\Command\StoreRasterFmtDataToPrintBufferGraphicsSubCmd.php(81): Imagick->readimageblob('P4\n764 830\n\x00\x00\x00\x00...', 'pbm')

1 C:\xampp\htdocs\MyProject\public\escpos-tools-master\esc2html.php(59): ReceiptPrintHq\EscposTools\Parser\Command\StoreRasterFmtDataToPrintBufferGraphicsSubCmd->asPng()

2 {main}

thrown in C:\xampp\htdocs\MyProject\public\escpos-tools-master\src\Parser\Command\StoreRasterFmtDataToPrintBufferGraphicsSubCmd.php on line 81

Thank you!

mike42 commented 6 years ago

Are you able to share the contents of temp_print.bin either on this issue tracker or via the email address on my profile?

I can't see a way to debug this without a sample file which shows the issue.

mikysetiawan commented 6 years ago

Hi Mike, I already send to your email

mike42 commented 6 years ago

Received.

Your file is malformed: You are using the GS ( L command, which only allows you to specify a data length up to 65kb. Your image is around 80kb in the ESC/POS 1-bit raster format, so you should be using GS 8 L, which allows for larger data sizes

The file contains 1d 28 4c 4a 37 (GS ( L data header for 14154 bytes of data), while the actual image command contains 764x830 dimensions. If necessary, we can detect the number of bytes from the dimensions, and ignore the data header entirely.

So with this small patch, your file will process:

diff --git a/src/Parser/Command/StoreRasterFmtDataToPrintBufferGraphicsSubCmd.php b/src/Parser/Command/StoreRasterFmtDataToPrintBufferGraphicsSubCmd.php
index dcf9e47..41d4ced 100644
--- a/src/Parser/Command/StoreRasterFmtDataToPrintBufferGraphicsSubCmd.php
+++ b/src/Parser/Command/StoreRasterFmtDataToPrintBufferGraphicsSubCmd.php
@@ -51,6 +51,7 @@ class StoreRasterFmtDataToPrintBufferGraphicsSubCmd extends DataSubCmd implement
             return true;
         } else if ($this -> y2 === null) {
             $this -> y2 = ord($char);
+            $this -> dataSize = intdiv($this -> getWidth() + 7, 8) * $this -> getHeight();
             return true;
         } else if (strlen($this -> data) < $this -> dataSize) {
             $this -> data .= $char;

I have not tried to print your file, but if it works on an Epson printer, I would consider applying this patch in some form, otherwise we should return only part of the image.

In either case, we can detect the problem and log a warning when this situation arises.

mikysetiawan commented 6 years ago

Thanks! its working to process to html, I don't have Epson printer, but I think my client using an Epson printer, so if it's working I will tell you.

Thank you