Skip to content

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.
  • 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 cleanupSymbol.dispose support (using keyword).
  • Typed exceptions — Error subclasses with error codes for precise handling.
  • Universal — Works in Node.js 22+ and modern browsers.
FeatureDescription
Render pagesConvert PDF pages to RGBA pixel data
Extract textGet text content with position information
Search textFind text with bounding rectangles
Read annotationsAccess all 28 annotation types
Read bookmarksNavigate document outlines
Extract attachmentsGet embedded files
Read structureAccess tagged PDF structure trees
Create documentsBuild PDFs from scratch
Save documentsSave with various options
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);
}
Terminal window
pnpm add @scaryterry/pdfium

See Installation for bundler configuration.

PlatformStatusNotes
Node.js 22+✅ Full supportWASM loads automatically
Modern browsers✅ Full supportRequires WASM URL/binary
React/Vite/Next.js✅ Full supportSee bundler config
Web Workers✅ Full supportFor off-main-thread processing

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 using keyword

Convert PDF pages to images for:

  • Thumbnail generation
  • Document preview
  • Print preparation
  • Image extraction

Extract and search text for:

  • Full-text indexing
  • Content analysis
  • Data extraction
  • Accessibility

Inspect PDF structure for:

  • Bookmark extraction
  • Attachment handling
  • Annotation processing
  • Structure tree navigation

Create documents for:

  • Report generation
  • Invoice creation
  • Certificate production
  • Form generation