Documentation

Data & assets

Reactive data

The whole point of authoring in Vue is that your data drives the document. v-for, v-if, computed and slots all work - the renderer walks the same tree Vue builds, so anything that produces a tree produces a PDF.

<script setup lang="ts">
import { computed } from "vue";
import { Document, Page, Row, Text, Divider } from "@jasy/vue";

const props = defineProps<{ lines: { id: number; name: string; price: number }[] }>();
const total = computed(() => props.lines.reduce((sum, l) => sum + l.price, 0));
</script>

<template>
  <Document :size="11">
    <Page :size="'A4'" :margin="48" :gap="8">
      <Row v-for="line in lines" :key="line.id" :justify="'between'">
        <Text>{{ line.name }}</Text>
        <Text>{{ line.price }} EUR</Text>
      </Row>

      <Divider />
      <Row :justify="'between'">
        <Text bold>Total</Text>
        <Text bold>{{ total }} EUR</Text>
      </Row>
    </Page>
  </Document>
</template>

A PDF is rendered once, not kept alive like a page: load what the document needs beforerenderToPdf and pass it in as props. A change in onMounted (or an async fetch there) comes too late - the document has been read by then.

Custom fonts

The standard PDF fonts (Helvetica, Times, Courier) need nothing. To use your own, register the font on <Document :fonts> under a name, then use that name as a font. In the browser, fetch the bytes first and pass them in:

<!-- Report.vue -->
<script setup lang="ts">
import { Document, Page, Text } from "@jasy/vue";

defineProps<{ font: Uint8Array }>();
</script>

<template>
  <Document :fonts="{ Inter: font }" font="Inter">
    <Page size="A4" :margin="48">
      <Text :size="24" bold>Set in Inter</Text>
    </Page>
  </Document>
</template>
const font = new Uint8Array(await (await fetch("/fonts/Inter.woff2")).arrayBuffer());
const bytes = await renderToPdf(Report, { font });

The container does not matter: .ttf, .woff and .woff2 are the same thing here - jasy unwraps the wrapper itself. Full Unicode comes with it, and the font is subsetted on the way into the PDF, so the file stays small. For weights, register one file per style as { Inter: { normal, bold, italic } }.

On the server (Nuxt's definePdfHandler, or any Node process) a plain path works too - there :fonts="{ Inter: '/path/to/Inter.woff2' }" is the whole story. In the browser there is no filesystem to point at, so it is bytes.

Images

<Image :src> takes the image bytes as a Uint8Array, a URL, or - on a server - a path. JPEG and PNG both work, and PNG transparency is honored: it composites over whatever sits behind it, not a white box. Bytes arrive the same way as a font, as a prop:

<template>
  <Image :src="logo" :width="120" :height="40" fit="contain" />
</template>

A URL is fetched while the document is prepared, before the layout runs, so its size is known when the page is laid out.

Render options

renderToPdf (and renderToPdfString) take the engine's render options as their third argument:

const bytes = await renderToPdf(Invoice, props, {
  onOverflow: "warn",
  accessible: true,
  encrypt: { userPassword: "secret" },
  onDiagnostic: (d) => console.log(d.code, d.message),
});
  • onOverflow - what happens when a single unbreakable element is taller than the page: "error" (default) throws, "warn" places it clipped and logs, "ignore" places it clipped.
  • accessible - a tagged PDF with a structure tree for screen readers, see Accessibility.
  • encrypt - AES-256 password protection, see Encryption.
  • onDiagnostic - everything the engine has to say about the document, one call per finding, see Output.

A descriptor for a server

toDocumentDescriptor(Component, props) stops before the PDF and returns the engine's descriptor: a plain { type, props, children } tree that buildDocument turns into the document. To post it as JSON, encode font and image bytes yourself - JSON has no Uint8Array - and decode them on the other side. Functions do not survive JSON at all, so a document with a <Canvas>, a <Flow> or a <PageBuilder> goes through renderToPdf.