Vue PDF generation: Convert HTML to PDF with html2pdf.js in Vue.js
Table of contents
Nutrient DWS handles rendering, fonts, and CSS for you — start free with $5 of usage every month.
Need to generate PDFs in a Vue.js application? This guide compares four libraries:
- html2pdf.js(opens in a new tab) — Best for converting existing HTML elements into PDFs with minimal setup.
- jsPDF(opens in a new tab) — Best for generating PDFs dynamically via JavaScript.
- pdfmake(opens in a new tab) — Best for building complex, structured layouts.
- Nutrient Web SDK — A PDF SDK with headless generation, form support, and annotation.
Introduction
Generating PDF documents is a common requirement in modern Vue.js applications — especially in use cases like invoices, reports, dashboards, and printable forms. Users often need to export content as downloadable, shareable, or printable PDFs with a single click.
Vue works well with several JavaScript libraries that handle client-side PDF generation. These tools convert DOM elements, render canvases, or define structured layouts directly in the browser, with no server required.
This guide covers:
- How to use html2pdf.js to export Vue components to PDF
- How it compares to other options like jsPDF, pdfmake, and Nutrient Web SDK
- Example Vue components using each tool
If you prefer a video tutorial, watch the step-by-step walkthrough below.
Choosing the right Vue PDF library
Here’s a side-by-side comparison of popular tools for generating PDFs in Vue.js:
| Library | Best for | Ease of use | Flexibility | Documentation |
|---|---|---|---|---|
| html2pdf.js | Quick export of static HTML content | Easy | Low–Moderate | Good |
| jsPDF | Programmatic PDF generation via JavaScript | Moderate | High | Extensive |
| pdfmake | Structured multipage layouts with styles | Moderate | High | Good |
| Nutrient SDK | Full-featured client-side PDF workflows | Easy–Advanced | Very High | Extensive |
Overview of each library
The sections below describe what each library does and where it fits best.
html2pdf.js
html2pdf.js(opens in a new tab) is a lightweight library that captures existing HTML using html2canvas(opens in a new tab) and converts it to PDF via jsPDF(opens in a new tab). It suits exporting static Vue component content like receipts, confirmations, or print-ready sections.
jsPDF
jsPDF(opens in a new tab) is a library for creating PDFs from scratch using JavaScript. It gives you fine control over layout, fonts, tables, and dynamic content, and it suits apps that generate reports or data-driven documents programmatically.
pdfmake
Designed for applications that require declarative layout and complex structure, pdfmake(opens in a new tab) lets you define content using JSON-like objects. It supports multipage formatting, custom fonts, table styles, and automatic pagination.
Nutrient Web SDK
Nutrient Web SDK is an SDK built for Vue.js and modern web apps. It supports:
- Generating PDFs from Word (DOCX) templates, PDF templates, and images
- Merging, redacting, and manipulating PDFs
- Headless PDF generation (no UI required)
- Embedding a customizable PDF viewer with annotation, signing, and form support
Nutrient Web SDK runs client-side and requires no backend setup. It suits single-page applications (SPAs), enterprise platforms, and apps that need both document generation and interaction. To convert HTML to PDF on a server instead, use the HTML-to-PDF API.
Learn more in the Web SDK PDF generation guides.
Prerequisites
To follow this Vue PDF tutorial, you should be familiar with Vue.js(opens in a new tab). If you’re new to Vue, take a moment to review the Vue.js guides(opens in a new tab) before continuing.
Create a Vue.js project
Start by scaffolding a new Vue application using the official tool:
npm create vue@latestThis launches an interactive setup. Enter vue-pdf-example as the project name and choose JavaScript for the examples below. TypeScript and advanced testing tools aren’t required.
For example, you can skip Vue Router, Pinia, Vitest, and E2E testing. Optionally, you may enable Prettier and JSX support.
Once complete, navigate into your project directory and install its dependencies:
cd vue-pdf-examplenpm installWhat is html2pdf.js?
html2pdf.js is a lightweight library that converts HTML elements into downloadable PDFs by capturing them as canvases and passing the output to jsPDF.
The PDF pages contain images of the captured content, so the text isn’t selectable or searchable. Large documents can also exceed browser canvas limits and produce blank output. See the html2pdf.js limitations(opens in a new tab). For selectable text, use jsPDF’s text methods or pdfmake’s document definitions, or evaluate the HTML-to-PDF API for server-side conversion.
Why use it in Vue.js?
- Minimal setup and integration
- Best for converting static components (e.g. invoice sections, confirmation pages)
- Works well with Vue’s template system
The next section shows how to use html2pdf.js in a Vue component to generate your first PDF.
How to generate a PDF in Vue.js using html2pdf.js
This section shows how to convert an image (or any HTML element) to a PDF using html2pdf.js inside a Vue.js component.
1. Install html2pdf.js
Install the html2pdf.js library via npm:
npm install html2pdf.jsThe examples in this post were tested in November 2026 with Vue 3.5, Vite 8, html2pdf.js 0.14.0, jsPDF 4.2.1, and pdfmake 0.3.13. If you hit rendering issues with a different version, check the html2pdf.js GitHub repository(opens in a new tab) for known issues.
2. Setting up your Vue component
Update your App.vue file to include the content you want to convert and a button to trigger PDF generation. For this example, you’ll convert a simple image:
<template> <div id="app" ref="document"> <div id="element-to-convert"> <center> <img width="400" src="/logo.png" /> </center> </div> <button @click="exportToPDF">Export to PDF</button> </div></template>
<style>#app { margin-top: 60px; text-align: center;}</style>Make sure logo.png is placed inside your public/ folder:
my-vue-project/├── public/│ └── logo.png ← put image hereThis template includes a centered image and a button that will trigger the PDF export when clicked. The ref="document" is optional but can be useful for targeting the component in your script.
3. Adding PDF generation logic
In your <script> section, import html2pdf.js and define the PDF generation method:
<script>import html2pdf from "html2pdf.js";
export default { name: "App", methods: { exportToPDF() { html2pdf(document.getElementById("element-to-convert"), { margin: 1, filename: "generated-pdf.pdf", }); }, },};</script>You can customize PDF output using additional options such as margin, image, pagebreak, and jsPDF settings. See the html2pdf.js options documentation(opens in a new tab) for details.
The full example
Here’s how your complete App.vue file should look:
<template> <div id="app" ref="document"> <div id="element-to-convert"> <center> <img width="400" src="/logo.png" /> </center> </div> <button @click="exportToPDF">Export to PDF</button> </div></template>
<script>import html2pdf from "html2pdf.js";
export default { name: "App", methods: { exportToPDF() { html2pdf(document.getElementById("element-to-convert"), { margin: 1, filename: "generated-pdf.pdf", }); }, },};</script>
<style>#app { margin-top: 60px; text-align: center;}</style>Run your Vue.js app with:
npm run dev 
You’ll see a page with an image and a button. Clicking Export to PDF will generate and download a PDF containing the image.
Using jsPDF for dynamic PDF generation in Vue
jsPDF(opens in a new tab) is a library that enables programmatic PDF creation directly in the browser using JavaScript. It’s well-suited for Vue.js applications that need to dynamically generate content like reports, invoices, or data exports based on user input or runtime data.
Unlike html2pdf.js, which captures and converts existing HTML, jsPDF lets you build PDFs from scratch using methods like .text() and .addImage(). For tables, add the separate jspdf-autotable plugin.
Key features
- Define layout using code (coordinates, sizes, font styles)
- Add dynamic elements like charts, tables, or inputs
- Export and save with a customizable filename
- Suited to data-driven applications
How to use jsPDF for PDF generation in Vue
Install jsPDF
Terminal window npm install jspdfImport and use it in your Vue component
Create an instance of jsPDF and use its methods to add content and customize your PDF document. For example, you can add text to the PDF and then save it:
import jsPDF from "jspdf";
const doc = new jsPDF();
// Add text to the PDF.doc.text("Hello world!", 10, 10);
// Save the PDF.doc.save("document.pdf");Here’s a complete example of using jsPDF in a Vue component to generate and download a PDF:
<template> <div> <button @click="generatePDF">Generate PDF</button> </div></template>
<script>import jsPDF from 'jspdf';
export default { name: 'PDFGenerator', methods: { generatePDF() { // Create a new jsPDF instance. const doc = new jsPDF();
// Add text to the PDF. doc.text("Hello world!", 10, 10);
// Save the PDF with a filename. doc.save("document.pdf"); }, },};</script>Using pdfmake for structured PDF layouts in Vue
pdfmake(opens in a new tab) is designed for developers who need to build structured, styled PDFs. It uses a declarative syntax where you define content in a docDefinition object, which suits generating consistent layouts like tables, multicolumn pages, and styled text blocks.
Unlike html2pdf.js, which converts rendered HTML, pdfmake gives you fine-grained control by letting you define content in a JSON-like structure. In Vue (especially with Vite), you’ll need to import it carefully due to its CommonJS structure and font system.
Key features
- Create multipage layouts with headers, footers, and tables
- Define font styles, alignment, spacing, and page breaks
- Uses a JSON-like format that’s easy to serialize or generate dynamically
- Suited to form-driven apps or document-heavy workflows
Creating PDFs in Vue with pdfmake
This section shows how to integrate pdfmake into a Vue 3 project using the Options API(opens in a new tab).
Step 1 — Install pdfmake
First, install the pdfmake package using npm:
npm install pdfmakeStep 2 — Create a Vue component with a button
Build a simple component that lets the user click a button to generate a PDF:
<template> <div> <button @click="generatePDF">Generate PDF</button> </div></template>Step 3 — Dynamically import pdfmake and its fonts
This example imports pdfmake and vfs_fonts when the button is clicked. In a Vite(opens in a new tab) build, dynamic imports let the PDF engine and fonts load separately from the initial bundle. They’re a loading choice, not a compatibility requirement; pdfmake also supports static imports(opens in a new tab).
<script>export default { name: 'PDFGenerator', methods: { async generatePDF() { // Load the main PDF engine. const pdfMakeModule = await import('pdfmake/build/pdfmake'); // Load the default Roboto font definitions. const pdfFonts = await import('pdfmake/build/vfs_fonts');
// Set the virtual font system (vfs). pdfMakeModule.default.vfs = pdfFonts.default;
// Define document content. const docDefinition = { content: 'This is a sample PDF generated in Vue using pdfmake.', };
// Generate and download the PDF. pdfMakeModule.default.createPdf(docDefinition).download('sample.pdf'); }, },};</script>Clicking the Generate PDF button will dynamically load pdfmake, register the fonts, create a PDF from the defined content, and trigger its download as sample.pdf.
Summary
html2pdf.js(opens in a new tab) generates PDFs from HTML with a few lines of code, which makes it a good fit for simple applications.
If your workflows are more complex, or you expect them to become so, Nutrient can help. For example, if you’re interested in automating the PDF generation process, refer to our PDF generator API, which enables you to generate PDF files from HTML templates. You can style the CSS, add unique images and fonts, and persist your headers and footers across multiple pages.
To display PDFs in a customizable viewer with features like annotating PDFs or digitally signing them, see our Vue.js PDF viewer tutorial or Document Engine.
Related tutorials
If you’re working across different frameworks or platforms, these guides may also be useful:
- How to generate a PDF with JavaScript
- How to convert HTML to PDF using React
- How to generate PDF reports from HTML in C#
- How to convert HTML to PDF using wkhtmltopdf and Python
Related Vue.js guides
Framework comparisons
FAQ
If your Vue app needs to generate large or multipage PDFs, libraries like pdfmake or Nutrient Web SDK are better suited. They offer efficient rendering and pagination controls that help manage memory usage and layout complexity.
Yes. Libraries like pdfmake, jsPDF, and Nutrient allow you to embed custom fonts. You’ll need to register the font definitions with the library (typically via vfs) or use the SDK’s font-loading API, depending on the tool.
While basic libraries like html2pdf.js don’t support redaction, Nutrient Web SDK includes built-in tools for redacting, encrypting, and securing PDF files entirely on the client side, which suits applications that handle private or regulated content.
Yes. Since all the libraries featured — html2pdf.js, jsPDF, pdfmake, and Nutrient Web SDK — run in the browser, they can generate PDFs offline without server access. Just make sure fonts and assets are bundled locally.
Most libraries work on mobile browsers, but performance varies. html2pdf.js and jsPDF can struggle with large documents or images on lower-end devices, so test on the devices your users have.
Yes. Libraries like jsPDF and pdfmake allow you to open PDFs in a new browser tab. Nutrient Web SDK also includes a built-in PDF viewer with annotation and interaction features directly inside your Vue app.