pdffillerjs / pdffiller

Take an existing PDF Form and data and PDF Filler will create a new PDF with all given fields populated.
MIT License
286 stars 113 forks source link

How to handle PDF Stream at fillForm method #49

Open hmtri1011 opened 7 years ago

hmtri1011 commented 7 years ago

In README we have an example of fillForm method:

pdfFiller.fillForm( sourcePDF, destinationPDF, data, function(err) {
    if (err) throw err;
    console.log("In callback (we're done).");
});

I have a question, can we have a way to handle the filled form PDF before saving it to destinationPDF path??

In my project, can I send a filled PDF to my client through REST API. Here is my code..

pdfFiller.fillForm('http://res.cloudinary.com/xx/form1.pdf', destPDF, data, (err, result) => {
                        if (err) throw err;
                        console.log("In callback (we're done).");
                        //how to send a filled PDF to my client??
                    });

Any help would be appreciated, tks so much!

jasonphillips commented 7 years ago

I needed this as well (for a microservice where I don't want to generate a lot of temporary files) but then realized it's just not the design of this library at the moment, since it invokes pdftk with execFile and then waits for its completion.

So I forked the project and set it up to return a stream within a promise: https://github.com/jasonphillips/pdffiller-stream

Streams can be served to the client in Express (though I forget the exact syntax) so from here it should be straightforward for you to implement your response if you search those docs.

I haven't PR-ed any of that fork's work here since it feels like a slightly different goal, but am certainly open to doing so.

hmtri1011 commented 7 years ago

Tks @jasonphillips

whitef0x0 commented 7 years ago

@jasonphilips if you added streaming as an option In sure we would be open to adding it to the project

helgetan commented 7 years ago

Could you make an example how to do this with express ? i'm a bit stuck here:

export function fillForm(req, res) {
...

return pdfFiller.fillFormWithFlatten(sourcePDF, data, shouldFlatten)
.then(outputStream => {
      res.header('Content-disposition', 'inline; filename=' + 'sdfsdf.pdf');
      res.header('Content-type', 'application/pdf');
      console.log('done');

      const streamRes = outputStream.pipe(res);
      return streamRes.end();
    });
}`