Vue HTML2Canvas Mixin

0.0.4 · abandoned · verified Sun Apr 19

vue-html2canvas is a Vue.js mixin designed for Vue 2 applications, providing a streamlined integration of the `html2canvas` library. This package facilitates the capture of HTML elements as images directly within Vue components by exposing a `$html2canvas` method on the Vue instance. With its last stable release at version `0.0.4`, published over seven years ago, the package is considered abandoned and is not compatible with Vue 3. Its purpose is to simplify the process of client-side rendering of DOM elements to a canvas or data URL, abstracting the direct `html2canvas` setup. While convenient for legacy Vue 2 projects, developers building new applications are advised to seek more modern, actively maintained alternatives that support Vue 3 and modern JavaScript practices.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to install `vue-html2canvas` as a Vue 2 plugin and use the `$html2canvas` method to convert a referenced DOM element into a data URL image, displaying it in the component.

import Vue from 'vue';
import VueHtml2Canvas from 'vue-html2canvas';

Vue.use(VueHtml2Canvas);

export default {
  data() {
    return {
      output: null
    }
  },
  methods: {
    async print() {
      const el = this.$refs.printMe;
      const options = {
        type: 'dataURL',
        // Example: Add a custom background color if the element has transparent areas
        backgroundColor: '#ffffff'
      }
      // Ensure the component is mounted before calling print
      if (el) {
        this.output = await this.$html2canvas(el, options);
      } else {
        console.error('Element to print not found.');
      }
    }
  },
  mounted() {
    // Automatically print on mount for demonstration
    this.print();
  },
  template: `
    <div>
      <div ref="printMe" style="padding: 20px; border: 1px solid #ccc; background-color: #f9f9f9;">
        <h1>Hello, Vue HTML2Canvas!</h1>
        <p>This content will be converted to an image.</p>
        <ul>
          <li>Item 1</li>
          <li>Item 2</li>
        </ul>
        <button @click="print">Re-print</button>
      </div>
      <div v-if="output">
        <h2>Generated Image:</h2>
        <img :src="output" alt="Generated Image" style="max-width: 100%; border: 1px dashed #eee; margin-top: 20px;"/>
      </div>
      <div v-else>
        <p>Generating image...</p>
      </div>
    </div>
  `
}

view raw JSON →