Search code examples
node.jspdfkitnode-pdfkit

How to get pdf data in pdfkit without saving it to file


I am using pdfkit in nodejs to create pdfs. Right now, to get the data from pdfDocument, I first write it to a file using 'fs' and then read back from it.

I want to be able to use the data directly from pdfDocument object and send it as a response. How can I do that?


Solution

  • Each pdfDocument is a stream. You can basically pipe it to the response like this:

    require('http').createserver(function (request, response) {
        var pdfdocument = require('pdfkit'),
            pdfdocument = new pdfdocument();
    
        pdfdocument.text('wassup');
        pdfdocument.pipe(response);
    
        pdfdocument.end()
    }).listen(1999);