# Generate a PDF from JSON with Typst

Author: Daniel Hallman

This small example turns fictional work items into a PDF using a local Typst compiler. It uses no browser, external package, or network request during compilation.

## Files

- `sample.json`: the report title, display date, summary, and work items.
- `report.typ`: the template, input checks, and page layout.
- `report.pdf`: the compiled sample output.
- `preview.png`: a preview of the sample PDF layout.

## Compile the report

Install the [Typst CLI](https://github.com/typst/typst#installation), then run these commands from this directory:

```sh
typst --version
typst compile --ignore-system-fonts report.typ report.pdf
```

This example was checked with Typst 0.15.1. It uses the compiler's bundled Libertinus Serif font. `--ignore-system-fonts` avoids a dependency on fonts installed on your computer. There are no package imports or additional font downloads.

To choose another JSON file in the same directory, pass its filename:

```sh
typst compile --ignore-system-fonts --input data=sample.json report.typ report.pdf
```

To render a PNG preview, run:

```sh
typst compile --ignore-system-fonts --format png --ppi 144 report.typ preview.png
```

For a report with several pages, use `preview-{p}.png` as the output filename so each page has a separate image.

## Change the data

The top-level JSON value must be an object containing nonempty strings for `title`, `date`, and `summary`. `date` is display text; the template does not validate calendar dates. `rows` must be a nonempty array of objects. Each row needs nonempty strings for `item`, `status`, and `note`.

The allowed statuses are `Complete`, `In progress`, and `Needs review`. A missing field, wrong type, empty string, or unknown status stops compilation with an error. Extra fields are ignored.

The template passes input strings to `text()`. It does not call `eval()` or treat the data as Typst markup. Keep the template itself trusted. This example does not provide file-upload security, resource limits, or a sandbox for untrusted templates.

## Check the output

Open the PDF after changing the data. Check the title, rows, status labels, line wrapping, and page breaks. The table uses `table.header()` so its column headings repeat when it continues onto another page.

The supplied sample is one page. Longer reports can need layout changes, especially when one cell contains a large amount of text. This example demonstrates compilation and input checks; it does not establish a speed or memory comparison with browser-based PDF generation.

## References

- [Typst JSON loading](https://typst.app/docs/reference/data-loading/json/)
- [Typst external inputs](https://typst.app/docs/reference/foundations/sys/)
- [Typst table headers](https://typst.app/docs/reference/model/table/#definitions-header)

The example source is available under the MIT license in `LICENSE`.
