<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Anand Kumar on Medium]]></title>
        <description><![CDATA[Stories by Anand Kumar on Medium]]></description>
        <link>https://medium.com/@hello.anand007?source=rss-6e80848c73b8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*T8rLVVJPD5BLN4ggGWubwQ.jpeg</url>
            <title>Stories by Anand Kumar on Medium</title>
            <link>https://medium.com/@hello.anand007?source=rss-6e80848c73b8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 09:54:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hello.anand007/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[# Rendering What You See: Building a Serverless PDF Pipeline That Never Lies to the User]]></title>
            <link>https://medium.com/@hello.anand007/rendering-what-you-see-building-a-serverless-pdf-pipeline-that-never-lies-to-the-user-8ad812a4ee85?source=rss-6e80848c73b8------2</link>
            <guid isPermaLink="false">https://medium.com/p/8ad812a4ee85</guid>
            <category><![CDATA[pdf-generation]]></category>
            <category><![CDATA[serverless-architecture]]></category>
            <category><![CDATA[quill-editor]]></category>
            <dc:creator><![CDATA[Anand Kumar]]></dc:creator>
            <pubDate>Fri, 06 Nov 2026 03:58:19 GMT</pubDate>
            <atom:updated>2026-11-06T04:11:33.572Z</atom:updated>
            <content:encoded><![CDATA[<p>*A reflection on an architecture problem I got to solve: turning rich, structured document content into</p><p>pixel-faithful PDFs, at scale, entirely on serverless infrastructure.*</p><p><strong>## The problem nobody warns you about</strong></p><p>If you’ve ever built a “what you see is what you get” document editor, you already know the trap: the</p><p>editor and the exported file are, technically, two completely different rendering engines. One is your</p><p>web app’s rich-text component; the other is whatever PDF library you bolted on afterward. They <em>*try*</em> to</p><p>agree on fonts, spacing, table borders, page breaks — and they drift. Someone adds a new table style to the</p><p>editor, and three sprints later a customer opens a PDF where that table is subtly, infuriatingly wrong.</p><p>That was the core problem I set out to solve while architecting the document-export capability for a</p><p>specification/document-authoring platform: users compose long, structured technical documents — chapters,</p><p>tables, cross-references, a generated table of contents — in a browser-based rich-text editor, and need a</p><p>polished, print-ready PDF (and sometimes a packaged bundle with supporting files) on demand. The documents</p><p>are long enough, and the formatting rich enough, that “good enough” conversion wasn’t acceptable. It had to</p><p>be <em>*exactly*</em> what the editor showed.</p><p><strong>## Why “just render HTML to PDF” wasn’t good enough</strong></p><p>The naive version of this feature is one synchronous API call: take the saved content, run it through an</p><p>HTML-to-PDF library, return the bytes. For short documents and simple formatting, that’s fine. It falls</p><p>apart in two ways once documents get large or formatting gets rich:</p><p>1. <strong>**Timeouts.**</strong> A serverless API has a hard response-time ceiling. Converting a long, table-heavy,</p><p>multi-chapter document — with a generated, correctly-numbered table of contents — can take minutes, not</p><p>seconds, especially under load. You cannot hold an HTTP connection open for that.</p><p>2. <strong>**Fidelity drift.**</strong> A generic HTML-to-PDF library doesn’t know anything about your editor’s specific CSS,</p><p>table plugin quirks, or heading styles. Every visual feature of the editor becomes a second thing to</p><p>reimplement in the exporter, forever, in parallel.</p><p><strong>## The architectural decision that mattered most</strong></p><p>I split the feature into two halves along exactly the boundary where “fast and synchronous” stops making</p><p>sense: a thin, synchronous request layer, and a fully asynchronous, event-driven rendering pipeline behind</p><p>it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p0FpSX32l2CruPDS-mcqjQ.png" /><figcaption>Solution Design</figcaption></figure><p>The request layer does almost nothing: it validates the request, checks whether generation is already in</p><p>progress for that document (more on that below), and drops a message on a queue. It responds in</p><p>milliseconds. Everything expensive happens downstream, asynchronously, with no user waiting on an open</p><p>connection.</p><p>The second, and honestly more interesting, decision was about the render worker itself: **instead of</p><p>building a custom HTML-to-PDF conversion layer, I reused the exact same rendering engine — a real, headless</p><p>browser — that the editor’s own content library uses to display content in the first place.** The render</p><p>worker literally loads the editor’s content renderer inside a headless browser instance, lets it produce</p><p>real DOM with the editor’s own stylesheet applied, and then prints <em>*that*</em> to PDF. The browser that shows you</p><p>the table is the browser that prints the table. There is no second code path to drift out of sync.</p><p>On top of that, I layered a CSS “paged media” library to solve a very specific, annoying problem: generating</p><p>an accurate, auto-numbered table of contents. Standard browser print-to-PDF has no concept of “the actual</p><p>page number this heading landed on” — that information only exists after pagination happens. A paged-media</p><p>polyfill makes that page-number information available as a CSS counter, so the TOC can reference real page</p><p>numbers without me hand-rolling a pagination algorithm.</p><p><strong>## Making “asynchronous” not mean “racy”</strong></p><p>Decoupling the slow part into a queue solves the timeout problem, but introduces a new one: what happens if</p><p>a user clicks “Generate” twice, or edits a document while it’s mid-render? I treated this as two separate</p><p>problems:</p><p>- <strong>**Preventing duplicate work for the same document**</strong>, without blocking unrelated documents from rendering</p><p>in parallel. The fix was to use an ordered queue, grouped <em>*per document*</em>: regenerations of the same</p><p>document are strictly sequential, while different documents still render fully in parallel. A lightweight</p><p>status check in the database acts as a soft lock before anything is queued — not a perfect distributed</p><p>lock, but a deliberate, documented trade-off that’s cheap and good enough, backed by a clear “please retry</p><p>in a few seconds” response if a generation is already in flight.</p><p>- <strong>**Not letting “preview” and “final” generation share the same failure modes.**</strong> Users click “preview”</p><p>constantly, and often impatiently — blocking concurrent previews would just frustrate them for no safety</p><p>benefit. Final document generation, by contrast, is treated as a true single-writer operation with</p><p>conflict detection. Modeling these as two separate, independent status machines rather than one shared</p><p>flag turned out to be the simplest way to keep their very different concurrency rules from leaking into</p><p>each other.</p><p>I also split the pipeline again downstream: rendering a PDF (memory-hungry because of the embedded browser)</p><p>and bundling a final document together with its related files into a ZIP (memory-hungry for a completely</p><p>different reason — buffering/streaming multiple large files) are two separate asynchronous stages, each with</p><p>its own retry queue and its own dead-letter queue. A failure bundling attachments shouldn’t force an</p><p>expensive re-render, and vice versa.</p><p><strong>## What I’d tell another engineer tackling this</strong></p><p>A few lessons that generalized well beyond this one feature:</p><p>1. **If your “export” and your “editor” are visually supposed to match, seriously consider making them the</p><p>same rendering engine**, rather than two engines you promise to keep in sync. It’s a heavier runtime</p><p>dependency, but it eliminates an entire category of bugs.</p><p>2. <strong>**Decouple anything whose duration you can’t bound from anything with a response-time contract.**</strong> A queue</p><p>in between is almost always the right answer, and it buys you retries, dead-letter handling, and</p><p>backpressure almost for free.</p><p>3. <strong>**Idempotency doesn’t have to be perfect to be good enough — it has to be honest.**</strong> I didn’t build a fully</p><p>atomic distributed lock; I built a soft lock with a documented race window and a clear conflict response.</p><p>Knowing exactly where your consistency guarantees end, and handling that gracefully, is more valuable than</p><p>pretending you’ve closed every gap.</p><p>4. <strong>**Not every retry should be treated the same.**</strong> A user-facing “preview” and a committed “final” export can</p><p>have completely different concurrency and retry philosophies — resist the urge to force them through one</p><p>generic workflow.</p><p><strong>## Closing thought</strong></p><p>None of this is exotic technology — queues, headless browsers, status columns in a database. What made it</p><p>work was being deliberate about <em>*where*</em> the system is allowed to be slow, <em>*where*</em> it must be fast, and</p><p><em>*which*</em> failure modes were acceptable to leave as documented trade-offs rather than engineering away at</p><p>disproportionate cost. That, more than any single tool choice, is what I’d call the architecture.</p><p>*I’d love to hear how others have approached the “editor vs. exporter fidelity” problem — if you’ve solved</p><p>it differently, tell me about it.*</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8ad812a4ee85" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>