pdfmake

0.3.7 · active · verified Sun Apr 19

pdfmake is a JavaScript library for generating PDF documents on both client and server sides. It is currently at version 0.3.7 and maintains an active release cadence, with multiple minor updates released recently addressing features, fixes, and security patches. Key features include advanced text layout with line-wrapping and alignments, support for bulleted and numbered lists, comprehensive table layouts with features like col-spans, row-spans, and repeating headers, and the new snaking columns for newspaper-style content. It also handles images, vector graphics, and provides a powerful styling system with inheritance. The library supports dynamic page headers/footers, background layers, custom page breaks, and outlines/bookmarks for generated PDFs. Differentiating itself through its pure JavaScript implementation, pdfmake provides a unified, promise-based interface for both Node.js and browser environments, simplifying PDF generation workflows across different platforms.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to generate a PDF file using pdfmake in a Node.js environment, showcasing basic text, lists, tables, styles, and the promise-based API. It also includes the crucial step of assigning font definitions and setting a URL access policy.

import * as pdfMake from 'pdfmake/build/pdfmake';
import * as pdfFonts from 'pdfmake/build/vfs_fonts';
import * as fs from 'fs'; // For Node.js file system operations

// Assign fonts (crucial for pdfmake to work)
pdfMake.vfs = pdfFonts.pdfMake.vfs;

// Define an optional URL access policy for external images/resources
// This is critical since v0.3.6 (CVE-2026-26801 fix)
pdfMake.setUrlAccessPolicy((url) => {
  // Only allow access to URLs from example.com
  return url.startsWith("https://example.com/");
});

const docDefinition = {
  content: [
    { text: 'Hello, pdfmake!', style: 'header' },
    'This is an example PDF generated using pdfmake version 0.3.7.',
    { text: 'Features showcased:', margin: [0, 15, 0, 5] },
    {
      ul: [
        'Structured content with text and lists.',
        'Basic styling with custom font sizes and bold text.',
        'Uses the modern promise-based API for generation.'
      ]
    },
    { text: 'A small table:', style: 'subheader', margin: [0, 15, 0, 5] },
    {
      table: {
        headerRows: 1,
        widths: ['*', 'auto', 100],
        body: [
          ['Column 1', 'Column 2', 'Column 3'],
          ['One value', 'Another value', 'Some more text'],
          [{ text: 'Complex cell with span', colSpan: 2 }, {}, 'Last cell']
        ]
      }
    }
  ],
  styles: {
    header: { fontSize: 22, bold: true, alignment: 'center', margin: [0, 0, 0, 20] },
    subheader: { fontSize: 16, bold: true }
  },
  defaultStyle: {
    font: 'Roboto' // Ensure a font is available, Roboto is default via vfs_fonts
  }
};

async function generatePdf() {
  try {
    const pdfDoc = pdfMake.createPdf(docDefinition);
    const buffer = await pdfDoc.getBuffer();
    fs.writeFileSync('output.pdf', buffer);
    console.log('PDF generated successfully: output.pdf');
  } catch (error) {
    console.error('Error generating PDF:', error);
  }
}

generatePdf();

view raw JSON →