Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set filename for PDFKit piped to HTTP response

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');
like image 802
Geraint Anderson Avatar asked Jan 11 '17 12:01

Geraint Anderson


2 Answers

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';
like image 94
Manoj Krishna Avatar answered Nov 12 '22 03:11

Manoj Krishna


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.

like image 2
user13626624 Avatar answered Nov 12 '22 02:11

user13626624