1.使用vue-print-nb插件打印
1.1 安装
npm install vue-print-nb --save
1.2 配置
在main.js文件中注册
import Print from 'vue-print-nb';
Vue.use(Print);
1.3 使用
// 要打印的内容
<Table :columns="sortList1" :data="docList" id="printMe"></Table>
<Button type="primary" icon="ios-print-outline" title="打印" v-print="'#printMe'">打印</Button> -->
2.vue-print-nb 打印不完整
用以上方法打印Table,如果Table宽带比较宽,打印出的东西显示不完整,即使将内容缩放到60%,也不会显示完整。
因此,采取另外一种打印方法。
思路是,先将要打印的内容转换为图片,然后对图片进行打印。
- 使用 html2Canvs + print-js 打印方法
3.1 安装html2canvas
npm install html2canvas --save
3.2 配置html2canvas
在main.js文件中注册
import html2canvas from 'html2canvas';
Vue.use(html2canvas);
3.3 新建print.js
在main.js父文件夹中,新建plugins文件,并在此文件夹下新建print文件夹,最后新建print.js文件,内容为
// 打印类属性、方法定义
/* 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;font-size:14px}</style>";
return str;
},
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')
}
}
}
}
}
return this.dom.outerHTML;
},
writeIframe: function (content) {
var w, doc, iframe = document.createElement('iframe'),
f = document.body.appendChild(iframe);
iframe.id = "myIframe";
//iframe.style = "position:absolute;width:0;height:0;top:-10px;left:-10px;";
iframe.setAttribute('style', 'position:absolute;width: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) {
console.log('err', 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
3.4 main.js引入print.js
import Print from './plugins/print/Print';
Vue.use(Print);
3.5 使用
<Button type="primary" icon="ios-print-outline" title="打印" @click="toImg">打印</Button>
<Table :columns="sortList1" :data="docList" id="print"></Table>
toImg() {
// 转图片打印
const htmlDom = document.getElementById('print');
html2canvas(htmlDom, {
allowTaint: false,
taintTest: true,
useCORS: true,
windowHeight: document.body.scrollHeight,
background: '#fff',
}).then(canvas => {
const url = canvas.toDataURL('image/jpeg', 1.0);
const myimg = document.createElement('img');
myimg.src = url;
this.$print(myimg);
});
},