@dicepdf/core
v0.1.0
Published
Portable, privacy-first PDF engine. The same operations that power dicepdf.com — runnable in Node, the browser, and AI agents. Your files never leave the machine.
Maintainers
Readme
@dicepdf/core
The portable PDF engine behind dicepdf.com. The same operations that power the website, packaged as pure functions you can call from Node, the browser, a CLI, or an AI agent.
One promise: your files never leave the machine. Every function reads bytes you already hold in memory and returns new bytes. There is no network path, no upload, no telemetry.
Install
npm install @dicepdf/coreUsage
import { readFile, writeFile } from 'node:fs/promises';
import { merge, splitByRanges } from '@dicepdf/core';
import { extractText } from '@dicepdf/core/extract';
import { renderToImages } from '@dicepdf/core/render';
// Merge
const a = await readFile('a.pdf');
const b = await readFile('b.pdf');
await writeFile('merged.pdf', await merge([a, b]));
// Extract text (great for feeding an LLM/agent)
const text = await extractText(await readFile('report.pdf'));
// Render pages to PNGs
const images = await renderToImages(await readFile('report.pdf'), { dpi: 150 });
for (const img of images) await writeFile(img.name, img.bytes);
// Split
const parts = await splitByRanges(await readFile('book.pdf'), [
[1, 10],
[11, 20],
]);
for (const part of parts) await writeFile(part.name, part.bytes);Every operation accepts a Uint8Array or ArrayBuffer and (for PDF
outputs) returns a Uint8Array.
Three entry points
| Import | Runs in | Pulls in |
|--------|---------|----------|
| @dicepdf/core | browser + Node | pdf-lib, fflate only — light & bundler-safe |
| @dicepdf/core/extract | Node | pdfjs-dist (layout-aware text extraction) |
| @dicepdf/core/render | Node | @napi-rs/canvas (optional) for rasterization |
The main entry has no node: imports, so it bundles cleanly into a browser
or web-worker build. render needs the optional @napi-rs/canvas
dependency (prebuilt — no native toolchain required).
API
| Function | Description |
|----------|-------------|
| merge(files) | Concatenate PDFs in order |
| splitByRanges(file, ranges) / splitPages(file, pages) / splitEveryPage(file) | Split into multiple PDFs |
| rotate(file, angle, pageIndices?) | Rotate 90/180/270° |
| deletePages(file, pages) | Remove 1-based pages |
| reorder(file, order) | Reorder via a page permutation |
| crop(file, marginsMm) | Trim margins (millimeters) |
| watermark(file, options) | Stamp a text watermark |
| addPageNumbers(file, options) | Stamp page numbers |
| stripMetadata(file, options?) | Remove Info/XMP/ID metadata |
| compress(file) | Structural compression + metadata strip |
| repair(file) | Best-effort reparse + rewrite |
| imageToPdf(images, options?) | PNG/JPEG → PDF (one image per page) |
| zip(entries) | Bundle named byte arrays into a ZIP |
| getMetadata(file) | Page count, AcroForm, signature flag |
| extractDocument / extractText / extractMarkdown | Layout-aware text extraction (/extract) |
| renderToImages(file, options?) | Rasterize pages to PNG/JPEG (/render) |
| compressAggressive(file, options?) | Rasterizing compression for scan-heavy PDFs (/render) |
Errors are typed: catch EncryptedPdfError and InvalidPdfError to branch
on bad input.
License
MIT
