1 year ago
#384584
Sean
PDFKit send PDF back to client from Express Backend
I have an express server that has a PDF generation endpoint using PDFKit.
this.app.post("/print", (req: Request, res: Response): void => {
const header = { ...req.body };
printHandlers.execute(header).then(pdf => {
res.writeHead(200, {
'Content-Type': 'application/pdf',
'Access-Control-Allow-Origin': '*',
'Content-Disposition': 'attachment; filename='+header.po_num+'.pdf'
});
console.log(pdf);
return res.end(pdf);
})
.catch(err => console.log(err));
})
This is code that actually generates the PDF:
//creates new pdf object
.then((location) => {
let pdfObj = new createPDF(obj, buffer, location);
//console.log(pdfObj)
pdfObj.generate();
return pdfObj;
})
This is the generate() method:
generate(){
let output = new PDFGenerator();
const fileName: string = `${this.data.header.po_num}.pdf`;
const stream = fs.createWriteStream(fileName)
output.pipe(stream);
this.generateHeaders(output);
output.moveDown();
this.generateTable(output);
output.end();
}
I am trying to send the PDF back to the client, and I am struggling on how to do this? Using arrayBuffer? Any other ideas?
javascript
node.js
express
pdfkit
0 Answers
Your Answer