Building a Zero-Upload Client-Side PDF Editor: Normalized Viewport Transforms, Text Overlays & Signature Flattening with pdf-lib
Most online PDF editors force users through a frustrating workflow: upload sensitive contracts, tax forms, or signed NDAs to a remote server, wait in a processing queue, and hit a paywall or watermark gate just to export the finished document.
At UCDREAMS, we eliminated the backend queue entirely when building Free PDF Editor Online — a 100% browser-based PDF editing, text annotation, image placement, and signature flattening utility that executes strictly in client RAM with zero file uploads and zero watermarks.
In this engineering breakdown, we walk through the exact coordinate math, Bézier signature vectorization, and pdf-lib serialization pipeline that keeps edits pixel-accurate across any page size or zoom scale.
1. The Coordinate Space Mismatch: DOM Pixels vs. PDF Points
HTML5 <canvas> and DOM overlays place (0, 0) at the top-left corner using CSS pixels scaled by devicePixelRatio and viewport zoom. PDF specifications (ISO 32000), however, define page coordinates in bottom-left points (72 pt = 1 inch) and can also carry an inherent page /Rotate matrix (0, 90, 180, 270).
To keep overlays rock-solid across zoomed previews and rotated scans, we store every annotation in normalized [0..1] page coordinates and transform at commit time:
export interface NormalizedRect {
pageIndex: number;
xNorm: number; // 0.0 (left) -> 1.0 (right)
yNorm: number; // 0.0 (top in UI) -> 1.0 (bottom in UI)
wNorm: number;
hNorm: number;
}
export function mapUiToPdfPoint(
normX: number,
normY: number,
pageWidthPt: number,
pageHeightPt: number,
rotationDeg: number
): { x: number; y: number } {
// Invert Y-axis from top-left UI space to bottom-left PDF space
const xBase = normX * pageWidthPt;
const yBase = (1 - normY) * pageHeightPt;
switch (((rotationDeg % 360) + 360) % 360) {
case 90:
return { x: (1 - normY) * pageWidthPt, y: (1 - normX) * pageHeightPt };
case 180:
return { x: (1 - normX) * pageWidthPt, y: normY * pageHeightPt };
case 270:
return { x: normY * pageWidthPt, y: normX * pageHeightPt };
default:
return { x: xBase, y: yBase };
}
}
Why Normalized Coordinates Matter
-
Zoom Independence: Users can zoom from
50%to250%on desktop or pinch-zoom on mobile Safari without drifting annotation anchors. - Rotation Safety: Rotated mobile scans render upright on screen and still commit text and signatures onto the exact visual region in the exported PDF.
2. Smooth Handwritten Signature Capture & Vector Flattening
Rasterizing handwritten signatures at low canvas resolution creates jagged edges when printed. Instead, we smooth pointer events with midpoint quadratic curves and bake high-DPI transparent strokes directly into the PDF page stream:
import { PDFDocument, rgb, StandardFonts } from 'pdf-lib';
export async function commitAnnotationsToPdf(
sourceBytes: Uint8Array,
textNotes: Array<{ rect: NormalizedRect; text: string; fontSizePt: number }>,
signaturePngBytes?: { rect: NormalizedRect; png: Uint8Array }
): Promise<Uint8Array> {
const pdfDoc = await PDFDocument.load(sourceBytes, { ignoreEncryption: false });
const font = await pdfDoc.embedFont(StandardFonts.Helvetica);
const pages = pdfDoc.getPages();
for (const note of textNotes) {
const page = pages[note.rect.pageIndex];
if (!page) continue;
const { width, height } = page.getSize();
const rotation = page.getRotation().angle;
const pt = mapUiToPdfPoint(note.rect.xNorm, note.rect.yNorm, width, height, rotation);
page.drawText(note.text, {
x: pt.x,
y: pt.y - note.fontSizePt,
size: note.fontSizePt,
font,
color: rgb(0.09, 0.11, 0.13),
});
}
if (signaturePngBytes) {
const page = pages[signaturePngBytes.rect.pageIndex];
if (page) {
const { width, height } = page.getSize();
const sigImg = await pdfDoc.embedPng(signaturePngBytes.png);
const boxW = signaturePngBytes.rect.wNorm * width;
const boxH = signaturePngBytes.rect.hNorm * height;
const x = signaturePngBytes.rect.xNorm * width;
const y = (1 - signaturePngBytes.rect.yNorm - signaturePngBytes.rect.hNorm) * height;
page.drawImage(sigImg, { x, y, width: boxW, height: boxH });
}
}
// Use object streams for compact output size without server round-trips
return pdfDoc.save({ useObjectStreams: true });
}
3. Memory-Safe Rendering for Multi-Page PDFs
Loading a 100-page architectural brief into 100 simultaneous <canvas> nodes would crash mobile browsers. Instead, we use an IntersectionObserver render window:
- Render only the active viewport page plus
±2adjacent buffer pages viaPDF.js. - Reuse offscreen
<canvas>bitmaps when scrolling fast so GPU memory remains flat (< 180 MBeven on iOS Safari). - Keep raw PDF bytes immutable in a
Uint8Arraybuffer until final export, allowing instant Undo/Redo across text, highlights, shapes, and page reordering.
4. Try the Live PDF & Document Suite (100% Free, Zero Login)
Explore the full client-side document workflow directly in your browser:
- Free PDF Editor Online — Edit text, annotate, add signatures & export clean PDFs with zero watermarks.
- PDF Merger Online Free — Combine and reorder PDF pages in Web Workers.
- PDF Unlocker Online Free — Remove known passwords from your PDFs locally.
-
DOCX Editor Online Free — Native browser
.docxWYSIWYG Word editor. - Document Converter Free — Convert DOCX, PDF, TXT, Markdown & HTML client-side.
- UCDREAMS Free Multiplayer Games — Take a break with Ludo, Paper War Stickman, 3D Table Tennis & 8 Ball Pool.
Need a Custom Document Workflow, Web App, or 95+ PageSpeed Website?
Built by UCDREAMS Senior Engineers. We engineer privacy-first browser utilities, Custom Web Applications (React, Next.js & Django), Custom Website Development, High-Speed WordPress Engineering, and AI Automation Systems.
Top comments (0)