I recently built a PDF editor that runs entirely in the browser: you open a PDF, add text, shapes, highlights and images, and download the result. No server touches the file. Along the way I hit a handful of problems that cost me time, so here they are with the fixes.
Disclosure: I run Signplify, and the editor is one of its free tools. The code snippets below are simplified from what I actually shipped.
The architecture in one paragraph
- pdf.js renders each page to a canvas for display.
- A transparent overlay on top of the canvas holds the annotations (text, shapes, images). Their coordinates are stored in PDF points, not screen pixels.
- pdf-lib loads the original bytes, draws the annotations onto the real pages and saves a new file.
Keeping the overlay in points means zooming never changes the data, only how it is drawn.
Lesson 1: PDF coordinates start at the bottom-left, and the page box may not start at zero
Browsers measure from the top-left. PDFs measure from the bottom-left. If you forget the flip, everything lands upside down. A second trap is that a page’s visible area is its crop box, which can have a non-zero origin.
const cb = page.getCropBox(); // pdf-lib const X = x => cb.x + x; const Y = y => cb.y + cb.height - y; // flip the y axis // a rectangle that the user drew with its top-left at (a.x, a.y) page.drawRectangle({ x: X(a.x), y: Y(a.y + a.h), // pdf-lib draws from the bottom-left corner width: a.w, height: a.h, }); I store everything with a top-left origin in the overlay and convert only when exporting.
Lesson 2: render sharp on retina screens without breaking your coordinates
If you render the canvas at CSS size it looks blurry on high-DPI screens. Render at zoom * devicePixelRatio, then size it back with CSS. For the overlay, use an SVG whose viewBox is the page size in points.
const v1 = page.getViewport({ scale: 1 }); const dpr = window.devicePixelRatio || 1; const v = page.getViewport({ scale: zoom * dpr }); canvas.width = Math.floor(v.width); canvas.height = Math.floor(v.height); canvas.style.width = v1.width * zoom + 'px'; svg.setAttribute('viewBox', `0 0 ${v1.width} ${v1.height}`); The SVG scales automatically, so a shape at (100, 100) in points stays at (100, 100) whatever the zoom.
Lesson 3: text baselines are not where you think
In the browser, a text box is positioned by its top edge. In a PDF, drawText positions the baseline. If you place the PDF text at the same y as the box, it ends up too high or too low.
With CSS line-height: 1.2 and an Arial-like font (ascent about 0.905, descent about 0.212), the first baseline sits at roughly 0.9465 times the font size below the top of the box:
const baseline = a.y + a.size * 0.9465 + lineIndex * a.size * 1.2; page.drawText(line, { x: X(a.x), y: Y(baseline), size: a.size, font }); I checked this by exporting the PDF and rendering it with pdftoppm, then comparing it with what the editor showed.
Lesson 4: a full re-render can destroy the element the user is typing in
My first version rebuilt the overlay DOM every time something changed. Clicking with the Text tool created a text box and focused it, but then the same click event bubbled to a page-level “click on empty space deselects” handler. That handler called the render function, which replaced the focused element, so the box lost focus and the empty annotation was deleted.
The fix was small once I saw it: ignore events whose target is the capture layer.
page.addEventListener('pointerdown', e => { if (tool === 'select' && e.target.id !== 'cap' && !e.target.closest('.ann, .txt')) { select(null); } }); The general lesson is that re-rendering and focus do not mix. Either update elements in place while editing, or make sure nothing triggers a re-render between creating an editable element and focusing it.
Lesson 5: undo and redo with plain JSON snapshots
For a small editor, you do not need a command pattern. Keep the annotations in one array, and push a JSON snapshot before every change.
function snap() { undoStack.push(JSON.stringify(anns)); redoStack = []; } function undo() { if (!undoStack.length) return; redoStack.push(JSON.stringify(anns)); anns = JSON.parse(undoStack.pop()); } One caveat: store images in a separate map and reference them by key, otherwise every snapshot contains the full image data.
Lesson 6: standard fonts only cover WinAnsi
StandardFonts.Helvetica in pdf-lib cannot encode characters outside WinAnsi. Passing something like an Arabic letter or an emoji to drawText throws an error. I sanitize the text before exporting: map curly quotes and dashes to plain equivalents, replace anything else unsupported with a question mark, and tell the user how many characters were replaced. To support other scripts you need to embed a font that contains those glyphs.
Lesson 7: rotated pages need their own handling
pdf.js applies a page’s /Rotate value when it renders, but your pdf-lib coordinates assume an unrotated page. Edits then land in the wrong place. I detect page.rotate and show a warning, and I suggest rotating the file to zero first. Doing the full transform is possible, but I decided it was not worth the complexity for a first version.
Testing without clicking around
Two things made testing painless. First, a headless Chromium script (Puppeteer) that opens the editor, uploads a sample PDF, draws shapes with simulated mouse events, exports, and checks the annotation data. Second, rendering the exported PDF with pdftoppm and looking at it, which caught the baseline and coordinate bugs immediately.
Checking that nothing is uploaded
Because the whole point was privacy, you can verify it yourself. Open DevTools, go to the Network tab, and edit a PDF. If no request carries the file, it stayed on the device.
What it cannot do
- It adds content on top of a PDF. It does not rewrite the original text, which would require parsing and re-laying out the content streams.
- A white box hides text visually but does not remove it. For sensitive data you need real redaction.
- Added text uses a standard font, so it will not always match the original typography.
If you want to try it, the editor is at signplify.com/tools/pdf-editor, and there are related tools for annotating, filling and signing and redacting PDFs. I would be glad to hear which edge cases break it for you, especially odd PDFs.