Most web-based PDF editors force a frustrating trade-off: you upload sensitive documents (contracts, W-2s, bank statements, NDA drafts) to a third-party server just to fix a single typo.
When I added an Edit Text in PDF tool to PDFTransform, my main constraint was strict: zero bytes leave the user's browser. Every page render, coordinate calculation, whiteout mask, and vector text export has to run locally in browser memory.
Here is how the coordinate math and rendering pipeline actually work under the hood using pdfjs-dist and pdf-lib.
Why PDF Text Is Tricky to "Edit"
Unlike HTML or .docx, PDFs do not store reflowable paragraphs or <input> boxes. Text in a PDF is painted as positioned vector glyphs (or bitmap scans) at explicit coordinates inside a page stream.
To support reliable browser-side editing without mangling complex layouts, modern browser editors use a three-part overlay pipeline:
- Render the PDF page cleanly onto an HTML
<canvas>usingpdfjs-dist. - Place & drag annotations (new text, whiteout masks, or whiteout-and-replace boxes) in canvas pixel space (
top-leftorigin). - Transform & embed each edit back into native PDF coordinate space (
bottom-leftorigin) usingpdf-lib.
Step 1: Responsive Preview Rendering with pdfjs-dist
First, we load the selected PDF File directly from an ArrayBuffer (no FormData, no fetch upload) and cap preview width to ~700px so it stays sharp on desktop and mobile viewports:
const buffer = await pdfFile.arrayBuffer(); const loadingTask = pdfjsLib.getDocument({ data: new Uint8Array(buffer) }); const pdf = await loadingTask.promise; const page = await pdf.getPage(targetPageNum); const standardViewport = page.getViewport({ scale: 1.0 }); let renderScale = 1.25; if (standardViewport.width > 700) { renderScale = 700 / standardViewport.width; } const viewport = page.getViewport({ scale: renderScale }); canvas.width = viewport.width; canvas.height = viewport.height; await page.render({ canvas: canvas as any, canvasContext: ctx, viewport, }).promise; We store viewportDims (width and height in canvas pixels) alongside the user's annotations so we can translate screen coordinates accurately regardless of the PDF's native page size (A4, US Letter, or custom landscape sheets).
Step 2: Converting Canvas Coordinates (top-left) to PDF Coordinates (bottom-left)
This is the most common source of off-by-one or upside-down bugs when mixing HTML canvas coordinates with pdf-lib:
- Browser Canvas:
(0, 0)is top-left, andyincreases downward. - PDF Standard (
pdf-lib):(0, 0)is bottom-left, andyincreases upward.
For each page during export:
const { width: nativeWidth, height: nativeHeight } = page.getSize(); const renderedW = viewportDims.width || nativeWidth; const renderedH = viewportDims.height || nativeHeight; const scaleX = nativeWidth / renderedW; const scaleY = nativeHeight / renderedH; const itemW = item.width * scaleX; const itemH = item.height * scaleY; const itemX = item.x * scaleX; // Flip Y axis from top-left canvas space to bottom-left PDF space const itemY = nativeHeight - (item.y * scaleY) - itemH; If we're running in Whiteout & Replace or Whiteout Erase mode, we first paint an opaque vector mask rectangle over the old typo:
if (item.mode === 'whiteout' || item.mode === 'whiteout-replace') { page.drawRectangle({ x: itemX, y: itemY, width: itemW, height: itemH, color: hexToPdfRgb(item.backgroundColor || '#ffffff'), }); } Step 3: Baseline Alignment for Multi-Line Vector Text
Drawing text at itemY alone places the baseline slightly off compared to what the user saw on the HTML overlay. Accounting for font ascender height (~0.85 * pdfFontSize) keeps visual alignment tight between preview and exported PDF:
const pdfFontSize = item.fontSize * scaleY; const lines = item.text.split('\n'); const lineHeight = pdfFontSize * 1.2; lines.forEach((line, lineIdx) => { if (!line) return; const paddingX = item.mode === 'whiteout-replace' ? 4 * scaleX : 0; const lineX = itemX + paddingX; const lineY = item.mode === 'whiteout-replace' ? itemY + itemH - pdfFontSize * 0.85 - lineIdx * lineHeight - 3 * scaleY : nativeHeight - item.y * scaleY - pdfFontSize * 0.85 - lineIdx * lineHeight; page.drawText(line, { x: lineX, y: lineY, size: pdfFontSize, font: selectedFont, color: hexToPdfRgb(item.color || '#000000'), }); }); Finally, const finalBytes = await pdfDoc.save() serializes a clean vector PDF right in memory and triggers a local download — crisp, searchable text with zero server storage.
Try It Live (Zero Uploads, Free)
You can test the full workflow — adding text, fixing typos with Whiteout & Replace, and erasing sensitive lines — right here:
👉 Edit Text in PDF (PDFTransform)
And if you work with production JSON, JWTs, or SQL payloads and want the same zero-telemetry guarantee for developer utilities, check out our companion Web Worker toolkit:
👉 DevTransform — 46+ Air-Gapped Dev Utilities
What edge cases have you hit when manipulating PDFs in JavaScript (pdf-lib, pdfjs-dist, rotated pages, or custom embedded fonts)? Drop a comment below!