how to convert pdfkit object into buffer using nodejs

蓝咒 提交于 2019-12-23 09:58:15

问题


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...


回答1:


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 :)



来源:https://stackoverflow.com/questions/23686843/how-to-convert-pdfkit-object-into-buffer-using-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!