pdforge logo

Product

Resources

Integrations

pdforge logo

Redirected from pdforge.com? You’re in the right place. we’re now pdf noodle!

Redirected from pdforge.com? You’re in the right place. we’re now pdf noodle!

Redirected from pdforge.com? You’re in the right place. we’re now pdf noodle!

How to Generate PDF from HTML with Playwright with using NodeJs (Updated 2025)

Written by

Written by

Marcelo Abreu, founder of pdforge

Marcelo | Founder of pdf noodle

Marcelo | Founder of pdf noodle

Last Updated

Last Updated

Jul 10, 2025

Jul 10, 2025

Tags

Tags

PDF Libraries

PDF Libraries

Javascript

Javascript

pdforge logo
pattern behind Call to action

What is Playwright and How Can I Use It to Convert HTML to PDF?

Playwright is a powerful open-source automation library designed to handle browser tasks, such as testing, web scraping, and PDF generation. It supports multiple browsers, including Chromium, Firefox, and WebKit, ensuring consistent rendering of web content. Developers widely use Playwright due to its simplicity, speed, and exceptional handling of dynamic content.

When it comes to generating PDFs from HTML, Playwright stands out because it captures exactly what the browser displays—including CSS styling, JavaScript-rendered content, and complex page layouts. This ensures pixel-perfect quality, making it perfect for generating invoices, reports, or dynamic documentation.

Playwright also has well-written, robust documentation that you can use if you run into any trouble.


If you're using other programming languages, check out our other guides:

Alternative PDF Libraries: How Playwright Compares to Other Tools

Several JavaScript alternatives exist for PDF generation:

print from npmtrends showing playwright growth
  • Puppeteer: Similar to Playwright but supports only Chromium browsers.

  • jsPDF: Lightweight and easy-to-use, but limited with complex CSS and JavaScript rendering.

  • pdf-lib: Great for PDF manipulation, but lacks HTML rendering capabilities.

  • pdfmake: Useful for structured document creation, less flexible with complex HTML/CSS.

  • pdfkit: Primarily for simple PDF documents, struggles with complex layouts and dynamic content.

  • react-pdf: Ideal for React applications but not suited for general-purpose HTML-to-PDF rendering.

Playwright has recently seen skyrocketing popularity in downloads and community support according to npmtrends data. Its flexibility, browser compatibility, and exceptional handling of dynamic content have contributed significantly to its growing adoption.

If you want to dig deeper on a comparison between playwright and other javascript pdf libraries, we also have a detailed article with a full comparison between the best PDF libraries for NodeJs in 2025.

Html to pdf using playwright
Html to pdf using playwright
Html to pdf using playwright

Step 01: Setting Up Playwright for HTML to PDF Conversion

First, ensure Node.js is installed on your machine. If not, download it from nodejs.org.

Then, install Playwright via npm:

npm

This command automatically includes all necessary browser engines (Chromium, Firefox, WebKit).

Step 02: Generating Your First "Hello World" PDF

Creating a PDF with Playwright involves running a simple Node.js script. Let's create our first basic example:

Alternative 01: Generating PDF from a URL

Here’s how you can generate a PDF from any URL:

const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await page.pdf({ path: 'example.pdf', format: 'A4' });
  await browser.close();
})();

This script:

  • Opens a headless Chromium browser

  • Navigates to your chosen URL

  • Saves the resulting PDF

Alternative 02: Generating PDF from an HTML File

If you already have an HTML file or a raw HTML string, follow these steps:

Directly using a string without a file:

const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();
  const htmlContent = '<h1>Hello World</h1><p>This is your first PDF generated by Playwright.</p>';
  await page.setContent(htmlContent);
  await page.pdf({ path: 'hello-world.pdf', format: 'A4' });
  await browser.close();
})();

Playwright Print Configurations (Footer, Header, Margins, etc.)

Playwright allows detailed customizations, such as setting headers, footers, margins, and more:

