question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I save buffer as PDF in client side(in browser on response as buffer) ?

See original GitHub issue

This is how I sending buffer in the response.

htmlToPDF.create(finalHTML, options).toBuffer((err, buffer) => {
                         if (err) {
                           res.json(responses.genericError(500, 'Internal server error.'));
                         } else {
                           res.type('application/pdf');
                           res.statusCode = 200;
                           res.send({ data: buffer, success: true });
                         }
                       });

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:13

github_iconTop GitHub Comments

6reactions
GunjanSkrycommented, Nov 19, 2018

This is the code that I’ve written to download pdf on the client side,

    res.writeHead(200, {
      'Content-Type': 'application/pdf',
      'Content-disposition': `attachment; filename=test.pdf`,
    });

    pdf.create(html, option).toStream((err, stream) => {
      stream.pipe(res);
    });
1reaction
cavasinfcommented, Jul 3, 2020

@lowzijian If you have res.setHeader('Content-disposition', 'attachment; filename=export-from-html.pdf'); // Remove this if you don't want direct download It will automatically popup the “file open or download” on the response of your “controller”. But if you comment this line, your browser will open the stream with a PDF reader (eg Firefox).

Here is my code if you want :


const html_pdf = require('html-pdf');

    res.render('pages/index', { res: res },function (err, html) {
        const html_for_pdf = "<h1>Am I a true HTML ?</h1>";
        html_pdf.create(html_for_pdf ).toStream((err, stream) => {
          if (err) return res.end(err.stack);
          res.setHeader('Content-type', 'application/pdf');
          res.setHeader('Content-disposition', 'attachment; filename=export-from-html.pdf'); // Remove this if you don't want direct download
          res.setHeader('Content-Length', ''+stream.length);
          stream.pipe(res);
        });
    });
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to output a PDF buffer to browser using NodeJS?
Try this in client side: var oReq = new XMLHttpRequest(); oReq.open("GET", url, true); oReq.responseType = "arraybuffer"; oReq.onload ...
Read more >
How can I serve a PDF to a browser without storing a file on ...
How can I serve the PDF file to the client without storing the file on the server side and allow the client side...
Read more >
Puppeteer HTML to PDF Generation with Node.js
It is possible to generate a PDF file both on the client-side and on ... the PDF won't be saved to the disk,...
Read more >
How to create PDF files - Django documentation
The response will automatically set the MIME type application/pdf based on the filename extension. This tells browsers that the document is a PDF...
Read more >
Methods - PDFMake
Download the PDF · Open the PDF in a new window · Print the PDF · Put the PDF into your own page...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found