pdf - generate simple PDF documents
Import with import "pdf.j" as pdf;. Build a Document of Pages with value-semantic builders - text, lines, rectangles - then render() writes the PDF object / xref structure by hand (no stdlib PDF) as bytes, the way html / label generate their formats. Content streams are FlateDecode-compressed via compress. Pure Jennifer; runs on both binaries.
import "pdf.j" as pdf;
use fs;
def p as pdf.Page init pdf.page(612, 792);
$p = pdf.text($p, 72, 720, "Helvetica", 24, "Hello, PDF");
def doc as pdf.Document init pdf.addPage(pdf.document(), $p);
fs.writeBytes("out.pdf", pdf.render($doc));Runnable: examples/modules/pdf_demo.j.
Coordinates and units
Coordinates are in PDF points (1/72 inch), with the origin at the bottom-left and y increasing upward. All coordinates and sizes are integers. Common page sizes: US Letter 612 x 792, A4 595 x 842. Colours are 0-255 RGB integers.
Building
Every builder is value-semantic - it returns a fresh copy and never mutates its argument, so you thread them ($p = pdf.text($p, ...)).
Each per-op call extends the page's content stream by string concatenation, which copies the stream so far, so drawing many elements one call at a time is O(ops²) (8,000 text calls take about ten times as long as one draw of the same ops). Called as $p = pdf.text($p, ...) on a local inside a function, the page itself is handed over without a copy; at top level, inside a try, or when you keep the old page too, each call also copies the whole page. For a page with lots of content (a table, a long run of lines or text), build a list of DrawOp and apply it once with pdf.draw, which joins the content once and is O(ops). It produces byte-identical output to the equivalent sequential calls.
import "pdf.j" as pdf;
def rows as list of string init ["first row", "second row", "third row"];
def ops as list of pdf.DrawOp init [];
def y as int init 720;
for (def row in $rows) {
$ops[] = pdf.DrawOp.Text{x: 72, y: $y, font: "Helvetica", size: 10, str: $row};
$y = $y - 14;
}
def p as pdf.Page init pdf.draw(pdf.page(612, 792), $ops);The variants mirror the per-op functions: DrawOp.Text / DrawOp.TextUnicode (embedded font), DrawOp.Glyphs (glyph ids already mapped in an embedded font, like textGlyphs), DrawOp.Line, DrawOp.Rect (filled), DrawOp.Color (sets the colour for ops after it), DrawOp.Image (an image already registered with addImage), and DrawOp.Link.
| Call | Returns | |
|---|---|---|
pdf.document() | Document | an empty document |
pdf.page(width, height) | Page | a blank page of the given size (both > 0, else pdf error) |
pdf.text(pg, x, y, font, size, str) | Page | draw text in a standard-14 font at (x, y) |
pdf.loadFont(name, ttfBytes) | LoadedFont | load an embeddable TrueType font under a resource name |
pdf.addFont(doc, lf) | Document | register an embedded font in the document |
pdf.addFonts(doc, lfs) | Document | register a list of LoadedFont in one call |
pdf.textUnicode(pg, x, y, lf, size, str) | Page | draw Unicode text in an embedded font at (x, y) |
pdf.textGlyphs(pg, x, y, font, size, gids, cps) | Page | draw glyph ids of a registered embedded font, named by its resource name, with the code point of each (see below) |
pdf.loadImage(name, imgBytes) | Image | load a PNG / JPEG / GIF under a resource name |
pdf.imageFrom(name, img) | Image | embed an images.Image under a resource name |
pdf.addImage(doc, img) | Document | register an embedded image in the document |
pdf.addImages(doc, imgs) | Document | register a list of Image in one call; use it instead of addImage in a loop, which is O(images squared) |
pdf.drawImage(pg, img, x, y, width, height) | Page | draw an image into the box at (x, y) |
pdf.measureText(font, size, str) | float | width of str in points (standard-14) |
pdf.measureTextUnicode(lf, size, str) | float | width of str in points (embedded font) |
pdf.wrapText(font, size, str, maxWidth) | list of string | word-wrap to maxWidth points (standard-14); a line with an unbreakable token is hard-folded so nothing exceeds maxWidth |
pdf.foldLine(font, size, text, maxWidth) | list of string | hard-fold one line to maxWidth, breaking at a space / seam or mid-token (a code line, a bare URL) |
pdf.toWinAnsi(s, replacement) | string | replace every character the standard-14 fonts cannot encode with replacement ("" drops it) |
pdf.wrapTextUnicode(lf, size, str, maxWidth) | list of string | word-wrap to maxWidth points (embedded font) |
pdf.textBlock(pg, x, y, width, font, size, leading, str, align) | Page | flow wrapped, aligned text into a column (standard-14) |
pdf.textBlockUnicode(pg, x, y, width, lf, size, leading, str, align) | Page | flow wrapped, aligned text into a column (embedded font) |
pdf.line(pg, fromX, fromY, toX, toY) | Page | draw a stroked line |
pdf.rect(pg, x, y, width, height, filled) | Page | draw a rectangle (fill or stroke) |
pdf.link(pg, x, y, width, height, uri) | Page | a clickable link annotation (/Link + /URI); an invisible rect over drawn text makes it a hyperlink |
pdf.color(pg, red, green, blue) | Page | set fill + stroke colour for what follows (each 0-255, else pdf error; also for DrawOp.Color) |
pdf.draw(pg, ops) | Page | apply a list of DrawOp in order (batch form of the per-op draws; see below) |
pdf.addPage(doc, pg) | Document | append a page |
pdf.addPages(doc, pgs) | Document | append a batch of pages in one copy; use this (collecting pages into a list of Page) instead of addPage in a loop, which is O(pages squared) |
pdf.bookmark(doc, page, y, title, level) | Document | append an outline entry (see Bookmarks) |
pdf.bookmarks(doc, entries) | Document | append a list of OutlineEntry in one call; use it instead of bookmark in a loop, which is O(entries squared) |
pdf.pageLabel() | PageLabel | a blank running header / footer spec (Helvetica 9pt, 36pt margin) |
pdf.setHeader(doc, label) | Document | attach a running header, drawn on every page at render |
pdf.setFooter(doc, label) | Document | attach a running footer, drawn on every page at render |
pdf.getTotalPages(doc) | int | pages added so far (what %pages% expands to) |
pdf.getCurrentPageNr(doc) | int | the page being built, getTotalPages(doc) + 1 |
pdf.render(doc) | bytes | the finished PDF (a document without pages is a pdf error) |
Running headers and footers
A PageLabel is drawn on every page at render time, once the total page count is known. It has three text slots - left / center / right, each aligned with the font's own metrics - and each may contain %page% (the 1-based page number) and %pages% (the total). The tokens are percent-delimited (not braces) so they do not collide with Jennifer's {expr} string interpolation. border draws a thin rule under a header / over a footer; margin is the inset from the page edge; font / size / red / green / blue style the text.
import "pdf.j" as pdf;
def doc as pdf.Document init pdf.addPages(pdf.document(), [pdf.page(595, 842), pdf.page(595, 842)]);
def f as pdf.PageLabel init pdf.pageLabel();
$f.left = "sample.pdf";
$f.center = "page %page%/%pages%"; # -> "page 13/108"
$f.right = "jennifer-lang.dev";
$f.rightUri = "https://jennifer-lang.dev"; # makes the right slot a clickable backlink
$f.border = true;
$doc = pdf.setFooter($doc, $f);
def out as bytes init pdf.render($doc);A slot becomes a clickable link when its leftUri / centerUri / rightUri is set - e.g. a footer backlink to a website, drawn on every page. (It is a pdf.link annotation over the slot's measured rectangle; use pdf.link directly to make any drawn text a hyperlink.)
getTotalPages / getCurrentPageNr report the page count while building (the total is known only once every page is added, which is why the placeholders resolve at render). Because Jennifer has no closures, per-page footer text beyond the %page% / %pages% placeholders is not expressible; for fully custom per-page labels, build all pages, then loop them drawing with pdf.text and getTotalPages(doc).
color sets the drawing colour for subsequent operations on that page (both fill and stroke), so order matters: set the colour, then draw. rect's filled flag fills the rectangle when true, otherwise strokes its outline.
Fonts
text takes one of the standard-14 base fonts every PDF viewer provides; any other name throws Error{kind: "pdf"}:
Helvetica Helvetica-Bold Helvetica-Oblique Helvetica-BoldOblique
Times-Roman Times-Bold Times-Italic Times-BoldItalic
Courier Courier-Bold Courier-Oblique Courier-BoldOblique
Symbol ZapfDingbatsEach distinct font used becomes one shared Type1 font object (WinAnsiEncoding). Text is escaped for the PDF literal-string syntax (\, (, ), and line breaks), so any ASCII / Latin-1 string is safe to pass. A character WinAnsi cannot encode (ń, →) makes text, draw, measureText, wrapText, foldLine, textBlock and a running header or footer throw Error{kind: "pdf"} naming the first one (character U+0144 is outside WinAnsi, ...); pdf.toWinAnsi replaces such characters beforehand.
Embedded TrueType fonts (Unicode)
For text beyond Latin-1 - accented text, Greek, Cyrillic, CJK, ... - embed a TrueType (.ttf) font and draw with textUnicode:
import "pdf.j" as pdf;
use fs;
def body as pdf.LoadedFont init pdf.loadFont("Body", fs.readBytes("/path/NotoSans.ttf"));
def doc as pdf.Document init pdf.addFont(pdf.document(), $body);
def p as pdf.Page init pdf.textUnicode(pdf.page(595, 842), 50, 780, $body, 18, "Héllo, 日本語");
$doc = pdf.addPage($doc, $p);
fs.writeBytes("out.pdf", pdf.render($doc));The font is embedded as a Type0 / CIDFontType2 composite (Identity-H encoding, FontFile2 font program) over the font module: each character maps through the font's cmap to a glyph, so any script the font covers renders. A ToUnicode map is written too, so the text stays selectable and copyable in a viewer. The whole font file is embedded (no glyph subsetting). loadFont accepts a TrueType (glyf) font; a CFF / OpenType-PostScript (.otf) font throws (FontFile3 embedding is not supported).
Every call that takes a LoadedFont hands the whole font (its bytes included) into the module. A layout engine that has already mapped and measured its text (with font.glyphIds / font.advances, once per font) draws with textGlyphs(pg, x, y, font, size, gids, cps) or DrawOp.Glyphs instead: the font is named by its resource name, gids are the glyph ids and cps the code point each one stands for (for the ToUnicode map). The page records each distinct glyph once (textUnicode records every glyph drawn), so it stays small however much text it holds; the rendered PDF is the same bytes as with textUnicode for the same glyphs. A malformed name, a glyph id outside 0..65535 or cps of another length than gids throws Error{kind: "pdf"}. doctree draws its embedded faces this way.
Images
Embed a PNG, JPEG or GIF as an image XObject and draw it scaled into a box:
import "pdf.j" as pdf;
use fs;
def logo as pdf.Image init pdf.loadImage("Logo", fs.readBytes("/path/logo.png"));
def doc as pdf.Document init pdf.addImage(pdf.document(), $logo);
def p as pdf.Page init pdf.drawImage(pdf.page(595, 842), $logo, 50, 700, 120, 90);
$doc = pdf.addPage($doc, $p);
fs.writeBytes("out.pdf", pdf.render($doc));loadImage detects the format from the file signature and reads the pixel geometry / colour space. addImage registers the image on the document (so the resource is written once and can be reused across pages); drawImage places it, scaled to fill the width x height box in points - pass a box in the image's own proportion to avoid stretching. The origin is the box's lower-left, like everything else.
Supported inputs:
| Format | Handling |
|---|---|
| JPEG (baseline / progressive) | embedded as-is via DCTDecode; greyscale / RGB / CMYK (an Adobe CMYK file gets an inverting /Decode) |
PNG greyscale / RGB / palette (non-interlaced, no tRNS) | embedded directly - the raw zlib image data with a FlateDecode PNG predictor, so no pixel decode happens (1 / 2 / 4 / 8 / 16-bit) |
PNG with alpha (greyscale+alpha / RGBA, any depth), interlaced (Adam7) or with tRNS transparency; GIF (first frame) | decoded natively by the images library and embedded as 8-bit RGB, plus an 8-bit greyscale soft mask (/SMask) when any pixel is translucent (a tRNS colour key or palette transparency included), so transparency renders. The decoder reads no more pixel data than the dimensions imply; a file past images.decode's pixel cap (64 Mpx, 8 Mpx on jennifer-tiny) is a limit error |
Headers are walked with bounds, so a crafted file cannot keep the parser busy: more than 4096 JPEG markers before the frame header, more than 4096 PNG chunks that carry no image data, or PNG image data split into more than 4096 chunks averaging under 256 bytes, is a limit error, like every reached cap.
The image and its soft mask round-trip pixel-exact (the JPEG is re-embedded losslessly; 16-bit input decoded through images comes down to 8 bits). Any other file - not a PNG, JPEG or GIF, or a malformed one - throws Error{kind: "pdf"} with a reason. An image larger than images.decode's pixel cap throws Error{kind: "limit"} instead, like any reached cap, and the other generic kinds ("timeout", "closed", "cancelled") pass through unchanged too. addImage the image before you drawImage it, so the /name resolves (the same rule as addFont / textUnicode).
From an images.Image
A picture built or edited with the images library - a resized photo, a composited badge, a dithered logo - embeds with imageFrom, without encoding it to PNG and parsing it again:
import "pdf.j" as pdf;
use fs;
use images;
def photo as images.Image init images.fit(images.decode(fs.readBytes("photo.jpg")), 800, 800);
def img as pdf.Image init pdf.imageFrom("Photo", $photo);
def doc as pdf.Document init pdf.addImage(pdf.document(), $img);The pixels embed as an 8-bit RGB colour stream, plus an alpha soft mask when any pixel is not fully opaque. A JPEG that needs no editing is better passed to loadImage, which embeds it as-is: imageFrom stores decoded pixels (Flate compressed), which is larger than the JPEG for a photograph.
Text layout
text / textUnicode place a single string at exact coordinates. To flow a paragraph into a column - wrapped, aligned, multi-line - use the layout functions, which sit on top of accurate width measurement.
Measure. measureText(font, size, str) returns the rendered width of a string in points, using the Adobe Core-14 AFM metrics for the standard-14 fonts; measureTextUnicode(lf, size, str) uses an embedded font's own glyph advances.
Wrap. wrapText(font, size, str, maxWidth) greedily word-wraps to a maximum line width (points) and returns the lines. Existing newlines in str are honoured as hard breaks (so a \n\n leaves a blank line), and runs of spaces collapse. A token wider than maxWidth (no space to break on) is hard-folded via foldLine, so no line exceeds maxWidth. wrapTextUnicode is the embedded-font form; it does not hard-fold, so there an over-wide word lands alone on its line and overflows.
Flow. textBlock(pg, x, y, width, font, size, leading, str, align) wraps str to width and draws each line - the first line's baseline at (x, y), each next line leading points lower:
import "pdf.j" as pdf;
def p as pdf.Page init pdf.page(612, 792);
def body as string init "Jennifer flows wrapped, justified paragraphs into a "
+ "column using accurate standard-14 width metrics.";
$p = pdf.textBlock($p, 72, 720, 240, "Times-Roman", 11, 15, $body, "justify");
# the block is len(pdf.wrapText("Times-Roman", 11, $body, 240)) * 15 points tallalign is one of:
align | Effect |
|---|---|
"left" | lines start at x (the default look) |
"right" | lines end at x + width |
"center" | lines centred in the column |
"justify" | inter-word gaps padded so each line fills width exactly - except the last line of each paragraph, which stays left-aligned |
textBlockUnicode is the same for an embedded font. The block does not clip or paginate; a column height is len(wrapText(...)) * leading, so you decide where it ends and when to start a new page. Measuring a Symbol / ZapfDingbats font, or a character outside WinAnsi, throws Error{kind: "pdf"}, and so does a width of 0 or less passed to wrapText, wrapTextUnicode, foldLine, textBlock or textBlockUnicode: no line fits it.
Metadata
Set document metadata - the PDF Info dictionary shown in a viewer's "Document Properties" - with pdf.info(doc, key, value). key is a PDF Info key:
| Key | |
|---|---|
Title / Author / Subject / Keywords | the descriptive fields |
Creator | the app that authored the source |
Producer | the app that wrote the PDF (defaults to "Jennifer pdf") |
CreationDate / ModDate | PDF date strings (see pdfDate below) |
import "pdf.j" as pdf;
def doc as pdf.Document init pdf.document();
$doc = pdf.info($doc, "Title", "Q3 Report");
$doc = pdf.info($doc, "Author", "Ada Lovelace");
$doc = pdf.info($doc, "Keywords", "report, finance, q3");document() presets Producer to "Jennifer pdf"; every other field is unset until you set it. Any custom key works too. Dates use the PDF date syntax, which pdf.pdfDate(t) builds from a time.Time:
import "pdf.j" as pdf;
use time;
def doc as pdf.Document init pdf.document();
$doc = pdf.info($doc, "CreationDate", pdf.pdfDate(time.utc())); # D:20260714160000+00'00'Metadata and bookmark titles are PDF text strings, read by the viewer rather than set in a font like page text, so they are not limited to what the standard-14 fonts can encode. An ASCII value is written as a plain literal; anything else goes out as UTF-16BE with a byte order mark, which every viewer reads and which covers every script. Nothing has to be transliterated, and no embedded font is involved.
Bookmarks (outline)
pdf.bookmark(doc, page, y, title, level) appends an outline entry - a bookmark in the viewer's sidebar that jumps to page page (0-based) at y (PDF points, origin bottom-left). Entries nest by level in the order added: a level-2 entry becomes a child of the most recent level-1, and so on, so a heading hierarchy becomes a bookmark tree. When any bookmark is present, render emits the /Outlines object tree and opens the document with the bookmark panel showing (/PageMode /UseOutlines). Building the tree takes one pass over the entries, so thousands of bookmarks cost no more per entry than a few.
pdf.bookmarks(doc, entries) appends a whole list of OutlineEntry (title, page, y, level) at once, with the same nesting and checks; nothing is appended when an entry is invalid. Each bookmark call hands the whole document into the module and back, so a long outline built one call at a time costs O(entries squared); collect the entries and add them with one bookmarks call.
A level below 1 or a negative page throws Error{kind: "pdf"} at once. The page index is checked against the document when it is rendered, so a bookmark may be added before its page; a page past the last page then makes render throw Error{kind: "pdf"} rather than point at another page.
import "pdf.j" as pdf;
def doc as pdf.Document init pdf.addPages(pdf.document(), [pdf.page(612, 792), pdf.page(612, 792)]);
$doc = pdf.bookmark($doc, 0, 720, "Introduction", 1);
$doc = pdf.bookmark($doc, 0, 600, "Background", 2); # nests under Introduction
$doc = pdf.bookmark($doc, 1, 720, "Results", 1);A title may hold any script - see the note under Metadata. A German heading keeps its umlauts in the sidebar even when the page it points at is drawn in a standard-14 font.
Rendering
render(doc) produces a complete PDF 1.7 file as bytes: a catalog, a page tree, one page dict + one FlateDecode-compressed content stream per page, the shared font objects, the image XObjects (each with a soft-mask XObject when it carries alpha), an Info dictionary when any metadata is set, a cross-reference table with correct byte offsets, and the trailer. Object numbers are assigned dynamically, so any mix of pages, fonts, and images cross-references correctly. Write it with fs.writeBytes, return it from an httpd handler, or attach it via mime. It validates clean under qpdf --check. A PDF needs at least one page, so rendering a document without pages throws Error{kind: "pdf"}.
Byte-identical output. The same document always renders to the exact same bytes - on either binary, run to run. pdf never stamps a CreationDate or any other timestamp on its own (set one with info + pdfDate), so the output can be asserted against a golden file and reproduced for content-addressed builds.
Scope
- Text, lines, rectangles. The standard-14 fonts, solid fills / strokes, and RGB colour. No curves / paths beyond rectangles, no clipping, no transparency.
- Embedded TrueType fonts (Type0 / CIDFontType2, Unicode + ToUnicode) via
loadFont/textUnicode. No glyph subsetting (smaller files) and no CFF.otfembedding. - Raster images (PNG of any kind, JPEG, GIF) via
loadImage/drawImage, and animages.ImageviaimageFrom. - Text layout - width measurement, word-wrap, and left / right / center / justify column flow via
measureText/wrapText/textBlock. No automatic pagination or clipping (you place each block); tab stops, hyphenation, and mixed inline runs are out of scope. - A writer, not a reader. It generates PDFs; it does not parse them.
See also
- compress.md - the FlateDecode (
zlib) streams. - html.md / label.md - the sibling format-generation modules.
- fs.md -
writeBytesto save the rendered PDF. - modules/index.md - the module catalog and import rules.
Academy talks
| Talk | What it shows |
|---|---|
| Images, barcodes and labels with Jennifer | a shipping label and a scanned page as PDF |
| Inside a document pipeline: doctree, MyST, reST and fonts | rendering a doctree to PDF with PdfOptions |
| Invoicing with web services: VAT, SOAP and GraphQL | one PDF invoice per order |
| Making PDFs with Jennifer | a multi-page table report with page breaks and Page x of y footers |
API reference
Every exported function, struct and constant of pdf, with its signature, generated from the module's doc comments: the pdf API reference.