GUIDES

Your first PDF

Go from a document definition to a saved PDF. Start with built-in Helvetica, then add browser downloads or Unicode fonts.

Save a receipt in Node.js or Bun#

Install the package, save the example as receipt.mjs, and run it. It uses ASCII text and built-in Helvetica, so no font files are needed.

Terminal
npm install minkpdf
receipt.mjs
import pdf from 'minkpdf';
import { writeFile } from 'node:fs/promises';

const receipt = pdf.createPdf({
  info: { title: 'Receipt' },
  content: [
    { text: 'Receipt', fontSize: 18, bold: true },
    'Payment received: USD 1,234.50'
  ]
});

await writeFile('receipt.pdf', await receipt.getBuffer());
Terminal
node receipt.mjs
# Or: bun receipt.mjs

You now have receipt.pdf in the current working directory. getBuffer() returns a Uint8Array that Node’s writeFile() accepts directly.

Download in a browser#

Add a download button, then include the JavaScript in your bundled browser application. The object URL is released after the download is started.

HTML
<button id="download-pdf" type="button">Download receipt</button>
JavaScript · browser
import pdf from 'minkpdf';

const button = document.querySelector('#download-pdf');
button.addEventListener('click', async () => {
  button.disabled = true;
  try {
    const receipt = pdf.createPdf({
      content: ['Receipt', 'Payment received: USD 1,234.50']
    });
    const url = URL.createObjectURL(await receipt.getBlob());
    const link = document.createElement('a');
    link.href = url;
    link.download = 'receipt.pdf';
    document.body.append(link);
    link.click();
    link.remove();
    setTimeout(() => URL.revokeObjectURL(url), 1000);
  } finally {
    button.disabled = false;
  }
});

Without a bundler, serve the built dist/ module tree over HTTP(S) and import its index.js. Preserve relative module paths. See installation without a bundler.

Add Unicode with an embedded font#

Helvetica supports ASCII. For names like José or currency symbols like ₱, register a TrueType font that contains the requested characters. Put Inter-Regular.ttf and Inter-Bold.ttf in a local fonts/ directory.

unicode-receipt.mjs
import pdf from 'minkpdf';
import { readFile, writeFile } from 'node:fs/promises';

const [regular, bold] = await Promise.all([
  readFile('./fonts/Inter-Regular.ttf'),
  readFile('./fonts/Inter-Bold.ttf')
]);

pdf.addVirtualFileSystem({
  'Inter-Regular.ttf': regular,
  'Inter-Bold.ttf': bold
});
pdf.addFonts({
  Inter: { normal: 'Inter-Regular.ttf', bold: 'Inter-Bold.ttf' }
});

const receipt = pdf.createPdf({
  defaultStyle: { font: 'Inter' },
  content: [{ text: 'Receipt', bold: true }, 'José García · ₱1,234.50']
});
await writeFile('unicode-receipt.pdf', await receipt.getBuffer());

Run node unicode-receipt.mjs or bun unicode-receipt.mjs. Font paths are relative to the working directory. Supply and license your own font files; the npm package does not include the repository’s Inter fixtures.

In a browser, fetch the font yourself and pass new Uint8Array(await response.arrayBuffer()) to addVirtualFileSystem(). Check response.ok before reading the response.

Build a more useful document#

Add tables with repeated headers, page numbers, or named styles. For smaller bundles, choose optional feature imports.

Search pages, examples, and API methods.