DEV Community

Cover image for tinypdf - A tiny PDF library in Dart (<600 LOC, zero deps, real PDFs!)
Rizmy Abdulla πŸŽ–οΈ
Rizmy Abdulla πŸŽ–οΈ

Posted on

tinypdf - A tiny PDF library in Dart (<600 LOC, zero deps, real PDFs!)

tinypdf is a small and minimal PDF creation library for Dart that keeps things simple and easy to understand. It comes in under 600 lines of code, has zero dependencies, and still produces fully valid PDF files.

Github repo: Click Here
pub.dev: Click Here

Even with its compact size, it supports:

  • text rendering
  • alignment options
  • rectangles and lines
  • JPEG images
  • basic markdown β†’ PDF conversion

The focus is on keeping the implementation transparent and readable, so the internal PDF structure is clear instead of being buried under layers of abstraction.

⁂ How it handles PDFs

The library works closely with core PDF building blocks:

  • numbered objects
  • page content streams
  • the cross-reference table
  • the trailer section

A PDFBuilder manages objects and pages, and each page exposes a simple PageContext with drawing operations such as:

ctx.text("Hello PDF", 50, 700, 18);
ctx.rect(50, 650, 200, 40, "#ffcc00");
ctx.line(50, 640, 250, 640, "#222");
Enter fullscreen mode Exit fullscreen mode

These map directly to real PDF operators like:

  • BT /F1 18 Tf ... ET
  • re
  • S

The overall idea is to keep behavior predictable and the code easy to follow.

⁂ Text, alignment & colors

tinypdf includes:

  • left, center, and right text alignment
  • optional text box width for alignment behavior
  • hex color parsing (#rgb / #rrggbb)

Text width is calculated using the Helvetica width table and converted to points, which helps alignment stay accurate instead of approximate.

⁂ JPEG image support

Images are embedded as XObjects, with:

  • width and height read from JPEG SOF markers
  • /DCTDecode used for decoding
  • /DeviceRGB as the colorspace

The scope is intentionally focused on JPEG support, aiming to keep things lightweight rather than trying to handle every possible format.

⁂ Markdown β†’ PDF

There’s also a small markdown renderer that supports:

  • #, ##, ### headers
  • bullet lists
  • numbered lists
  • horizontal rules (---)
  • wrapped paragraphs

It works well for simple documents, quick exports, and lightweight reports where a compact PDF generator is enough.

⁂ Where a tiny PDF library fits well

A minimal library like this can be useful when:

  • dependency-free tools are preferred
  • readable source code matters
  • experimenting or learning about PDF internals
  • generating straightforward documents or reports

Instead of packing in large feature sets, tinypdf leans toward clarity, compactness, and direct control over the output.

Sure β€” here’s a cleaner and more natural version of that line:

If you find tinypdf useful, feel free to ⭐ star it on GitHub and leave a πŸ‘ like on pub.dev:

You can keep the links like this:

If you find tinypdf useful, feel free to ⭐ star it on GitHub and leave a πŸ‘ like on pub.dev.

Top comments (0)