Introduction
@scaryterry/pdfium is a TypeScript/JavaScript wrapper for the PDFium library, compiled to WebAssembly for use in Node.js and modern browsers.
- PDFium — An open-source library for PDF manipulation and rendering, developed by Google and used in Google Chrome.
- @scaryterry/pdfium — A modern TypeScript wrapper with type safety, automatic resource management, and structured error handling.
Features
Section titled “Features”- Zero dependencies — PDFium is compiled to WebAssembly and bundled with the package.
- Type-safe — Full TypeScript support with branded types and strict mode.
- Automatic resource cleanup —
Symbol.disposesupport (usingkeyword). - Typed exceptions — Error subclasses with error codes for precise handling.
- Universal — Works in Node.js 22+ and modern browsers.
Capabilities
Section titled “Capabilities”| Feature | Description |
|---|---|
| Render pages | Convert PDF pages to RGBA pixel data |
| Extract text | Get text content with position information |
| Search text | Find text with bounding rectangles |
| Read annotations | Access all 28 annotation types |
| Read bookmarks | Navigate document outlines |
| Extract attachments | Get embedded files |
| Read structure | Access tagged PDF structure trees |
| Create documents | Build PDFs from scratch |
| Save documents | Save with various options |
Quick Example
Section titled “Quick Example”import { PDFium } from '@scaryterry/pdfium';import { promises as fs } from 'fs';import sharp from 'sharp';
async function renderFirstPage() { const pdfData = await fs.readFile('document.pdf');
using pdfium = await PDFium.init(); using document = await pdfium.openDocument(pdfData); using page = document.getPage(0);
const { data, width, height } = page.render({ scale: 2 });
const png = await sharp(data, { raw: { width, height, channels: 4 }, }).png().toBuffer();
await fs.writeFile('page-1.png', png);}Installation
Section titled “Installation”pnpm add @scaryterry/pdfiumSee Installation for bundler configuration.
Platform Support
Section titled “Platform Support”| Platform | Status | Notes |
|---|---|---|
| Node.js 22+ | ✅ Full support | WASM loads automatically |
| Modern browsers | ✅ Full support | Requires WASM URL/binary |
| React/Vite/Next.js | ✅ Full support | See bundler config |
| Web Workers | ✅ Full support | For off-main-thread processing |
TypeScript Support
Section titled “TypeScript Support”The library is written in TypeScript with strict mode:
- Branded handle types prevent mixing document/page handles
- Typed error classes with specific error codes
- Full interface definitions for all options and results
- Support for ES2024
usingkeyword
Use Cases
Section titled “Use Cases”PDF Rendering
Section titled “PDF Rendering”Convert PDF pages to images for:
- Thumbnail generation
- Document preview
- Print preparation
- Image extraction
Text Processing
Section titled “Text Processing”Extract and search text for:
- Full-text indexing
- Content analysis
- Data extraction
- Accessibility
Document Analysis
Section titled “Document Analysis”Inspect PDF structure for:
- Bookmark extraction
- Attachment handling
- Annotation processing
- Structure tree navigation
PDF Generation
Section titled “PDF Generation”Create documents for:
- Report generation
- Invoice creation
- Certificate production
- Form generation
Next Steps
Section titled “Next Steps”- Quick Start — 5-minute tutorial
- Installation — Setup and configuration
- Render PDF — Rendering guide
- API Reference — Complete API docs