How can I set the filename for a PDF file created with pdfKit for sending via HTTP (rather than saving locally). The following doesn't set the filename and the decs only show how to set the file name for saving locally.
var doc = new PDFDocument({bufferPages: true});
doc.info.Title = 'Case ' + req.params.id + ' overview';
// Some more content here
doc.pipe(res);
I've tried setting headers too but that doesn't seem to help.
res.setHeader('Content-disposition', 'attachment; filename=testFile.pdf');
I know this is a old thread but this answer may be helpful to some one I think.
let doc = new PDFDocument();
doc.info['Title'] = 'Test Document';
Even though this is old, I am commenting because it took me a while to find the working solution and this is still the top google response.
To get the PDF to display in the browser but define the download name I used:
res.setHeader('Content-disposition', 'inline; filename='+ yourfilename +'.pdf');
hope this helps someone else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With