Vue 3 Component for Printing
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
-
TypeError: Cannot read properties of undefined (reading 'toValue')
cause Attempting to use `toValue()` on print event properties that no longer require it after a bug fix in v1.3.1.fixUpdate `vue-to-print` to version `1.3.1` or newer. Remove explicit `toValue()` calls from print event handlers, as the values are now directly accessible. -
npm ERR! ERESOLVE unable to resolve dependency tree
cause Conflict with `@vue/composition-api` dependency in older `vue-to-print` versions and your project's Vue setup.fixUpgrade `vue-to-print` to version `1.2.0-alpha.3` or later. If the issue persists, ensure your project's `package.json` correctly specifies `vue` as a peer dependency matching the library's requirements (`^3.0.0`).
Warnings
- breaking Prior to v1.2.0-alpha.3, the package included `@vue/composition-api` internally, which could cause Vue version conflicts during installation or runtime, especially in projects not using a `vue-demi` setup.
- gotcha In versions prior to v1.3.1, accessing print event values might have required using `toValue()` which was later fixed as unnecessary, suggesting potential incorrect usage or a bug in earlier implementations.
- gotcha Version 1.5.0 introduced 'AI assistant integration features' and new documentation formats (`llms.txt`, `llms-full.txt`). This feature addition is unusual for a printing utility and might introduce unexpected dependencies or expanded scope beyond printing functionalities.
- gotcha The package internally augmented `vue` rather than `@vue/runtime-core` prior to v1.1.1. This could lead to type inference issues or conflicts for TypeScript users who had custom type augmentations or specific runtime core imports.
Install
-
npm install vue-to-print -
yarn add vue-to-print -
pnpm add vue-to-print
Imports
- usePrint
const { usePrint } = require('vue-to-print')import { usePrint } from 'vue-to-print' - Print
import Print from 'vue-to-print'
import { Print } from 'vue-to-print' - PrintPlugin
import { PrintPlugin } from 'vue-to-print'import PrintPlugin from 'vue-to-print'
Quickstart
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>
`,
});