~/vimal-jain
MicroserviceMay 2024

HTML to PDF Generation Microservice

Stateless Node.js microservice converting dynamic HTML, MathJax/LaTeX mathematical formulas, dynamic charts, and large documents into publication-grade PDFs.

Problem Statement

Generating high-fidelity PDF reports containing dynamic SVG charts, tabular data, and complex mathematical notation (KaTeX / MathJax) in high-volume environments frequently destabilizes standard application servers due to headless browser memory leaks, zombie processes, and CPU spikes. This project delivered a dedicated, isolated, and scalable document-generation microservice engineered for high throughput and bulletproof process stability.

System Architecture

The microservice is built on Node.js and Express.js, backed by headless Chromium controlled via Puppeteer. Asynchronous conversion jobs are managed through BullMQ queues running over Redis to isolate heavy document rendering from primary API response cycles. Finished PDF artifacts are automatically streamed to AWS S3 storage with expiring signed URLs returned to clients.

Client Application (HTTP Request)
  │
  ▼
Express.js REST Microservice
  ├── Payload Validation & Security Sanitization
  └── Job Enqueueing
  │
  ▼
BullMQ Task Queue (Redis Backed)
  │
  ▼
Worker Process Pool
  │
  ├── Browser Instance Pool (Lifecycle Manager)
  │     │
  │     ▼
  │   Puppeteer / Headless Chromium
  │     ├── HTML/CSS Rendering
  │     ├── KaTeX / LaTeX Formula Evaluation
  │     ├── Chart & SVG Render Synchronization
  │     └── PDF Buffer Generation
  │
  ▼
AWS S3 Object Storage
  │
  ▼
Expiring Pre-Signed Download URL Response

Technical Challenges & Reliability Engineering

•Browser Process Leaks & Crashes: Addressed severe Chromium memory consumption and zombie process proliferation by building a strict browser lifecycle manager. Implemented browser instance recycling after a configured number of jobs, explicit process teardown hooks on errors, and health checks to detect hung pages.
•Mathematical Notation Rendering: Ensured KaTeX and MathJax formulas fully evaluate before triggering PDF rasterization by combining DOM readyState listeners with explicit formula render guards.
•Concurrency Throttling: Utilized BullMQ queue concurrency controls to prevent server exhaustion under sudden bursts of report-generation requests.
•Cloud Storage Streaming: Streamed generated PDF binary buffers directly to AWS S3 without writing temporary files to local disk, maximizing container statelessness and horizontal scalability.

Stack

Node.jsExpress.jsPuppeteerChromiumBullMQRedisAWS S3KaTeXLaTeXDocker