<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/taking-generators-too-far-writing-a-pdf-from-scratch-in-python---arie-bovenberg-6sa6fxlnq" -->

---
title: Taking Generators Too Far: Writing a PDF from Scratch in...
description: A conference talk walks through building a minimal PDF writer from scratch in Python using only generators, motivated by frustration installing existing...
canonical: https://daily.dev/posts/taking-generators-too-far-writing-a-pdf-from-scratch-in-python---arie-bovenberg-6sa6fxlnq
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Taking Generators Too Far: Writing a PDF from Scratch in Python - Arie Bovenberg | daily.dev
og:description: A conference talk walks through building a minimal PDF writer from scratch in Python using only generators, motivated by frustration installing existing...
og:url: https://daily.dev/posts/taking-generators-too-far-writing-a-pdf-from-scratch-in-python---arie-bovenberg-6sa6fxlnq
og:image: https://api.daily.dev/og/posts/6sA6FXlnq.png
og:image:alt: Taking Generators Too Far: Writing a PDF from Scratch in Python - Arie Bovenberg
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Taking Generators Too Far: Writing a PDF from Scratch in Python - Arie Bovenberg

**[PyCon US](https://daily.dev/sources/pyconus)** · 26 min read · 0 upvotes · 0 comments

## Summary

A conference talk walks through building a minimal PDF writer from scratch in Python using only generators, motivated by frustration installing existing PDF/typography libraries and a desire to fix improper kerning that most Python PDF writers get wrong. The speaker explains the PDF file structure (header, objects, cross-reference table), then shows how to stream pages, content streams, kerning adjustments (using itertools.pairwise), zlib streaming compression, and generator return values to track stream length via PDF references, all while keeping memory usage constant regardless of document size. The result is a PDF writer built from seven generator functions, explicitly framed as a niche typography-focused project rather than a general-purpose PDF library.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.youtube.com/watch?v=WqdUOt0TRP0>

## Questions this post answers

### How can I generate a PDF file in Python without loading the whole document into memory?

Structure the writer as a chain of Python generators rather than building strings in memory: one generator per PDF object (page, page tree node, content stream), using itertools.pairwise for kerning adjustments, itertools.chain to combine command streams, zlib's streaming compressobj for constant-memory compression, and generator return values to pass computed lengths and object offsets forward without buffering the full document.

_For teams optimizing memory use while generating large PDFs, daily.dev surfaces practical patterns like this for streaming architectures._

### Why does text look wrong in PDFs generated by simple Python PDF libraries?

Most simple PDF writers skip kerning, the practice of nudging specific character pairs (like W and A, or A and V) closer together or farther apart so spacing looks visually correct. PDF does not apply kerning by default, so a writer must insert explicit kerning adjustment values between character pairs using a kerning table specific to the font, or the text will look subtly uneven, especially around capital letters.

_Developers debugging typography quirks in generated PDFs can find deep dives like this through daily.dev._

### How do I calculate the length of a compressed PDF content stream before writing it into the stream's header dictionary?

Since a generator cannot go back in time to fill in a header once the length is known, use a PDF indirect reference: declare the stream length as a reference to a separate object, then write that length object right after the compressed content stream, once its total byte count has been tallied via a length-tracking generator that passes chunks through unchanged while accumulating a running total.

_Anyone architecting streaming file formats can track solutions like this reference trick on daily.dev._

## Similar posts on daily.dev

- [Zero-Dependency PDF Generator in Vanilla JavaScript](https://daily.dev/posts/zero-dependency-pdf-generator-in-vanilla-javascript-4vy9xz1tk) · SitePoint · 2 upvotes · 0 comments
- [Lulzx/tinypdf: Minimal PDF creation library. <400 LOC, zero dependencies, makes real PDFs.](https://daily.dev/posts/lulzx-tinypdf-minimal-pdf-creation-library-400-loc-zero-dependencies-makes-real-pdfs--enagb4xyw) · Hacker News · 33 upvotes · 0 comments
- [Six generative art pieces in one Python file, no dependencies](https://daily.dev/posts/six-generative-art-pieces-in-one-python-file-no-dependencies-kwej2x4mb) · Medium · 0 upvotes · 0 comments

---

Tags: [#python](https://daily.dev/tags/python)

[View this post on daily.dev](https://daily.dev/posts/taking-generators-too-far-writing-a-pdf-from-scratch-in-python---arie-bovenberg-6sa6fxlnq)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Taking Generators Too Far: Writing a PDF from Scratch in Python - Arie Bovenberg","url":"https://daily.dev/posts/taking-generators-too-far-writing-a-pdf-from-scratch-in-python---arie-bovenberg-6sa6fxlnq","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/taking-generators-too-far-writing-a-pdf-from-scratch-in-python---arie-bovenberg-6sa6fxlnq"},"datePublished":"2026-08-25T18:58:24.058Z","dateModified":"2026-09-14T07:19:03.696Z","description":"A conference talk walks through building a minimal PDF writer from scratch in Python using only generators, motivated by frustration installing existing...","image":"https://i.ytimg.com/vi/WqdUOt0TRP0/sddefault.jpg","thumbnailUrl":"https://i.ytimg.com/vi/WqdUOt0TRP0/sddefault.jpg","isAccessibleForFree":true,"articleSection":"PyCon US","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"PyCon US","logo":"https://media.daily.dev/image/upload/s--DLedNxG1--/f_auto,q_auto/v1777800767/logos/pyconus?_a=BAMAMiWQ0","url":"https://daily.dev/sources/pyconus"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/taking-generators-too-far-writing-a-pdf-from-scratch-in-python---arie-bovenberg-6sa6fxlnq","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"python","timeRequired":"PT26M","video":{"@type":"VideoObject","name":"Taking Generators Too Far: Writing a PDF from Scratch in Python - Arie Bovenberg","description":"A conference talk walks through building a minimal PDF writer from scratch in Python using only generators, motivated by frustration installing existing...","thumbnailUrl":"https://i.ytimg.com/vi/WqdUOt0TRP0/sddefault.jpg","uploadDate":"2026-08-25T18:58:24.058Z","duration":"PT26M","url":"https://api.daily.dev/r/6sA6FXlnq","embedUrl":"https://www.youtube.com/embed/WqdUOt0TRP0"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"PyCon US","item":"https://daily.dev/sources/pyconus"},{"@type":"ListItem","position":3,"name":"Taking Generators Too Far: Writing a PDF from Scratch in Python - Arie Bovenberg"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/taking-generators-too-far-writing-a-pdf-from-scratch-in-python---arie-bovenberg-6sa6fxlnq#faq","mainEntity":[{"@type":"Question","name":"How can I generate a PDF file in Python without loading the whole document into memory?","acceptedAnswer":{"@type":"Answer","text":"Structure the writer as a chain of Python generators rather than building strings in memory: one generator per PDF object (page, page tree node, content stream), using itertools.pairwise for kerning adjustments, itertools.chain to combine command streams, zlib's streaming compressobj for constant-memory compression, and generator return values to pass computed lengths and object offsets forward without buffering the full document. For teams optimizing memory use while generating large PDFs, daily.dev surfaces practical patterns like this for streaming architectures."}},{"@type":"Question","name":"Why does text look wrong in PDFs generated by simple Python PDF libraries?","acceptedAnswer":{"@type":"Answer","text":"Most simple PDF writers skip kerning, the practice of nudging specific character pairs (like W and A, or A and V) closer together or farther apart so spacing looks visually correct. PDF does not apply kerning by default, so a writer must insert explicit kerning adjustment values between character pairs using a kerning table specific to the font, or the text will look subtly uneven, especially around capital letters. Developers debugging typography quirks in generated PDFs can find deep dives like this through daily.dev."}},{"@type":"Question","name":"How do I calculate the length of a compressed PDF content stream before writing it into the stream's header dictionary?","acceptedAnswer":{"@type":"Answer","text":"Since a generator cannot go back in time to fill in a header once the length is known, use a PDF indirect reference: declare the stream length as a reference to a separate object, then write that length object right after the compressed content stream, once its total byte count has been tallied via a length-tracking generator that passes chunks through unchanged while accumulating a running total. Anyone architecting streaming file formats can track solutions like this reference trick on daily.dev."}}]}
```

