2

Using:

var doc = new PDFDocument();
doc.pipe(res);
doc.text('Add content here');
doc.end();

within an Express GET route I'm able to navigate in the browser and view the PDF. Once viewed it can be saved or printed.

That's good, but what I need is for the PDF to automatically download. The application will send a PUT to Express. From the one PUT, is it possible to also auto-download the PDF? Angular needs a 200 response to return control to Angular and the user, but the PDF also needs to be downloaded.

I've tried res.end(new Buffer(doc), 'binary), res.send(new Buffer(doc, 'binary')), etc, but can't get the PDF to download. If I can't return a 200 and download the PDF from a PUT then my fallback is to PUT and once that returns do a window.open to the GET route to view the PDF. Kind of hacky, so I would really prefer a download.

Options?

Erik Olson
  • 5,807
  • 1
  • 18
  • 17
  • Use window.open for downloading file - it is good solution. "Most of the references on the web about this issue point out to the fact that you cannot download files via ajax call 'out of the box'" please see http://stackoverflow.com/questions/20904151/download-text-csv-content-as-files-from-server-in-angular. – Nikolay Lukyanchuk May 22 '14 at 07:13
  • I'm sending the PUT via `$resource`. I'm now pretty sure that's just not going to work because `$resource` is meant to interact with an API. I went with the `windows.open(url, '_self')` approach. Only thing I don't like is that it results in a console error `Failed to load resource: Frame load interrupted`, but I guess I can live with that. – Erik Olson May 22 '14 at 14:44
  • Also, I needed to add `res.setHeader('Content-disposition', 'attachment; filename=' + fileName );` within my server controller to tell the browser (1) it's a file to be downloaded as an attachment, not to be rendered, and (2) to provide a filename. – Erik Olson May 22 '14 at 14:45

0 Answers0