await page.pdf({
  path: 'configured.pdf',
  format: 'Letter',
  margin: { top: '20px', bottom: '20px' },
  displayHeaderFooter: true,
  headerTemplate: '<span style="font-size:10px;">Header content</span>',
  footerTemplate: '<span style="font-size:10px;">Page <span class="pageNumber"></span></span>'
});

Refer to the Playwright Print Configuration documentation for more detailed options.

Using HTML Template Engines

For dynamic HTML generation, template engines like Handlebars, EJS, or Pug can streamline the process. Here’s an example using Handlebars:

const handlebars = require('handlebars');
const template = handlebars.compile('<h1>{{title}}</h1><p>{{content}}</p>');
const html = template({ title: 'Hello', content: 'Generated PDF content.' });

await page.setContent(html);
await page.pdf({ path: 'templated.pdf', format: 'A4' });

Step 03: Creating a Serverless Architecture to Scale PDF Generation

Playwright can be resource-intensive, consuming significant memory and processing power when rendering complex HTML content. To efficiently handle high volumes of PDF generation requests, using a serverless architecture like AWS Lambda is highly beneficial. This approach reduces costs by scaling automatically and only consuming resources when needed.

Note that you must install the browser engine (e.g., Chromium) on the Lambda layer or any other serverless service you're using. While various installation guides exist online, what worked best for me was using a dockerized container to install the browser engine effectively.

I’ve prepared a detailed step-by-step guide to help you set this up, available here.

Alternative: Convert HTML to PDF Using pdf noodle

Homepage of pdf noodle

Managing HTML-to-PDF conversion at scale can quickly become a nightmare!

Especially in serverless environments where cold starts, memory limits, and headless browser quirks love to break at the worst possible time (we even wrote a full article about it). Add constant template iterations, version control headaches, and the need to support non-technical contributors, and suddenly your “simple PDF library” turns into an ongoing engineering project.

pdf noodle eliminates all of that.

Instead of maintaining brittle infrastructure or wrestling with outdated pdf libraries, pdf noodle gives you a battle-tested PDF generation API that just works!

Fast, scalable, and designed for both developers and non-developers. You send raw HTML or use our AI-powered template builder, and pdf noodle handles the rendering, scaling, optimization, and delivery so your team doesn’t have to.

Here's an example of a simple API request to generate your pixel-perfect PDF with just a few lines of code:

fetch('https://api.pdfnoodle.com/v1/html-to-pdf/sync', {
  method: 'POST',
  body: JSON.stringify({ html:'your-html' } }),
  headers: { 'Authorization' : 'Bearer your-api-key' }
});

pdf noodle also includes a powerful AI Agent that can generate PDF templates instantly, along with a modern editor for refining the design, also using AI, to match your brand. You don't need developing or design experience to quickly update layouts, adjust styling, and manage template versions.

Here’s a quick demo showing how it works:

You can create your account and design your first template without any upfront payment.

Conclusion

Playwright is a robust, versatile tool for generating PDFs from HTML. Its ability to render pixel-perfect PDFs, combined with rapidly increasing popularity, positions it as an exceptional solution for generating high-quality PDFs with complex layouts at scale.

If you don't want to waste time maintaining pdfs layouts and their infrastructure or if you don't want to keep track of best practices to generate PDFs at scale, third-party PDF APIs like pdf noodle will save you hours of work and deliver a high quality pdf layout.

Generating pdfs can be annoying!

Let us help you make it easier while you focus on what truly matters for your company.

pdforge logo
pattern behind Call to action

Generating pdfs can be annoying!

Let us help you make it easier while you focus on what truly matters for your company.

pdforge logo
pattern behind Call to action

Generating pdfs can be annoying!

Let us help you make it easier while you focus on what truly matters for your company.

pdforge logo
pattern behind Call to action

Table of contents

Automate PDF Generation in minutes

No code or design experience needed

AI creates your template in seconds

Fine tune the design in our friendly builder

Generate PDFs with our API or integrations