项目背景
需求:VUE项目中,需要打印表格数据,超过一页(多页打印)
初始方案:使用vue-print-nb实现打印,无法实现多页打印,只能打印当前页,且数据超过一页后打印预览未显示。
最终方案:使用print.js实现多页打印
一、将下面代码保存为print.js文件放到src/utils文件目录下
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