How do I output the pdf using expressjs:
var fs = require('fs');
var PdfPrinter = require('pdfmake/src/printer');
app.get('/', function (req, res) {
var printer = new PdfPrinter();
var first = 'Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines';
var dd = {
content: [
first,
'Another paragraph'
]
};
var pdfDoc = printer.createPdfKitDocument(dd);
pdfDoc.pipe(fs.createWriteStream('basics.pdf')).on('finish',function(){
//success
});
pdfDoc.end();
});
You can pipe the output to res
(after making sure that you set the correct Content-Type
):
app.get('/', function (req, res) {
var printer = new PdfPrinter();
var first = 'Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines';
var dd = {
content: [
first,
'Another paragraph'
]
};
// Make sure the browser knows this is a PDF.
res.set('content-type', 'application/pdf');
// Create the PDF and pipe it to the response object.
var pdfDoc = printer.createPdfKitDocument(dd);
pdfDoc.pipe(res);
pdfDoc.end();
});
(although I can't say that it yields legible PDF's for me, but neither does the code when run standalone or any of the pdfmake
examples)