UNPKG

@maistik/nuxt-pdf

Version:

A Nuxt 3 & 4 module for server-side PDF generation using Handlebars templates

43 lines (42 loc) 1.23 kB
import { useRuntimeConfig } from "#imports"; export function usePdf() { const config = useRuntimeConfig(); const generate = async (template, data, options = {}, locale) => { const response = await $fetch("/api/pdf", { method: "POST", body: { template, ctx: { data, options, locale: locale || config.public.pdf.defaultLocale } }, responseType: "arrayBuffer" }); return new Blob([response], { type: "application/pdf" }); }; const download = async (template, data, options = {}, filename = "document.pdf", locale) => { const blob = await generate(template, data, options, locale); const url = URL.createObjectURL(blob); const link = document.createElement("a"); link.href = url; link.download = filename; document.body.appendChild(link); link.click(); document.body.removeChild(link); URL.revokeObjectURL(url); }; const getAvailableLocales = () => { return config.public.pdf.availableLocales || ["en"]; }; const getDefaultLocale = () => { return config.public.pdf.defaultLocale || "en"; }; return { generate, download, getAvailableLocales, getDefaultLocale }; }