Vue HTML to Paper

2.0.3 · maintenance · verified Sun Apr 19

Vue HTML to Paper is a plugin for Vue.js applications, primarily designed for Vue 3 (version 2.x), which provides functionality to print specific HTML elements to paper. It offers a convenient way to print custom content sections of a webpage, bypassing the default browser print behavior for entire pages. The plugin integrates into a Vue application by registering a global method, `$htmlToPaper`, accessible from any component, and can also be used via a `v-html-to-paper` directive. It supports comprehensive print customization, including setting window names, specific print window features (like fullscreen or scrollbars), and the inclusion of external stylesheets for consistent styling in print output. While the current stable version 2.0.3 is built for Vue 3, a version 1.x branch exists for Vue 2 applications, though its support is now considered low priority. The package's release cadence is irregular, with the latest major update (2.0.3) published approximately three years ago, suggesting a maintenance rather than active feature development phase. It ships with TypeScript types, aiding developers in building type-safe Vue projects.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to install `vue-html-to-paper` as a Vue 3 plugin and use the injected `$htmlToPaper` method in a component to print a specific HTML element with custom styles and options.

import { createApp } from 'vue';
import App from './App.vue';
import VueHtmlToPaper from 'vue-html-to-paper';

const app = createApp(App);

const options = {
  name: '_blank',
  specs: [
    'fullscreen=yes',
    'titlebar=yes',
    'scrollbars=yes'
  ],
  styles: [
    'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css',
    '/my-custom-print-styles.css' // Ensure this path is correct and accessible from /public or build output
  ],
  timeout: 1000, // default timeout before the print window appears
  autoClose: true, // if false, the window will not close after printing
  windowTitle: 'Custom Print Title'
};

app.use(VueHtmlToPaper, options);

app.mount('#app');

// In a Vue component (e.g., App.vue):
<template>
  <div>
    <div id="print-content">
      <h1>Document to Print</h1>
      <p>This is the content that will be sent to the printer.</p>
      <ul>
        <li>Item 1</li>
        <li>Item 2</li>
      </ul>
    </div>
    <button @click="printContent">Print this section</button>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue';

export default defineComponent({
  name: 'PrintExample',
  methods: {
    printContent(): void {
      this.$htmlToPaper('print-content', {
        name: 'my-custom-print-job',
        styles: ['/additional-component-styles.css']
      }, () => {
        console.log('Printing finished or cancelled');
      });
    }
  }
});
</script>

view raw JSON →