npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

fast-html-pdf

v1.0.1

Published

Fast HTML → PDF generator using Playwright. Full CSS support, simple API, perfect for reports, invoices, and documents.

Readme

fast-html-pdf

High-performance HTML to PDF converter using Playwright. Simple API with full CSS support, perfect for generating reports, invoices, and documents.

npm version License: MIT Node.js >= 18

Features

Easy to Use - Simple, intuitive API
Full CSS Support - Style PDFs with complete CSS capabilities
Responsive Design - Works with modern web technologies
High Performance - Fast conversion with reliable output
Production Ready - Error handling, type safety, and comprehensive documentation
No Dependencies - Minimal footprint (Playwright only)
Batch Processing - Convert multiple documents efficiently

Installation

npm install fast-html-pdf

Requirements:

  • Node.js >= 18.0.0
  • ~300MB disk space for Playwright browsers (downloaded on first use)

Quick Start

Basic Usage

import { htmlToPdfFile } from 'fast-html-pdf';

const html = `
  <h1>Hello World</h1>
  <p>This is a PDF generated from HTML!</p>
`;

const path = await htmlToPdfFile(html, './output.pdf');
console.log(`PDF saved to: ${path}`);

Get PDF as Buffer

import { htmlToPdfBuffer } from 'fast-html-pdf';

const html = '<h1>Hello World</h1>';
const pdfBuffer = await htmlToPdfBuffer(html);

// Use the buffer directly (e.g., for API responses)
response.setHeader('Content-Type', 'application/pdf');
response.send(pdfBuffer);

With Custom Styling

import { htmlToPdfFile } from 'fast-html-pdf';

const html = `
  <div class="invoice">
    <h1>Invoice #12345</h1>
    <p>Amount: $99.99</p>
  </div>
`;

const css = `
  .invoice {
    font-family: Arial, sans-serif;
    padding: 20px;
    background: #f5f5f5;
  }
`;

await htmlToPdfFile(html, './invoice.pdf', {
  additionalStyles: css,
  format: 'A4',
  margin: { top: 10, right: 10, bottom: 10, left: 10 }
});

Batch Processing

import { htmlToPdfBatch } from 'fast-html-pdf';

const documents = [
  ['<h1>Report 1</h1>', './report1.pdf'],
  ['<h1>Report 2</h1>', './report2.pdf'],
  ['<h1>Report 3</h1>', './report3.pdf']
];

const paths = await htmlToPdfBatch(documents, {
  format: 'A4'
});

console.log('Generated:', paths);

API Reference

htmlToPdfFile(html, outputPath, options?)

Convert HTML to PDF and save to disk.

Parameters:

  • html (string) - HTML content
  • outputPath (string) - Path where to save the PDF
  • options (FastHtmlPdfOptions, optional) - Conversion options

Returns: Promise<string> - Absolute path to the saved PDF

Throws: FastHtmlPdfError - If conversion or file write fails

htmlToPdfBuffer(html, options?)

Convert HTML to PDF and return as Buffer.

Parameters:

  • html (string) - HTML content
  • options (FastHtmlPdfOptions, optional) - Conversion options

Returns: Promise<Buffer> - PDF content as Buffer

Throws: FastHtmlPdfError - If conversion fails

htmlToPdfBatch(htmlArray, options?)

Convert multiple HTML documents efficiently.

Parameters:

  • htmlArray (Array<[html, path]>) - Array of [HTML, output path] tuples
  • options (FastHtmlPdfOptions, optional) - Conversion options

Returns: Promise<string[]> - Array of saved file paths

Throws: FastHtmlPdfError - If any conversion fails

Options

Extended Playwright PDF options with additional configuration:

interface FastHtmlPdfOptions {
  // Playwright PDF options (all supported)
  format?: 'Letter' | 'Legal' | 'Tabloid' | 'Ledger' | 'A0' | 'A1' | 'A2' | 'A3' | 'A4' | 'A5' | 'A6';
  margin?: { top?: number; right?: number; bottom?: number; left?: number };
  width?: string | number;
  height?: string | number;
  scale?: number;
  displayHeaderFooter?: boolean;
  headerTemplate?: string;
  footerTemplate?: string;
  preferCSSPageSize?: boolean;
  printBackground?: boolean;
  
