galkahana / HummusJS

Node.js module for high performance creation, modification and parsing of PDF files and streams
http://www.pdfhummus.com
Other
1.14k stars 169 forks source link

Example of using streams with createFormXObjectFromJPG #318

Open stevebaldwin21 opened 5 years ago

stevebaldwin21 commented 5 years ago

I am trying to pass a stream from node-canvas with the PDFRStreamForBuffer class into this method and getting the error of;

TypeError: unable to create form xobject. verify that the target is an existing jpg file/stream

Could you please give an example of how to use this method with the stream instance.

var data = canvas.toBuffer('image/jpg');
var stream = new PDFRStreamForBuffer(data);
var pdfWriter = new hummus.createWriterToModify("Attachment 1.pdf", {
    modifiedFilePath: __dirname + '/test.pdf'
});
pdfWriter.createFormXObjectFromJPG(stream);

Thanks

galkahana commented 5 years ago

this would be a trivial example using PDFRStreamForFile, with a jpg file:

var stream= new hummus.PDFRStreamForFile('/path/to/my/jpg/file');
pdfWriter.createFormXObjectFromJPG(stream);

but sounds like the syntax is quite clear to you. what's not clear is whether node-canvas can be used in PDFRStreamForBuffer. so here's a little test for you. PDFRStreamForBuffer is a custom stream, and as such implements this. use read and notEnded to read the image and write the result to a file...then see if you got a jpeg. you probably wont, and then you just have to figure out why this part is not working. either node-canvas stream cant be used together with PDFRStreamForBuffer,and you'll have to find, or write you own Custom Stream wrapper, or you'll find something surprising. Gal.

stevebaldwin21 commented 5 years ago

Hey @galkahana

Thanks for your fast response. I'll check try your example. {node-canvas}.toBuffer method returns a buffer which should be compatible with PDFRStreamForBuffer (in that it implements the byteLength).

Off topic; your dog is hella cute and great work on the repo!!!

shaehn commented 5 years ago

@stevebaldwin21 did you get the buffer mechanism to work? If so, could you share your example? thanks.

thebenlamm commented 5 years ago

From the docs linked above it seems you can use image streams. However after looking at the source it seems that the drawImage function requires a string argument for filename.

Can someone confirm this? Am I making a mistake?

caribviper commented 5 years ago

It seems like it.

Look at the example provided on the wiki here

stevebaldwin21 commented 5 years ago

@shaehn

Sorry!

I never managed to get this working in the way I would like. I instead decided to use a different solution. (I can't however say as its private code for my work), I can vouch this is a great library however and hope to see this in the future!