mbingpdf v0.1.0

Packages

Vue and Nuxt

Show an mbingpdf document on screen in a Vue or Nuxt app, and make the PDF file from the same definition.

npm install mbingpdf @mbingpdf/vue

Needs Vue 3.5 or later.

Preview

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

const props = defineProps<{ invoice: { number: string } }>();
const document = computed(() =>
  Document(Page({ size: "A4", margin: 36 }, Text(`Invoice ${props.invoice.number}`))),
);
</script>

<template>
  <PdfPreview :document="document" />
</template>

PdfPreview draws the pages exactly as the PDF will have them: the same line breaks, the same page breaks. Pages take the width of the element around them, so the preview is readable on a phone. Each page is drawn only when it comes near the screen, so long documents open at once. Links work, both to the web and within the document.

The document is laid out again whenever the document prop changes. Build it in a computed, as above, so that happens only when the data changes.

Prop
documentThe document to show
gapSpace between pages in CSS pixels; default 16
debugOutline every element's box
page-labelWhat a screen reader calls each page: (page, pages) => string; default Page 1 of 3. The page's text follows it
page-class, page-styleFor each page; the default is a white sheet with a light shadow
Event
layout{ pages }, after each layout
errorThe error, when the document cannot be laid out

The error slot replaces the message shown in place: <template #error="{ error }">…</template>.

The file

<script setup lang="ts">
import { usePdf } from "@mbingpdf/vue";

const pdf = usePdf(document);
</script>

<template>
  <button :disabled="pdf.loading.value" @click="pdf.download('invoice.pdf')">Download</button>
</template>

usePdf(document, options?) takes a ref, a computed or a function, and returns { bytes, url, error, loading, make, download }; all but download are refs. url can be the href of a link or the src of an <iframe>; bytes can be uploaded to a server. The options are those of render().

To make a file without the composable, call render() from mbingpdf and pass the bytes to downloadPdf(bytes, filename).

With usePdf(document, { lazy: true }) nothing is made until pdf.download(name) or pdf.make() is called: for a large document that should not be rendered again on every change. make() gives the bytes.

Notes

MIT licence.

Edit this page on GitHub