使用printJS、html2Canvas实现打印功能

js文件如下:

import html2Canvas from "html2canvas";
import printJS from 'print-js'
export default {
  install(Vue /*options*/) {
    Vue.prototype.pintDom = function (dom, title) { // 转图片打印
      html2Canvas(document.querySelector(dom), {
        allowTaint: false,
        useCORS: false,
        height: document.querySelector(dom).outerHeight,
        width: document.querySelector(dom).outerWidth,
        windowWidth: document.body.scrollWidth,
        windowHeight: document.body.scrollHeight
      }).then((canvas) => {
        const url = canvas.toDataURL()
        printJS({
          printable: url,
          type: 'image',
          documentTitle: title
        })
      })
    }
  }
};

调用如下:

<el-button @click="pintDom('#pintBox', '打印标题')"></el-button>

你可能感兴趣的:(javascript,vue.js,vue)