pkg

ctxcode/valk-pdf

Pure-Valk PDF generator with text layout, tables, vector drawing and image embedding

github.com/ctxcode/valk-pdf ↗
0.2.0 latest Valk ≥ 0.9.1

Install

vman install github.com/ctxcode/valk-pdf

Adds the package to your valk.json. To allow only some versions, add them: vman install github.com/ctxcode/valk-pdf 0.2.x

valk-pdf

Pure-Valk PDF 1.4 generator with zero external dependencies. Supports standard Type1 typography
with accurate glyph measurement and word wrapping, vector drawing primitives, flowing multi-page
reports and tables with repeating headers, and JPEG / raw pixel image embedding with FlateDecode
compression via valk.compress.

Requires Valk 0.9.1 or newer.

Install

vman install github.com/ctxcode/valk-pdf

Quick Start

use pdf

fn main() {
    let doc = pdf.Document.new(pdf.DocumentOptions {
        page_size: pdf.PageSize.a4()
        margins: pdf.Margins.uniform(48.0)
        footer_height: 20.0
        footer_text: "Page {page} of {pages}"
        metadata: pdf.Metadata {
            title: "Invoice #2026-042"
            author: "Acme Studio"
        }
    })

    doc.heading("Invoice #2026-042", 1)
    doc.paragraph("Thank you for your business — summary of services rendered:")

    let tbl = pdf.Table.new()
        .column("Item", 0.0, 3.0, pdf.TextAlign.left)
        .column("Qty", 50.0, 0.0, pdf.TextAlign.right)
        .column("Total", 80.0, 0.0, pdf.TextAlign.right)
        .row(Array[String]{ "Consulting Services", "8 h", "€1,120.00" })
        .row(Array[String]{ "PDF Report Pipeline", "6 h", "€840.00" })

    doc.table(tbl.options)
    doc.write_file("invoice.pdf") ! panic(E.message)
}

Coordinate System

All coordinates on Page use a top-left origin in PDF points (1 pt = 1/72 inch):

  • (0, 0) is the top-left corner of the page.
  • x increases to the right; y increases downward.
  • For page.text(x, y, ...) and page.text_wrapped(x, y, ...), y is the top edge of the line.
  • Coordinates are automatically converted to PDF's bottom-up space when writing content streams.

Features

  • Document & Pages: PageSize.a4(), letter(), legal(), custom(w, h), .landscape(),
    .portrait(), margins, metadata (/Info with ASCII or UTF-16BE Unicode), headers & footers
    (placeholder templates or custom closures), and toggleable FlateDecode (zlib) compression.
  • Typography & Layout: All 14 standard PDF Type1 fonts (Helvetica, Times, Courier
    families, Symbol, ZapfDingbats) with Adobe AFM glyph width tables, measure_text,
    wrap_text, WinAnsiEncoding UTF-8 mapping (€, é, ü, ñ, —, –, “, ”, ’, •,
    ©, £, ...), and left/center/right alignment.
  • Vector Drawing: Color.rgb, Color.hex, Color.gray, named helpers, line, rect,
    filled_rect, stroked_rect, rounded_rect, circle, custom Path (lines & cubic Beziers),
    stroke width, dash patterns, and PDF 1.4 /ExtGState fill/stroke opacity.
  • Flow & Tables: Automatic page-breaking heading, paragraph, spacer, hr, image, and
    table with fixed or proportional column widths, cell wrapping, column alignment, alternating
    row striping, borders, and auto-repeating headers on continuation pages.
  • Image Embedding: Zero-re-encoding JPEG embedding (DCTDecode, parses SOF0/SOF1/SOF2
    dimensions and color space) and raw 8-bit Grayscale / RGB / RGBA pixel embedding (FlateDecode
    with automatic /SMask soft mask for RGBA transparency).

Development

make test runs the test suite, make lint checks the sources with --deny-warnings,
make example generates a sample invoice PDF, and make docs regenerates the API docs.