Making PDFs with Jennifer

Jennifer

A multi-page table report with the stdlib pdf module: fonts, measuring, right-aligned numbers, clipped columns, page breaks and "Page x of y".

November 2026, by @mplx.
Based on Jennifer 0.26.0

Powered by Jennifer's Cantrip - Markdown into slide decks, https://github.com/jennifer-language/cantrip
1 / 16

The pdf module in one slide

PaperWidth x height
A4 portrait595 x 842
A4 landscape842 x 595
US Letter612 x 792
2 / 16

Hello, PDF

import "pdf.j" as pdf;
use fs;

def pg as pdf.Page init pdf.page(842, 595); # A4 landscape
$pg = pdf.text($pg, 40, 540, "Helvetica-Bold", 20, "Stock list");
$pg = pdf.color($pg, 0, 90, 160);
$pg = pdf.line($pg, 40, 530, 802, 530);
$pg = pdf.rect($pg, 40, 470, 200, 40, false); # false: outline only

def doc as pdf.Document init pdf.addPage(pdf.document(), $pg);
$doc = pdf.info($doc, "Title", "Stock list");
fs.writeBytes("hello.pdf", pdf.render($doc));
3 / 16

Fonts and measuring

pdf.measureText("Helvetica", 8, "Hello")   # 18.224
pdf.measureText("Helvetica", 8, "...")     # 6.672
pdf.measureText("Helvetica", 8, "…")       # 8.0
4 / 16

What we are building

The first page of the stock list

5 / 16

The layout as constants

def const PAGE_W as int init 842;   # A4 landscape
def const PAGE_H as int init 595;
def const MARGIN as int init 40;
def const RIGHT as int init 802;    # PAGE_W - MARGIN
def const FONT as string init "Helvetica";
def const BOLD as string init "Helvetica-Bold";
def const SIZE as int init 8;
def const ROW as int init 12;       # baseline to baseline
def const TOP as int init 520;      # first row's baseline
def const BOTTOM as int init 60;    # keep clear for the footer
def const TITLE as string init "Stock list, November 2026";

38 rows fit between TOP and BOTTOM: 130 rows make 4 pages.

6 / 16

Right-aligned numbers

func width(font as string, s as string) {
    return math.ceil(pdf.measureText($font, SIZE, $s));
}
func rightText(pg as pdf.Page, right as int, y as int,
        font as string, s as string) {
    def x as int init $right - width($font, $s);
    return pdf.text($pg, $x, $y, $font, SIZE, $s);
}
func money(cents as int) {   # 1765045 gives "17,650.45"
    return io.sprintf("%d|group=3|sep=,.%d|pad=2|fill=0",
        $cents // 100, $cents % 100);
}
7 / 16

Clipping a column with an ellipsis

func clip(s as string, max as int) {
    if (width(FONT, $s) <= $max) {
        return $s;
    }
    def cut as string init $s;
    while (len($cut) > 0 and width(FONT, $cut + "…") > $max) {
        $cut = $cut[..len($cut) - 1];
    }
    return $cut + "…";
}
Cordless drill 18 V with two batteries, charger and carrying case, profe…
8 / 16

A page with its header

def struct Cursor { page as pdf.Page, y as int };

func newPage() {
    def pg as pdf.Page init pdf.page(PAGE_W, PAGE_H);
    $pg = pdf.text($pg, MARGIN, 550, BOLD, 14, TITLE);
    $pg = pdf.color($pg, 230, 230, 230);
    $pg = pdf.rect($pg, MARGIN, TOP + 2, RIGHT - MARGIN, 16, true);
    $pg = pdf.color($pg, 0, 0, 0);
    $pg = pdf.text($pg, 110, TOP + 6, BOLD, SIZE, "Product");
    $pg = rightText($pg, 640, TOP + 6, BOLD, "Qty");
    # ... the other four column titles
    return Cursor{page: $pg, y: TOP};
}
9 / 16

Rows and page breaks

def pages as list of pdf.Page init [];
def c as Cursor init newPage();
for (def it in $items) {
    if ($c.y - ROW < BOTTOM) {          # the next row would not fit
        $pages[] = $c.page;
        $c = newPage();
    }
    $c = row($c, $it);                  # draws one row, y - ROW
}
$pages[] = $c.page;
130 rows on 4 pages
10 / 16

"Page x of y": the second pass

The total is known only after the last row, so footers come after the loop:

func footer(pg as pdf.Page, n as int, total as int) {
    def out as pdf.Page init pdf.line($pg, MARGIN, 45, RIGHT, 45);
    $out = pdf.text($out, MARGIN, 32, FONT, SIZE,
        "Example Hardware Ltd.");
    return rightText($out, RIGHT, 32, FONT, "Page {$n} of {$total}");
}

def done as list of pdf.Page init [];
for (def i in 0..len($pages)) {
    $done[] = footer($pages[$i], $i + 1, len($pages));
}
$doc = pdf.addPages($doc, $done);
11 / 16

Or let the module stamp it

def f as pdf.PageLabel init pdf.pageLabel();
$f.left = "Example Hardware Ltd.";
$f.right = "Page %page% of %pages%";
$f.border = true;
$doc = pdf.setFooter($doc, $f);   # also setHeader
Example Hardware Ltd.   Page 1 of 2
Example Hardware Ltd.   Page 2 of 2
12 / 16

The limit: WinAnsi

The standard fonts encode Windows-1252 only: Western European text.

pdf.text($pg, 110, 500, FONT, SIZE, "Gda\u0144sk");   # throws:
# encoding.encode (windows-1252): rune U+0144 ... does not map
pdf.toWinAnsi("Gda\u0144sk \u2192 Kraków", "?")
# gives "Gda?sk ? Kraków"
13 / 16

Metadata, bookmarks, same bytes

$doc = pdf.info($doc, "Title", "Stock list, November 2026");
$doc = pdf.info($doc, "Author", "Warehouse team");
$doc = pdf.info($doc, "CreationDate", pdf.pdfDate(time.utc()));
$doc = pdf.bookmark($doc, 0, 550, "Linz", 1); # page from 0
14 / 16

When pages get busy

def ops as list of pdf.DrawOp init [];
for (def r in $rows) {
    $ops[] = pdf.DrawOp.Text{x: 44, y: $r.y, font: FONT,
        size: SIZE, str: $r.sku};
}
$pg = pdf.draw($pg, $ops);
15 / 16

Read more

Based on Jennifer 0.26.0, November 2026.

16 / 16