hummusRecipe
hummusRecipe copied to clipboard
Is it possible to add image in memory (PNG, JPEG) to PDF?
I have been looking for awhile now to see if there was a way to use a memory buffer image that was in one of the well known image formats that hummus and hummus-recipe know how to handle. So far all the interfaces I see imply that the image has to come from a file in the filing system. Is it even possible to do such a thing, or am I going to have to write the image to disk first, then use the current interfaces?
I believe it's possible. Since I am using drawImage
(https://github.com/chunyenHuang/hummusRecipe/blob/master/lib/image.js#L42) from HummusJS, you should be able to pass the stream instead of path, please check https://github.com/galkahana/HummusJS/wiki/Show-images#streams
Hmmm... unfortunately it seems to have failed miserably. This is what I tried.
const bwipjs = require('bwip-js');
const HummusRecipe = require('hummus-recipe');
const hummus = require('hummus');
let options = {
bcid: 'code128', // Barcode type
text: '0123456789', // Text to encode
scale: 1, // 3x scaling factor
height: 10, // Bar height, in millimeters
includetext: true, // Show human-readable text
textxalign: 'center', // Always good to set this
};
bwipjs.toBuffer(options, function(err, png) {
const width = png.readUInt32BE(16);
const height = png.readUInt32BE(20);
const stream = new hummus.PDFRStreamForBuffer(png)
const pdfDoc = new HummusRecipe('new', 'barcode.pdf');
pdfDoc.createPage('letter-size');
pdfDoc.image(stream, 100, 100, {
width: width,
height: height
})
.endPage()
.endPDF();
})
Which gave me the following error: (In this case imgSrc is the stream object I fed in above)
c:\Users\guest\pdf_test\node_modules\hummus-recipe\lib\image.js:70
const dimensions = this.writer.getImageDimensions(imgSrc);
^
TypeError: wrong arguments, pass 1 to 3 arguments. a path to an image, an optional image index (for multi-image files), and an options object
at Recipe._getImgOffset (c:\Users\guest\pdf_test\node_modules\hummus-recipe\lib\image.js:70:36)
at Recipe.image (c:\Users\guest\pdf_test\node_modules\hummus-recipe\lib\image.js:20:54)
at c:\Users\guest\pdf_test\barcode.js:21:12
So I did not even get to the code where the drawing is done. ..... I went and hacked at image.js to allow for width&height to be specified in options to get passed the error thrown by hummus above, but fell into this one afterwards.
c:\Users\shaehn\json2pdf\node_modules\hummus-recipe\lib\image.js:42
.drawImage(0, 0, imgSrc, imgOptions)
^
TypeError: Wrong Arguments, please provide bottom left coordinates, an edge size and optional options object
at Recipe.image (c:\Users\shaehn\json2pdf\node_modules\hummus-recipe\lib\image.js:42:14)
at c:\Users\shaehn\json2pdf\barcode.js:21:12
So I am not sure where to go from here via the buffer method.
According to HummusJS docs you should be able to use a buffer to draw an image. However based on the source code it does not look like it is supported.
Open issue in HummusJS https://github.com/galkahana/HummusJS/issues/354