Quick Start
This guide walks you through rendering a PDF to an image in just a few steps.
Prerequisites
Section titled “Prerequisites”- Node.js 22+ or modern browser
- TypeScript (recommended) or JavaScript
1. Install the Package
Section titled “1. Install the Package”pnpm add @scaryterry/pdfium2. Create a Script
Section titled “2. Create a Script”import { PDFium } from '@scaryterry/pdfium';import { promises as fs } from 'fs';import sharp from 'sharp';
async function main() { // Read PDF file const pdfData = await fs.readFile('document.pdf');
// Initialise the library using pdfium = await PDFium.init();
// Open the document using document = await pdfium.openDocument(pdfData, { onProgress: (progress) => console.log(`Loading: ${Math.round(progress * 100)}%`), });
console.log(`Document has ${document.pageCount} pages`);
// Render the first page using page = document.getPage(0);
const { data, width, height } = page.render({ scale: 2 });
// Convert to PNG using sharp const png = await sharp(data, { raw: { width, height, channels: 4 }, }).png().toBuffer();
// Save the image await fs.writeFile('page-1.png', png);
console.log(`Rendered page 1 as ${width}x${height} PNG`);}
main();3. Run It
Section titled “3. Run It”npx tsx render-pdf.tsWhat’s Happening?
Section titled “What’s Happening?”PDFium.init()— Loads the WASM binary and initialises the libraryopenDocument()— Parses the PDF and returns a document objectgetPage(0)— Loads the first page (zero-indexed)render({ scale: 2 })— Renders at 2x scale (144 DPI) to RGBA pixelssharp— Converts raw pixels to PNG format
The using Keyword
Section titled “The using Keyword”The using keyword ensures resources are automatically cleaned up:
using pdfium = await PDFium.init();// pdfium.dispose() is called automatically when the scope endsThis is ES2024 explicit resource management. Without it:
const pdfium = await PDFium.init();try { // Use pdfium...} finally { pdfium.dispose(); // Must manually dispose}Render All Pages
Section titled “Render All Pages”for (const page of document.pages()) { using p = page;
const { data, width, height } = p.render({ scale: 2 });
const png = await sharp(data, { raw: { width, height, channels: 4 }, }).png().toBuffer();
await fs.writeFile(`page-${p.index + 1}.png`, png);}Extract Text
Section titled “Extract Text”using page = document.getPage(0);const text = page.getText();console.log(text);Handle Password-Protected PDFs
Section titled “Handle Password-Protected PDFs”using document = await pdfium.openDocument(pdfData, { password: 'secret123',});Browser Usage
Section titled “Browser Usage”// Load WASM firstconst wasmResponse = await fetch('/pdfium.wasm');const wasmBinary = await wasmResponse.arrayBuffer();
using pdfium = await PDFium.init({ wasmBinary });
// Load PDF from file input or fetchconst pdfData = await file.arrayBuffer();using document = await pdfium.openDocument(pdfData);Next Steps
Section titled “Next Steps”- Render PDF Guide — Detailed rendering options
- Extract Text Guide — Text extraction patterns
- API Reference — Complete API documentation
- Resource Management — Understanding disposal
Common Issues
Section titled “Common Issues””Cannot find module” Error
Section titled “”Cannot find module” Error”Ensure the package is installed:
pnpm add @scaryterry/pdfiumWASM Loading Fails (Browser)
Section titled “WASM Loading Fails (Browser)”Make sure the WASM file is served with correct MIME type:
Content-Type: application/wasmOut of Memory
Section titled “Out of Memory”Reduce scale or process pages sequentially:
// Lower scale uses less memoryconst result = page.render({ scale: 1 });
// Process pages one at a timefor (const page of document.pages()) { using p = page; // Process and save immediately}