This article is about fixing How do I save buffer as PDF in client side(in browser on response as buffer) ? in marcbachmann node-html-pdf
  • 30-Jan-2023
Lightrun Team
Author Lightrun Team
This article is about fixing How do I save buffer as PDF in client side(in browser on response as buffer) ? in marcbachmann node-html-pdf

How do I save buffer as PDF in client side(in browser on response as buffer) ? in marcbachmann node-html-pdf

Lightrun Team
Lightrun Team

Explanation of the problem

The given code block is a part of a Node.js server function that is used to convert HTML to PDF and send the resulting buffer in the response.

In the code, the “htmlToPDF.create” method is called with the “finalHTML” parameter and “options”. The method returns a Promise that generates the PDF buffer when resolved. The “.toBuffer” method is used to handle the Promise and convert it to a buffer.

In the callback function passed to the “.toBuffer” method, error handling is performed. If there is an error during the conversion, a JSON response is sent with a “500 Internal server error” status code. If there is no error, the response type is set to “application/pdf”, the status code is set to “200”, and the PDF buffer is sent as a success JSON response.

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for How do I save buffer as PDF in client side(in browser on response as buffer) ? in marcbachmann node-html-pdf

To save a PDF buffer in the client-side browser, you can use the following code in the response handler of the server:

htmlToPDF.create(finalHTML, options).toBuffer((err, buffer) => {
  if (err) {
    res.json(responses.genericError(500, 'Internal server error.'));
  } else {
    res.setHeader('Content-Type', 'application/pdf');
    res.setHeader('Content-Disposition', 'attachment; filename=pdfFile.pdf');

Here, in the response handler, the “Content-Type” header is set to “application/pdf” to indicate that the response is a PDF file. The “Content-Disposition” header is set to “attachment; filename=pdfFile.pdf” to specify the file name of the PDF file that will be saved on the client-side. The “buffer” is sent as the response body.

This will cause the browser to prompt the user to download and save the PDF file, which is represented by the buffer in the response.

Other popular problems with marcbachmann node-html-pdf

Problem: Compatibility issues with certain HTML/CSS elements

One of the most common problems faced by users of the “node-html-pdf” library is compatibility issues with certain HTML and CSS elements. The library uses PhantomJS under the hood, which might not support all the latest HTML and CSS features. As a result, the PDF generated from HTML may not look the same as the original HTML, or some elements may not render at all.


To resolve compatibility issues, users can try using alternative libraries like “puppeteer” or “pdfkit” that use more modern rendering engines. Additionally, users can also try using a different version of PhantomJS or a different rendering engine altogether.

Problem: Slow performance

Another common problem with “node-html-pdf” is slow performance, especially when generating PDFs from large or complex HTML files. This can cause long wait times for users and also lead to increased resource utilization on the server.


To improve performance, users can try reducing the size of the HTML file by removing unnecessary elements, using CSS instead of HTML for formatting, and using caching to avoid re-generating the same PDFs multiple times. Additionally, users can also try using a different rendering engine that is optimized for performance.

Problem: Memory leaks

The “node-html-pdf” library is known to have issues with memory leaks, which can cause the server to run out of memory and crash. This is especially problematic when generating multiple PDFs in quick succession.


To resolve memory leak issues, users can try using a different version of PhantomJS or a different rendering engine altogether. Additionally, users can also try using memory-management techniques like garbage collection and memory pooling to optimize memory utilization on the server.

A brief introduction to marcbachmann node-html-pdf

“node-html-pdf” is a popular library for Node.js developers that allows them to generate PDF files from HTML. The library uses PhantomJS as the underlying rendering engine to convert HTML into a PDF file. It provides an easy-to-use API with a variety of options and configurations that allow developers to customize the look and behavior of the generated PDFs.

“node-html-pdf” supports a wide range of HTML and CSS elements, including tables, images, and complex formatting. It also supports multiple page sizes, margins, and page orientation options. In addition, “node-html-pdf” provides support for headers, footers, and page numbers, making it a versatile solution for a variety of PDF generation needs. The library is well documented and has a large user community, which makes it a popular choice for developers looking for a robust and flexible PDF generation solution.

Most popular use cases for marcbachmann node-html-pdf

  1. Generating Invoices and Receipts: One of the most common use cases for “node-html-pdf” is generating invoices and receipts. This can be achieved by using HTML templates to define the layout and content of the PDF, and then using the “node-html-pdf” library to convert the HTML into a PDF file. The following code shows an example of generating a PDF invoice using “node-html-pdf”:
var pdf = require('html-pdf');
var html = fs.readFileSync('./invoice.html', 'utf8');
var options = { format: 'Letter' };

pdf.create(html, options).toFile('./invoice.pdf', function(err, res) {
  if (err) return console.log(err);
  console.log(res); // { filename: '/app/businesscard.pdf' }
  1. Exporting Reports: Another use case for “node-html-pdf” is exporting reports in PDF format. This can be useful for creating reports that need to be printed or shared with stakeholders who prefer to view reports in PDF format. The library provides options for customizing the look and feel of the PDF, such as page size, margins, and page orientation.
  2. Generating PDFs from HTML Forms: “node-html-pdf” can also be used to generate PDFs from HTML forms, such as contact forms, survey forms, and registration forms. The library provides an easy-to-use API for converting HTML form data into a PDF file, which can be useful for creating printable versions of form submissions. Additionally, “node-html-pdf” provides support for adding headers, footers, and page numbers to the PDF, making it a versatile solution for generating PDFs from forms.

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications. It’s a registration form away.

Get Lightrun

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.