Getting Started
Setup
Install Nuxt PDFMake and generate your first PDF.
Installation
Install nuxt-pdfmake in your Nuxt project.
Add the module to nuxt.config.ts.
nuxt.config.ts
export default defineNuxtConfig({
modules: ["nuxt-pdfmake"],
});
Client Usage
Use usePDFMake() in client-side Vue code.
pages/index.vue
<script setup lang="ts">
const downloadPdf = () => {
const pdfMake = usePDFMake();
pdfMake
?.createPdf({
content: [
{ text: "Hello from Nuxt PDFMake", fontSize: 18, bold: true },
"This PDF was generated in the browser.",
],
})
.download("hello.pdf");
};
</script>
<template>
<button @click="downloadPdf">Download PDF</button>
</template>
usePDFMake() returns null during server-side rendering. Call it from browser interactions, lifecycle hooks, or client-only code.
Server Usage
Use createPDFBuffer() in Nitro route handlers.
server/api/report.get.ts
export default defineEventHandler(async (event) => {
const buffer = await createPDFBuffer({
content: [
{ text: "Server generated PDF", fontSize: 18, bold: true },
"This PDF was generated in a Nitro route.",
],
});
setHeader(event, "Content-Type", "application/pdf");
setHeader(event, "Content-Disposition", 'inline; filename="report.pdf"');
return buffer;
});
createPDFBuffer(), createPDFBase64(), and useServerPDFMake() are auto-imported in your server/ directory. Use createPDFBase64() when you need a base64 string, for example to return the PDF inside a JSON response or to build a data URI.
server/api/report-base64.get.ts
export default defineEventHandler(async () => {
const base64 = await createPDFBase64({ content: ["Hello from a Nitro route"] });
return { filename: "report.pdf", base64 };
});
usePDFMake() and the server utilities are auto-imported while enableComposable is true (the default). See Options to turn that off.Fonts
Out of the box the module registers pdfmake's default Roboto font. To use your own fonts, add local files, Google Fonts, CDN fonts, or reuse the families from @nuxt/fonts in the pdfmake.fonts option. See Font Options for every choice.