DEV Community

Cover image for Generate PDF from HTML Content in Node.js Applications
Froala
Froala

Posted on • Originally published at froala.com

Generate PDF from HTML Content in Node.js Applications

If you’re building a content-driven application, sooner or later you’ll face this requirement: convert HTML into a downloadable PDF on the server.

Maybe you’re generating invoices. Maybe you’re exporting reports. Maybe you’re allowing users to export editor content to PDF from a CMS or LMS.

If you’re working with clean HTML from a rich text editor like Froala, the good news is this: Node.js gives you powerful, production-ready options for server-side PDF generation.

In this guide, we’ll walk through:

  • The best libraries for NodeJS HTML to PDF

  • A direct comparison of Puppeteer, PDFKit, and jsPDF

  • How to implement server-side PDF generation in Express

  • Performance trade-offs and scaling considerations

  • Best practices for rendering WYSIWYG editor content to PDF

This is not a theory. This is implementation.

Why Server-Side PDF Generation in Node.js?

If you’re using a WYSIWYG editor like Froala, you’re already storing clean, structured HTML. For example, after integrating the Froala editor into your Node.js app, you’ll receive HTML via API and store it in your database.

That HTML becomes the input for your PDF service.

Server-side PDF generation in Node offers:

  • Full layout control

  • Access to custom fonts and assets

  • Secure processing (no client tampering)

  • Reliable export for invoices, reports, contracts

  • Better scaling for business workflows

Client-side PDF tools are useful, but this guide focuses strictly on server-side PDF generation Node workflows.

Comparing Node.js HTML to PDF Libraries

When evaluating the best HTML to PDF for NodeJS, three names dominate:

  1. Puppeteer

  2. PDFKit

  3. jsPDF

Let’s break them down properly.

1. Puppeteer (Recommended for Editor Content)

Best for: Rendering real HTML/CSS exactly as it appears in a browser.

Puppeteer is a headless Chrome automation library. It renders your HTML like a browser and exports the page as a PDF.

Why it’s ideal for WYSIWYG content

If you’re generating PDFs from rich editor output (tables, images, page breaks, styling), Puppeteer is the most reliable choice.

It supports:

  • Modern CSS

  • Web fonts

  • Images

  • Page break control

  • Complex layouts

  • Print CSS rules

Performance note: Puppeteer PDF generation can take approximately 1–3 seconds per document on average hardware, depending on complexity.

Core Puppeteer Implementation

Below is the standard, production-safe API pattern for generating a PDF from HTML:

const puppeteer = require('puppeteer');

