Moving PaperLab from a Go server into the browser
PaperLab 2 runs 37 of its 41 PDF tools on the visitor's device. What replaced Ghostscript, LibreOffice and the job queue, and the five-route Worker that's left.
On this page
Why the server went
PaperLab 1 was a Go server that wrapped Poppler, Ghostscript, qpdf, Tesseract and LibreOffice. It worked, but every feature meant uploading someone's document to a machine I ran. The open items in my job pipeline notes were all server problems: thirty Office conversions starting thirty LibreOffice processes, jobs lost on restart, and a URL check with a DNS-rebinding gap.
Version 2 deletes the server. 37 of the 41 tools now run in the browser, and the file never leaves the device. A static Astro site on Cloudflare serves the pages, and a small Worker handles what a browser can't do alone.
| Runs in | Tools | What leaves the device |
|---|---|---|
| The browser | 37 | Nothing |
| Workers AI | Summarise, translate, ask | Extracted text; for questions, six passages |
| Browser Run | HTML to PDF from a URL | The URL |
What replaced each engine
| Version 1 (server) | Version 2 (browser) |
|---|---|
| Ghostscript, qpdf CLI | pdf-lib for writing and encryption, qpdf compiled to WebAssembly for repair |
| Poppler | pdf.js for rendering and positioned text |
| Tesseract CLI | Tesseract.js |
| LibreOffice | An OOXML reader and a small typesetter on top of pdf-lib |
| Headless Chromium | Cloudflare Browser Run, called from the Worker |
LibreOffice was the hard one. Nothing in the browser converts a .docx to PDF, so the engine reads WordprocessingML itself: styles with inheritance, numbering, runs, tables with spans and shading, section page sizes. It turns them into blocks, and a 467-line flow typesetter paginates the blocks onto real PDF pages with selectable text. PowerPoint and Excel go through the same typesetter.
It's not LibreOffice. Floating shapes and text boxes are flattened into the text flow, and images in formats other than PNG and JPEG are skipped. The result reports how many were skipped instead of dropping them silently.
Two threads, one call
A PDF tool that blocks the main thread freezes the page. So the registry splits tools by what they need:
export const WORKER_TOOLS: Record<string, Loader> = {
merge: () => import("./tools/organize").then((m) => m.merge),
compress: () => import("./tools/images").then((m) => m.compress),
// 26 tools: pdf-lib, fflate, qpdf and OffscreenCanvas only
};
export const MAIN_TOOLS: Record<string, Loader> = {
ocr: () => import("./tools/recognize").then((m) => m.ocr),
// 14 tools: pdf.js rendering, tesseract.js, DOMParser or the network
};
26 tools run in a dedicated Web Worker. The 14 on the main thread still hand the heavy work to pdf.js's and Tesseract's own workers. Each tool is a dynamic import, so opening the merge page doesn't download OCR.
The page calls one function either way, with the same progress callback and an AbortSignal. On the Web Worker path, input bytes are copied and the copies transferred, so the page keeps its originals. Output buffers are transferred back, not copied.
The job queue's replacement for "recover from a panic" is this:
worker.onerror = (event) => {
const error = new ToolError(
"damaged",
`The tool stopped unexpectedly${event.message ? `: ${event.message}` : ""}. The file may be too large for this device.`,
);
for (const job of pending.values()) job.reject(error);
pending.clear();
worker?.terminate();
worker = null;
};
A crashed Web Worker, usually out of memory on a phone, fails every job it held with a plain message. The next run starts a fresh one.
Compression without Ghostscript
Ghostscript rewrote whole files. In the browser, compress finds each image, works out the size it's drawn at on the page, and re-encodes it with OffscreenCanvas:
| Level | Target resolution | JPEG quality |
|---|---|---|
| Light | 300 dpi | 0.85 |
| Balanced | 150 dpi | 0.75 |
| High | 110 dpi | 0.62 |
| Extreme | 72 dpi | 0.50 |
An image is only replaced when the new one is at least 5% smaller, and a codec failure keeps the original. Fonts and vector content aren't touched, so it does less than Ghostscript on text-heavy files.
The Worker that's left
The server shrank to five routes in Hono: /api/health, summarise, chat, translate and /api/render/pdf. Static assets are matched first and never invoke it.
Every request passes three checks. A POST must come from the site's own origin. The JSON body is read from the stream with a hard cap, so a lying Content-Length can't exhaust memory. And a Workers rate limiter allows 20 AI calls and 6 renders per IP per minute.
Asking questions keeps the document on the device too. The browser splits the text into passages of about 1,200 characters, ranks them with BM25 against the question, and sends only the top six to Workers AI. If the AI service is down, the page shows those passages instead, labelled as coming from the document. Summaries fall back to picking the most representative sentences locally.
The URL check changed character. Version 1 fetched pages from the server's own network, so a rebinding DNS answer could reach something private next to it. Now the Worker rejects private and local hostnames and Browser Run fetches the page from Cloudflare's network, where nothing of mine is reachable. The check doesn't resolve DNS, and a public name that points at 127.0.0.1 passes it. That matters much less when there's no internal network behind it.
What's still open
- Big files. The whole document is held in memory, so a file the old server handled can crash a phone tab. Fixing that means working page by page instead of loading the document at once.
- One third-party request. OCR downloads Tesseract language data from jsDelivr on first use. Serving it from the site is the next change.
- Translate has no fallback. Without Workers AI it reports itself unavailable.