Skip to content

Conversation

@BimBoss
Copy link

@BimBoss BimBoss commented Feb 11, 2018

Problem:

Many libraries (for example bwip-js) encoding their images with decimal numbers and returning it to the buffer. So first byte of generated image looks like a decimal number, for example data[0] for PNG image generated by bwip-js is 137 (0x89 in hex) and for JPEG - data[0] is 255 (0xff in hex). Your code verificate image only if first byte is hexadecimal number, but it can be decimal too. Many libraries stores decimal encoded images in buffers, and encodes it to hexadecimal only when writing to file system.
Because of that problem, buffers with valid decimal encoded images can't pass verification in the if...else statement and code throws an error 'Unknown image format.', so you can't paste your generated image to pdf file directly from buffer, you should save it in file system and only then you will be able to paste it to pdf, that is inconvenient and very bad for performance.

Solution:

So my changes add the opportunity to check if buffer is valid JPEG or PNG decimal encoded image. I have tested my changes in real projects, and your library works perfect with decimal encoded images and inserts the decimal encoded picture from buffer into the pdf document exactly as hexadecimal encoded images. Thank you.

Many libraries (for example bwip-js) encoding their images with decimal numbers and returning it to the buffer. So first byte of generated image looks like a decimal number, for example data[0] for PNG image, generated by bwip-js is 137 (0x89 in hex) and for JPEG - data[0] is 255 (0xff in hex). Your code checking image only if first byte is hexadecimal number, but it can be decimal too.
Because of that, valid images can't pass verification in the if...else statement and code throws an error 'Unknown image format.'.
So my changes add the opportunity to check if image is encoded with decimal number and work with it. I have tested my changes in real projects, and your library works perfect with decimal encoded images. Thank you.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant