Node API
renderToFile
Renders a PDF document and writes it directly to a file on the filesystem.
Signature
ts
function renderToFile(
element: Component,
filePath: string,
callbackOrProps?: ((output: string, instance: PDFInstance) => void) | Record<string, unknown>,
props?: Record<string, unknown>
): Promise<void>Usage
ts
import { renderToFile } from '@vuepdf/renderer'
import MyDocument from './MyDocument.vue'
await renderToFile(MyDocument, './output.pdf', (output, instance) => {
console.log('PDF saved to', output)
})renderToFile is a convenience wrapper around pdf().toBuffer() + fs.writeFile. Nuxt Server Route
Writing to disk suits generated artifacts you want to keep — nightly reports, exports a user can re-download. Note that a serverless deployment only gives you a writable temp directory, and it does not persist between invocations, so prefer renderToBuffer plus object storage there.
server/api/export.post.ts
import ExportDocument from '../../components/pdf/ExportDocument.vue'
import { renderToFile } from '@vuepdf/renderer'
import { join } from 'node:path'
import { mkdir } from 'node:fs/promises'
export default defineEventHandler(async (event) => {
const { label } = await readBody<{ label: string }>(event)
const dir = join(process.env.EXPORT_DIR ?? './.exports')
await mkdir(dir, { recursive: true })
const name = `export-${Date.now()}.pdf`
await renderToFile(ExportDocument, join(dir, name), { label })
return { file: name }
})components/pdf/ExportDocument.vue
<script setup lang="ts">
import { Document, Page, Text } from '@vuepdf/renderer/components'
defineProps<{ label: string }>()
</script>
<template>
<Document :title="label">
<Page size="A4" :style="{ padding: 48 }">
<Text>{{ label }}</Text>
</Page>
</Document>
</template> Keep the PDF primitive imports in the document SFC. The server route can import the document component and pass props as the third argument to
renderToFile. Importing a .vue document from server/ requires the @vuepdf/nuxt module — see renderToStream for why.