Vue.js HTML to PDF Generator

1.8.0 · active · verified Tue Apr 21

vue-html2pdf is a Vue.js wrapper component that facilitates client-side PDF generation directly from HTML content within Vue components, leveraging the robust capabilities of the html2pdf.js library internally. The package allows developers to easily convert any Vue component's template or arbitrary HTML element into a downloadable PDF document. Currently, the stable version is 1.8.0. While there isn't a strict release cadence specified, updates address breaking changes and new features. Key differentiators include its simple component-based API, extensive props for controlling PDF quality, format, orientation, and content width, and a comprehensive event system for managing the entire generation lifecycle, including pagination and pre-download hooks. It provides specific integration guidance for Nuxt.js projects and offers fine-grained control over the underlying html2pdf.js library through event arguments.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import and use the `VueHtml2pdf` component in a Vue application, trigger PDF generation programmatically, and listen to key lifecycle events like progress and completion.

import VueHtml2pdf from 'vue-html2pdf';

export default {
  components: {
    VueHtml2pdf
  },
  methods: {
    generateReport() {
      // This method is called to trigger PDF generation.
      // It accesses the component via its ref and calls the generatePdf method.
      this.$refs.html2Pdf.generatePdf();
    },
    onProgress(event) {
      console.log(`PDF generation progress: ${event}%`);
    },
    hasStartedGeneration() {
      console.log('PDF generation has started.');
    },
    hasGenerated(event) {
      console.log('PDF has been successfully generated.', event);
    }
  },
  template: `
    <div>
      <button @click="generateReport">Generate PDF</button>
      <vue-html2pdf
        :show-layout="false"
        :float-layout="true"
        :enable-download="true"
        :preview-modal="true"
        :paginate-elements-by-height="1400"
        filename="my-document"
        :pdf-quality="2"
        :manual-pagination="false"
        pdf-format="a4"
        pdf-orientation="landscape"
        pdf-content-width="800px"
        @progress="onProgress($event)"
        @hasStartedGeneration="hasStartedGeneration()"
        @hasGenerated="hasGenerated($event)"
        ref="html2Pdf"
      >
        <section slot="pdf-content">
          <h1>Hello, Vue-HTML2PDF!</h1>
          <p>This is a sample content that will be converted into a PDF document.</p>
          <img src="https://via.placeholder.com/300x150.png?text=Image" alt="Placeholder Image" style="width: 300px; height: 150px;" />
          <p>More content here to potentially trigger pagination if height exceeds 1400px.</p>
        </section>
      </vue-html2pdf>
    </div>
  `
}

view raw JSON →