Add suport for decimal encoded images #777
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.