  // fast-html-pdf specific options
  waitUntil?: 'load' | 'domcontentloaded' | 'networkidle'; // default: 'networkidle'
  additionalStyles?: string; // CSS to inject into the HTML
  viewport?: { width: number; height: number };
}

Advanced Examples

Generate Invoice with Dynamic Data

import { htmlToPdfFile } from 'fast-html-pdf';

function generateInvoiceHTML(invoiceData) {
  return `
    <!DOCTYPE html>
    <html>
    <head>
      <style>
        body { font-family: Arial, sans-serif; }
        .invoice-container { max-width: 800px; margin: 0 auto; padding: 20px; }
        .header { text-align: center; border-bottom: 2px solid #333; padding-bottom: 20px; }
        .items { margin: 30px 0; }
        .item { display: flex; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid #eee; }
        .total { font-weight: bold; font-size: 18px; text-align: right; margin-top: 20px; }
      </style>
    </head>
    <body>
      <div class="invoice-container">
        <div class="header">
          <h1>INVOICE</h1>
          <p>Invoice #${invoiceData.id}</p>
          <p>Date: ${new Date().toLocaleDateString()}</p>
        </div>
        <div class="items">
          ${invoiceData.items.map(item => `
            <div class="item">
              <span>${item.description}</span>
              <span>$${item.amount.toFixed(2)}</span>
            </div>
          `).join('')}
        </div>
        <div class="total">
          Total: $${invoiceData.items.reduce((sum, item) => sum + item.amount, 0).toFixed(2)}
        </div>
      </div>
    </body>
    </html>
  `;
}

const invoiceData = {
  id: '001',
  items: [
    { description: 'Web Development', amount: 500 },
    { description: 'UI Design', amount: 300 }
  ]
};

await htmlToPdfFile(
  generateInvoiceHTML(invoiceData),
  './invoice.pdf',
  { format: 'A4', margin: { top: 20, bottom: 20 } }
);

Express.js Integration

import { htmlToPdfBuffer } from 'fast-html-pdf';
import express from 'express';

const app = express();

app.get('/api/report/pdf', async (req, res) => {
  try {
    const html = `<h1>Monthly Report</h1><p>Generated on ${new Date().toISOString()}</p>`;
    const pdf = await htmlToPdfBuffer(html, { format: 'A4' });
    
    res.setHeader('Content-Type', 'application/pdf');
    res.setHeader('Content-Disposition', 'attachment; filename="report.pdf"');
    res.send(pdf);
  } catch (error) {
    res.status(500).json({ error: 'Failed to generate PDF' });
  }
});

app.listen(3000);

Error Handling

import { htmlToPdfFile, FastHtmlPdfError } from 'fast-html-pdf';

try {
  await htmlToPdfFile('<h1>Test</h1>', './output.pdf');
} catch (error) {
  if (error instanceof FastHtmlPdfError) {
    console.error('PDF conversion failed:', error.message);
    console.error('Original error:', error.originalError);
  } else {
    console.error('Unexpected error:', error);
  }
}

Perfect For

Reports & Analytics - Generate business reports dynamically
Invoices & Receipts - Create professional billing documents
Certificates - Design and generate certificates
Tickets & Passes - Generate event tickets and boarding passes
Data Export - Export data as formatted PDFs
Email Attachments - Generate PDFs for email distribution

Performance Tips

  1. Reuse HTML templates - Pre-compile templates when possible
  2. Batch processing - Use htmlToPdfBatch() for multiple documents
  3. Optimize HTML - Remove unnecessary styles and scripts
  4. Set viewport explicitly - Helps with consistent layout rendering
  5. Use appropriate wait conditions - networkidle is safest but slower

Troubleshooting

"Cannot find module 'playwright'"

Ensure playwright is installed: npm install playwright

PDF has blank content

  • Check if HTML is valid
  • Verify images/resources are accessible
  • Try different waitUntil option: 'load' or 'domcontentloaded'
  • Increase timeout if needed

Memory issues with large batches

Process in smaller batches and use htmlToPdfBatch() for optimal resource management.

Styling not applied

  • Use inline styles or <style> tags
  • Avoid external CSS files
  • Use additionalStyles option for global CSS

Browser Support

  • ✅ Chromium (default, used by Playwright)
  • Node.js only (server-side PDF generation)

Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines.

License

MIT © Nur

Changelog

See CHANGELOG.md for version history and updates.

Support


Made with ❤️ by Nur Mohammad Akash