How we made PDF contracts resistant to scraping
24 May 2024 · 6 min read · SmartCG
Drawing invoices and acceptance acts on the fly with PDFKit, and the two problems nobody warns you about: Cyrillic fonts and signature protection.
The task
Our B2B portal had to generate an invoice and a contract automatically when an order is placed. There was also a security requirement: the director's stamp and signature must not be easy to lift out of the PDF as plain images.
Choosing the tool: PDFKit
We dropped HTML-to-PDF conversion (Puppeteer, wkhtmltopdf) because it is slow and eats a lot of RAM on the server. We chose pdfkit — a low-level library that draws a vector PDF directly, without a browser in the middle.
Protecting the graphics
To make signature scraping harder, we draw the stamp and signature PNGs on the bottom layer as a background and put the document text on top. That removes the easy "right click, save image" path. An invisible watermark can be added on top of that as an extra layer of noise.
Cyrillic
By default PDFKit uses standard fonts with no Cyrillic support, and documents came out with blank characters. We baked Roboto TTF files into the Docker image and registered them programmatically before generating a document — no dependency on fonts installed on the host.
What this approach gives the business
Documents are generated in milliseconds, no headless browser has to be kept alive, and the layout is deterministic across environments. For a portal that issues hundreds of invoices a day, that is the difference between a background job and a server that constantly runs out of memory.
Services involved
Read next
React and Next.js performance: how we made a site three times faster
The fight for Core Web Vitals: server components, dynamic imports and removing unnecessary re-renders.
DevOpsDocker multi-stage builds: cutting images by 90%
Getting rid of ENOSPC alerts and making deploys fast again with a proper Dockerfile.
BackendPostgreSQL vs MongoDB: choosing a database for a B2B product
Relational against NoSQL, decided on real scenarios instead of fashion.
Need document generation that survives production volume?
We design the document pipeline — fonts, templates, storage, signatures — so invoices and contracts are generated reliably instead of becoming a nightly incident.