Vue 3 Component for Printing

1.5.1 · active · verified Sun Apr 19

Vue-to-print is a JavaScript library providing a Vue 3 component and composable hook designed to facilitate printing the content of any DOM element or Vue component. It aims to offer a consistent API experience with its React counterpart, ReactToPrint. Currently stable at version 1.5.1, the library demonstrates an active release cadence, with several updates including minor features and bug fixes throughout the last year. Key differentiators include its direct lineage from ReactToPrint, ensuring a familiar API for developers transitioning between frameworks, and robust TypeScript support, making it suitable for modern Vue 3 applications that prioritize type safety. It focuses on rendering content to a new print window while retaining original CSS styles.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates printing a specific DOM element's content using the `usePrint` composable and a ref.

import { defineComponent, ref } from 'vue';
import { Print, usePrint } from 'vue-to-print';

export default defineComponent({
  components: { Print },
  setup() {
    const printContentRef = ref(null);
    const { print, isPrinting } = usePrint(() => printContentRef.value);

    return {
      printContentRef,
      print,
      isPrinting,
    };
  },
  template: `
    <div>
      <h1>Document to Print</h1>
      <div ref="printContentRef" style="padding: 20px; border: 1px solid #ccc;">
        <h2>Printable Section</h2>
        <p>This content will be sent to the printer.</p>
        <ul>
          <li>Item 1</li>
          <li>Item 2</li>
          <li>Item 3</li>
        </ul>
        <p>Current date: {{ new Date().toLocaleDateString() }}</p>
      </div>
      <button @click="print" :disabled="isPrinting" style="margin-top: 20px; padding: 10px 20px;">
        {{ isPrinting ? 'Printing...' : 'Print Document' }}
      </button>
      <p v-if="isPrinting">Preparing document for print...</p>
    </div>
  `,
});

view raw JSON →