Vue 使用print.js实现前端打印功能

1.新建plugins文件夹,文件夹下面新建print,新建Print.js  源码地址(https://github.com/xyl66/vuePlugs_printjs)

// 打印类属性、方法定义
/* eslint-disable */
const Print = function (dom, options) {
  if (!(this instanceof Print)) return new Print(dom, options);

  this.options = this.extend({
    'noPrint': '.no-print'
  }, options);

  if ((typeof dom) === "string") {
    this.dom = document.querySelector(dom);
  } else {
    this.isDOM(dom)
    this.dom = this.isDOM(dom) ? dom : dom.$el;
  }

  this.init();
};
Print.prototype = {
  init: function () {
    var content = this.getStyle() + this.getHtml();
    this.writeIframe(content);
  },
  extend: function (obj, obj2) {
    for (var k in obj2) {
      obj[k] = obj2[k];
    }
    return obj;
  },

  getStyle: function () {
    var str = "",
      styles = document.querySelectorAll('style,link');
    for (var i = 0; i < styles.length; i++) {
      str += styles[i].outerHTML;
    }
    str += "<style>" + (this.options.noPrint ? this.options.noPrint : '.no-print') + "{display:none;}</style>";
    str += "<style>html,body,div{height: auto!important;}</style>";
    return str;
  },

  getHtml: function () {
    var inputs = document.querySelectorAll('input');
    var textareas = document.querySelectorAll('textarea');
    var selects = document.querySelectorAll('select');

    for (var k in inputs) {
      if (inputs[k].type == "checkbox" || inputs[k].type == "radio") {
        if (inputs[k].checked == true) {
          inputs[k].setAttribute('checked', "checked")
        } else {
          inputs[k].removeAttribute('checked')
        }
      } else if (inputs[k].type == "text") {
        inputs[k].setAttribute('value', inputs[k].value)
      }
    }

    for (var k2 in textareas) {
      if (textareas[k2].type == 'textarea') {
        textareas[k2].innerHTML = textareas[k2].value
      }
    }

    for (var k3 in selects) {
      if (selects[k3].type == 'select-one') {
        var child = selects[k3].children;
        for (var i in child) {
          if (child[i].tagName == 'OPTION') {
            if (child[i].selected == true) {
              child[i].setAttribute('selected', "selected")
            } else {
              child[i].removeAttribute('selected')
            }
          }
        }
      }
    }

    return this.dom.outerHTML;
  },



  // getHtml: function () {
  //   var inputs = document.querySelectorAll('input');
  //   var textareas = document.querySelectorAll('textarea');
  //   var selects = document.querySelectorAll('select');
  //   for (var k = 0; k < inputs.length; k++) {
  //     if (inputs[k].type == "checkbox" || inputs[k].type == "radio") {
  //       if (inputs[k].checked == true) {
  //         inputs[k].setAttribute('checked', "checked")
  //       } else {
  //         inputs[k].removeAttribute('checked')
  //       }
  //     } else if (inputs[k].type == "text") {
  //       inputs[k].setAttribute('value', inputs[k].value)
  //     } else {
  //       inputs[k].setAttribute('value', inputs[k].value)
  //     }
  //   }
  //   for (var k2 = 0; k2 < textareas.length; k2++) {
  //     if (textareas[k2].type == 'textarea') {
  //       textareas[k2].innerHTML = textareas[k2].value
  //     }
  //   }
  //   for (var k3 = 0; k3 < selects.length; k3++) {
  //     if (selects[k3].type == 'select-one') {
  //       var child = selects[k3].children;
  //       for (var i in child) {
  //         if (child[i].tagName == 'OPTION') {
  //           if (child[i].selected == true) {
  //             child[i].setAttribute('selected', "selected")
  //           } else {
  //             child[i].removeAttribute('selected')
  //           }
  //         }
  //       }
  //     }
  //   }
  //   // 包裹要打印的元素
  //   // fix: https://github.com/xyl66/vuePlugs_printjs/issues/36
  //   return this.wrapperRefDom(this.dom).outerHTML;
  // },

  // 向父级元素循环,包裹当前需要打印的元素
  // 防止根级别开头的 css 选择器不生效
  // wrapperRefDom: function (refDom) {
  //   let prevDom = null;
  //   let currDom = refDom;
  //   while (currDom && currDom.tagName.toLowerCase() !== 'body') {
  //     if (prevDom) {
  //       let element = currDom.cloneNode(false)
  //       element.appendChild(prevDom)
  //       prevDom = element;
  //     } else {
  //       prevDom = currDom.cloneNode(true);
  //     }
  //
  //     currDom = currDom.parentElement;
  //   }
  //
  //   return this.dom.outerHTML;
  //   return currDom.tagName.toLowerCase() === 'body' ? currDom : prevDom;
  // },

  writeIframe: function (content) {
    var w, doc, iframe = document.createElement('iframe'),
      f = document.body.appendChild(iframe);
    iframe.id = "myIframe";
    //iframe.style = "position:absolute;0;height:0;top:-10px;left:-10px;";
    iframe.setAttribute('style', 'position:absolute;0;height:0;top:-10px;left:-10px;');
    w = f.contentWindow || f.contentDocument;
    doc = f.contentDocument || f.contentWindow.document;
    doc.open();
    doc.write(content);
    doc.close();
    var _this = this
    iframe.onload = function(){
      _this.toPrint(w);
      setTimeout(function () {
        document.body.removeChild(iframe)
      }, 100)
    }
  },

  toPrint: function (frameWindow) {
    try {
      setTimeout(function () {
        frameWindow.focus();
        try {
          if (!frameWindow.document.execCommand('print', false, null)) {
            frameWindow.print();
          }
        } catch (e) {
          frameWindow.print();
        }
        frameWindow.close();
      }, 10);
    } catch (err) {
    }
  },
  isDOM: (typeof HTMLElement === 'object') ?
    function (obj) {
      return obj instanceof HTMLElement;
    } :
    function (obj) {
      return obj && typeof obj === 'object' && obj.nodeType === 1 && typeof obj.nodeName === 'string';
    }
};
const MyPlugin = {}
MyPlugin.install = function (Vue, options) {
  // 4. 添加实例方法
  Vue.prototype.$print = Print
}
export default MyPlugin

2.main.js 注册模块

// 导入打印组件
import Print from './plugins/print/Print'
Vue.use(Print)

3.新建print.vue组件

<template>
  <div class="mask">
    <table class="tab" cellpadding="1" cellspacing="0" border="1" ref="print">
      <tr>
        <th>河道名称</th>
        <th>检查地址</th>
        <th>巡查时间</th>
        <th>检查内容</th>
        <th>处理措施</th>
        <th>检查情况</th>
      </tr>
    </table>

    <div class="btn no-print">
      <el-row>
        <el-col :span="12">
          <el-button class="no-print" type="primary" icon="el-icon-circle-check"  @click="printContext">打印</el-button>
        </el-col>
        <el-col :span="12">
          <el-button class="no-print" @click="goBack" >返回</el-button>

        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {

    }
  },
  created () {

  },
  methods: {
    // 打印内容
    printContext () {
      this.$print(this.$refs.print)
    },
    goBack () {
      this.$router.go(-1)
    }
  }
}
</script>

<style type='text/css'>
  /*去除页眉页脚,默认横向打印*/
  @page {
    color: #000000 !important;
    size: landscape;
    margin: 1mm 2mm 1mm 1mm;
  }

  @media print {
    #executedailyPrint {
       1000px !important;
      padding-top: 100px;
    }

    #executedailyPrint .con-box {
       1000px !important;
      box-shadow: none;
    }

    #executedailyPrint .cont-list {
       1000px !important;
    }

    #executedailyPrint .tab {
       1000px !important;
      margin-top: 10px !important;
    }

    #executedailyPrint tr th {
      font-size: 16px !important;
      line-height: 40px !important;
    }

    #executedailyPrint tr td {
      font-size: 16px !important;
      line-height: 40px !important;
      height: 40px !important;
    }

    #executedailyPrint div {
      font-size: 14px !important;
    }

    #executedailyPrint span {
      font-size: 14px !important;
    }
  }
</style>

5. printContext 方法 

this.$print(this.$refs.print) 为实现打印方法
原文地址:https://www.cnblogs.com/WorldEye/p/14140308.html