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 使用
// 要打印的內(nèi)容
<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寬帶比較寬,打印出的東西顯示不完整,即使將內(nèi)容縮放到60%局服,也不會顯示完整鳖链。
因此,采取另外一種打印方法暴氏。
思路是,先將要打印的內(nèi)容轉(zhuǎn)換為圖片绣张,然后對圖片進行打印答渔。
- 使用 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文件,內(nèi)容為
// 打印類屬性芜飘、方法定義
/* 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() {
// 轉(zhuǎn)圖片打印
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);
});
},