async function generatePdfFromHtml(htmlContent) {
  const browser = await puppeteer.launch({
    headless: 'new',
    args: ['--no-sandbox', '--disable-setuid-sandbox']
  });

  const page = await browser.newPage();

  await page.setContent(htmlContent, {
    waitUntil: 'networkidle0'
  });

  const pdfBuffer = await page.pdf({
    format: 'A4',
    printBackground: true,
    margin: {
      top: '20mm',
      bottom: '20mm',
      left: '15mm',
      right: '15mm'
    }
  });
Enter fullscreen mode Exit fullscreen mode

That’s it. This is the standard Puppeteer HTML to PDF workflow.

Example: Convert HTML to PDF in Express

aapp.post('/convert-html-to-pdf', async (req, res) => {
  try {
    const { html } = req.body;

    const pdfBuffer = await generatePdfFromHtml(html);

    res.set({
      'Content-Type': 'application/pdf',
      'Content-Disposition': 'attachment; filename=document.pdf'
    });

    res.send(pdfBuffer);
  } catch (err) {
    res.status(500).json({ error: 'PDF generation failed' });
  }
});
Enter fullscreen mode Exit fullscreen mode

This is a typical convert HTML to PDF Express implementation.

2. PDFKit

Best for: Programmatic PDF generation (not HTML rendering).

PDFKit is a pure nodejs pdf generation library. It does not render HTML. Instead, you manually construct PDFs using drawing commands.

Example:

const PDFDocument = require('pdfkit');

const doc = new PDFDocument();
doc.text('Hello world');
doc.end();
Enter fullscreen mode Exit fullscreen mode

Strengths

  • Lightweight

  • No browser dependency

  • Great for invoices or simple documents

  • Fine-grained layout control

Limitations

  • Cannot directly render HTML

  • Requires manual layout logic

  • Not suitable for exporting WYSIWYG content

3. jsPDF

Best for: Client-side PDF generation (mostly).

While jsPDF can technically run in Node, it’s primarily browser-focused.

Why it’s usually not ideal here

  • Limited CSS support

  • No full HTML rendering

  • Better suited for front-end workflows

Since this guide focuses on server-side implementation, jsPDF is typically not the right tool.

Puppeteer vs PDFKit vs jsPDF

When developers search for PDFKit vs Puppeteer, the deeper comparison usually includes jsPDF as well.

Here’s the practical breakdown for NodeJS HTML to PDF use cases:

Why Clean HTML Matters

When generating PDFs from editor content, HTML quality directly affects rendering quality.

This is where Froala becomes important.

Froala produces:

  • Structured HTML

  • Semantic markup

  • Clean formatting

  • Predictable output

That makes it an ideal input for generating PDF from HTML NodeJS workflows.

If you haven’t yet integrated Froala, review this guide:

…after integrating the Froala editor into your Node.js app.

Clean HTML means:

  • Fewer layout bugs

  • Reliable page breaks

  • Better print styling

  • Easier debugging

Structuring HTML for Better PDFs

If you’re exporting long documents, page control matters.

You can:

  • Add CSS page-break-before

  • Use @media print styles

  • Structure headings clearly

If you’re building document workflows, consider using the Page Break plugin to structure your HTML for PDF.

This approach is also complementary to Froala’s built-in Word export feature, giving users multiple document export options.

And if you’re building automated reports or templated outputs, explore ideas similar to workflows used for other document automation workflows.

Production Optimization Tips

If you’re implementing server-side pdf generation node services at scale, consider:

1. Browser Pooling

Launching a new Chromium instance for every request is expensive.

Use a browser pool to reuse instances.

2. Queue System

PDF generation is CPU-intensive.

Use:

  • Redis queues

  • Background workers

  • Rate limiting

3. Static Assets

Ensure:

  • Fonts are accessible

  • Images use absolute URLs

  • Assets load before networkidle0

4. Print-Specific CSS

Use:

@media print {
  body {
    font-size: 12pt;
  }
}
Enter fullscreen mode Exit fullscreen mode

5. Page Break Control

.page-break {
  page-break-before: always;
}
Enter fullscreen mode Exit fullscreen mode

This ensures your editor content renders exactly as expected.

Decision Framework: Which Library Should You Use?

Use this checklist:

Choose Puppeteer if:

  • You need full HTML rendering

  • You export WYSIWYG content

  • You need CSS support

  • You want accurate layout replication

Choose PDFKit if:

  • You’re generating simple PDFs

  • Layout is fully controlled programmatically

  • No HTML rendering is required

Avoid jsPDF (server-side) if:

  • You need production-grade HTML rendering

For most real-world editor workflows, Puppeteer is the best html to pdf for nodejs.

Complete Working Example: Froala → PDF Export

To make this implementation easier to test, we’ve created a full working example that connects:

  • A Froala editor page

  • A Node.js + Express backend

  • Puppeteer for server-side PDF rendering

In this example, you can:

  • Type rich content in Froala

  • Insert page breaks

  • Click Export to PDF

  • Download a fully rendered A4 PDF generated on the server

Get the complete source code on GitHub.

The repository includes:

  • Froala editor integration

  • /convert-html-to-pdf Express endpoint

  • Production-safe Puppeteer configuration

  • Proper binary PDF response handling

  • Page break support

This gives you a complete, real-world reference implementation for exporting WYSIWYG editor content to PDF in Node.js.

Final Thoughts

When developers search for NodeJS HTML to PDF, they’re usually not asking “what is PDF generation?”

They’re asking:

“What’s the most reliable way to convert editor HTML into a production-ready PDF?”

The answer, in most cases, is:

  • Clean HTML (from a structured editor like Froala)

  • Puppeteer for accurate rendering

  • Express endpoint for delivery

  • Performance optimization for scale

This approach gives you:

  • Reliable exports

  • Business-ready documents

  • Full styling control

  • Scalable backend architecture

If you’re already generating structured content in your Node.js app, PDF generation becomes the natural next step in your content workflow.

And now, you have the implementation roadmap.

This article was published on the Froala blog.

Top comments (0)