Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert pdfkit object into buffer using nodejs

I am generating pdf document using pdfkit(nodejs module).i need to convert the pdfkit object to buffer and send response as attachment file without save a file in server.

i was using output function to achieve this:

pdfdocument.output(function(buffer){
    return buffer;
});

pdfkit deprecated the output function.

so right now i dont know how to do any idea...

like image 730
sridhar Avatar asked May 15 '14 19:05

sridhar


1 Answers

Working example for pdfkit v0.8.0:

let pdf = new pdfkit();

let buffers = [];
pdf.on('data', buffers.push.bind(buffers));
pdf.on('end', () => {

    let pdfData = Buffer.concat(buffers);

    // ... now send pdfData as attachment ...

});

pdf.text('Hello', 100, 100);
pdf.end();

Hope it helps :)

like image 72
dimacpp Avatar answered Sep 17 '22 12:09

dimacpp