@wasmedia/wghostscript
v0.2.0
Published
Ghostscript compiled to WebAssembly for browsers
Downloads
603
Maintainers
Readme
wghostscript
Ghostscript compiled to WebAssembly for browsers, with broad PDF/PostScript
feature support and three API layers (gsapi_* C bindings, CLI, and a
high-level functional TypeScript API).
Try it in the browser: ghostscript.wasmedia.org, with PDF rendering, compression, PDF/A and PDF/X conversion, text extraction and PostScript → PDF, all client-side in a Web Worker.
This is the sibling of wimagemagick — the two projects deliberately do not overlap: wimagemagick excludes Ghostscript-based features (PDF rendering), and wghostscript covers them.
Install
npm install @wasmedia/wghostscriptimport { initWGhostscript } from "@wasmedia/wghostscript";
const gs = await initWGhostscript();
const pages = await gs.ops.renderPdf(pdfBytes, { dpi: 150 }); // Map of PNGs
const text = await gs.ops.extractText(pdfBytes);Full API: docs/api.md. Works in Node, in plain browser
ES modules and workers, and with bundlers; Vite's dev server, Vite
module workers and webpack need a line or two of config, see
Using a bundler.
Status
Pre-1.0 and working: Ghostscript (current stable, tracked automatically)
compiled to WASM, with the three API layers, a Web Worker entry, and a
test suite covering the py-pdf sample corpus and veraPDF-validated PDF/A
output. See briefs/INITIAL.md for the design brief.
Feature scope
Included
- PDF → image rendering (rasterizing PDF pages to PNG, JPEG, TIFF, etc.)
- PDF transformation (compression, flattening, PDF/A and PDF/X conversion, merging, splitting)
- PostScript interpretation (rendering
.psinput) - Font handling / text extraction
Excluded
- Printer / CUPS output devices (not meaningful in a browser)
- X11 / GTK display devices
- Network / HTTP fetching from inside Ghostscript (security)
- Tesseract OCR integration (compose with a separate WASM OCR build instead)
- Multi-threading (single-threaded only — no
SharedArrayBuffer/ COOP/COEP requirement)
Technical requirements
- Ghostscript version: Always target the current stable release. CI
automation polls the official Artifex mirror
(
ArtifexSoftware/ghostpdl-downloads) GitHub Releases API and triggers a build when a new stable tag appears. - Scope: Ghostscript only (PostScript + PDF interpreter), not full GhostPDL (no PCL/XPS).
- API surface (three layers):
- Low-level
gsapi_*C bindings viacwrap/ccall. - CLI-style interface via
callMain()(["-sDEVICE=png16m", "-o", "out.png", "input.pdf"]). - High-level functional TypeScript API:
renderPdf,compressPdf,extractText,psToPdf,convertPdf, etc.
- Low-level
- Toolchain: Emscripten inside Docker.
- Output: Single
.wasm+ JS glue bundle, plus an npm-distributable package (@wasmedia/wghostscript). - File I/O: Both Emscripten MEMFS and direct
ArrayBuffer/Blobpassing. - Web Workers: Supported (and recommended for non-trivial work).
- Browser support: Chrome and Safari at minimum.
- Build: Single-threaded + SIMD — works on any site without special headers.
- Fonts: URW base 35 fonts are compiled into the WASM (Ghostscript's
%rom%file system), so standard fonts work offline with no setup.
Delegate libraries
The libraries Ghostscript bundles in its source tree, compiled in with it:
- libjpeg (IJG) — JPEG output and DCT streams in PDFs
- libpng — PNG output
- libtiff — TIFF output
- zlib — Flate streams in PDFs
- OpenJPEG — JPEG 2000 streams in PDFs
- jbig2dec — JBIG2 streams in PDFs
- lcms2mt — colour management / ICC profiles
- FreeType — font rendering
libidn, libpaper, IJS, CUPS and Tesseract are turned off
(native/scripts/build-ghostscript.sh).
Architecture
Two layers, mirroring wimagemagick:
native/— C/WASM compilation. Cross-compiles Ghostscript and delegate libraries to WASM via Emscripten inside Docker. Output:dist/wghostscript.{js,wasm}.src/— TypeScript wrapper:loader.ts—initWGhostscript(), loads the WASM moduleapi.ts—gsapi_*bindings viacwrap/ccallcli.ts— CLI-styleexecute()viacallMain()operations.ts— high-level functional API (renderPdf,compressPdf,extractText,psToPdf,convertPdf)filesystem.ts— MEMFS + ArrayBuffer/Blob helpersworker.ts— Web Worker entry pointtypes.ts— shared typesindex.ts— public re-exports
Prior art
| Project | Status | Notes |
|---------|--------|-------|
| @okathira/ghostpdl-wasm | Active (v1.1.0, Nov 2025) | Most credible reference build; minimal delegate set (BMP/JPEG/PNG/PS/TIFF only); CLI-via-callMain |
| @jspawn/ghostscript-wasm | Stale (last release 2022) | Basic npm package |
| ochachacha/ps-wasm | Active | Browser-extension-style; PostScript viewing focus |
| laurentmmeyer/ghostscript-pdf-compress.wasm | Demo | PDF compression demo (Web Worker example) |
Gaps wghostscript fills:
- Three API layers (
gsapi_*+ CLI + high-level functional TS API). Existing options stop at CLI-via-callMain. - Maximum practical delegate set with color management and JPEG2000.
- Documented SIMD build.
- TypeScript-first wrapper layer.
Build & test
See docs/building.md once it lands. Short version:
npm run build # native WASM (Docker) + TypeScript
npm run build:native # WASM only
npm run build:ts # TypeScript only
npm test
npm run test:watch
npm run test:browser # headless Chrome end-to-end test of the playground
npm run playground # local dev server hosting examples/browserLicense
AGPL-3.0-or-later, matching upstream Ghostscript. Downstream users accept the copyleft obligations: any application using this package over a network must release its source under AGPL.
