From 47dd35229488619c5d6c6ee7071c3db5b9e2a722 Mon Sep 17 00:00:00 2001 From: BimBoss Date: Sun, 11 Feb 2018 02:51:53 +0300 Subject: [PATCH] Add suport for decimal decoded images 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. --- lib/image.coffee | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/image.coffee b/lib/image.coffee index 5078c4ab2..1fee18985 100644 --- a/lib/image.coffee +++ b/lib/image.coffee @@ -22,13 +22,13 @@ class PDFImage data = fs.readFileSync src return unless data - if data[0] is 0xff and data[1] is 0xd8 + if data[0] is 0xff or 255 and data[1] is 0xd8 or 216 return new JPEG(data, label) - else if data[0] is 0x89 and data.toString('ascii', 1, 4) is 'PNG' + else if data[0] is 0x89 or 137 and data.toString('ascii', 1, 4) is 'PNG' return new PNG(data, label) else throw new Error 'Unknown image format.' -module.exports = PDFImage \ No newline at end of file +module.exports = PDFImage