PDF output

Typst supports both paged typesetting and HTML export. One .typ source in Tola can produce two #document(...) outputs in the same build: format: "html" for reading online and format: "pdf" for download or print.

Dual output

Opt pages in through a metadata field (such as pdf: true) and call document(...) twice for matching pages in site.typ:

typst
#import "@tola/address:0.0.0": join-route, output-of, output-url
#import "@tola/schema:0.0.0" as s

#let page-schema = s.schema((
  title: str,
  pdf: s.optional(bool, default: false),
))

#for page in pages {
  let pdf-output = if page.meta.pdf {
    let dir = join-route(page.route-segments).trim("/", at: start)
    dir + "article.pdf"
  } else {
    none
  }

  // 1. HTML output
  document(page.output, format: "html", title: page.meta.title)[
    #html.html(lang: "en")[
      #html.head[#html.title(page.meta.title)]
      #html.body[
        #html.header[
          #if pdf-output != none {
            html.a(href: output-url(pdf-output), download: "")[Download PDF]
          }
        ]
        #html.main[#include page.file]
      ]
    ]
  ]

  // 2. PDF output
  if pdf-output != none {
    document(pdf-output, format: "pdf", title: page.meta.title)[
      #set page(paper: "a4", margin: (x: 2.2cm, y: 2.4cm), numbering: "1 / 1")
      #set text(font: "Libertinus Serif", size: 10.5pt)
      #set par(justify: true, leading: 0.75em)
      #title()
      #include page.file
    ]
  }
}

Generating the download link with output-url(pdf-output) prepends site.base-path automatically and covers it in tola check.

target() branches

Guard rules specific to one target (#set page(...), #place(...), #v(1fr), or html.div(...)) inside shared components and sources with context if target() == "...":

  • target() == "html": inside an HTML document.
  • target() == "paged": inside a PDF, PNG, or SVG paged document.
typst
// components/callout.typ
#let callout(title: none, body) = context {
  if target() == "html" {
    html.aside(class: "callout")[
      #if title != none { html.p(class: "callout-title", title) }
      #body
    ]
  } else {
    block(
      width: 100%,
      inset: 10pt,
      radius: 4pt,
      fill: luma(245),
      stroke: (left: 2.5pt + rgb("#0f766e")),
    )[
      #if title != none [*#title*\ ]
      #body
    ]
  }
}
typst
// content/papers/guide.typ
#show: body => context {
  if target() == "paged" {
    set heading(numbering: "1.1")
    body
  } else {
    body
  }
}

// Page break only in PDF
#context if target() == "paged" { pagebreak() }

Relative site links such as #link("/docs/start/")[Start] break when opened in a standalone PDF viewer. Rewrite root-relative links to full URLs with site.origin in the PDF branch:

typst
#import "@tola/address:0.0.0": route-url
#import "@tola/site:0.0.0": site

#let pdf-links(body) = {
  show link: it => {
    if type(it.dest) == str and it.dest.starts-with("/") and site.origin != none {
      link(route-url(it.dest, origin: site.origin), it.body)
    } else {
      it
    }
  }
  body
}

Fonts

  • HTML prose uses browser CSS fonts, whereas PDF output embeds glyphs from Typst compiler fonts; place fonts needed for CI or --pure PDF builds under [typst.fonts].
  • Opt specific documents in via metadata (pdf: true) instead of compiling every site page as PDF, keeping tola dev rebuilds fast.
  • Wrap images in one helper function that calls @tola/image’s img(...) for HTML and native image(...) for PDF.

Search this site

Use the arrow keys to move through results.

Type to search.