vue使⽤print.js实现前端打印功能
最近新接了⼀个需求,想要在前端实现打印功能,最开始试了window.print()这个⽅法,结果发现我想打印的图⽚它只能打印图⽚中的⼀部分,打印的不完整,如下图
调整了缩放⽐例之后发现还是不可以,后来⼜尝试了vue⾥⾯的⼀个插件vue-print-nb,但是当所有的东
西都配置好之后,点击打印的时候竟然报错,⼤概意思是说我的css样式获取不到
然后我试了最后⼀种⽅法,亲测好使print.js
1.⾸先src下新建⽂件夹(只是⽅便管理,你放在其他位置也可以)
2.附上print.js的代码
你也可以直接去这⾥下载源码
// 打印类属性、⽅法定义
/* eslint-disable */
const Print = function (dom, options) {
if (!(this instanceof Print)) return new Print(dom, options);
novthis.options = d({
'noPrint': '.no-print'
}, options);
gam}, options);
if ((typeof dom) === "string") {
this.dom = document.querySelector(dom);
} el {
this.isDOM(dom)
this.dom = this.isDOM(dom) ? dom : dom.$el;迎接新生
}
this.init();
};
Print.prototype = {
init: function () {
var content = Style() + Html();
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>" + (Print ? Print : '.no-print') + "{display:none;}</style>";
return str;
},
getHtml: function () {
var inputs = document.querySelectorAll('input');
var textareas = document.querySelectorAll('textarea');dq
var lects = document.querySelectorAll('lect');
for (var k = 0; k < inputs.length; k++) {
if (inputs[k].type == "checkbox" || inputs[k].type == "radio") {
if (inputs[k].checked == true) {
blast2goinputs[k].tAttribute('checked', "checked")
} el {
inputs[k].removeAttribute('checked')
}
} el if (inputs[k].type == "text") {
inputs[k].tAttribute('value', inputs[k].value)
} el {
inputs[k].tAttribute('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 < lects.length; k3++) {
if (lects[k3].type == 'lect-one') {
var child = lects[k3].children;
for (var i in child) {
if (child[i].tagName == 'OPTION') {
if (child[i].lected == true) {
child[i].tAttribute('lected', "lected")
child[i].tAttribute('lected', "lected")
} el {
child[i].removeAttribute('lected')
}
}
}
}
}
return this.dom.outerHTML;
},
writeIframe: function (content) {
var w, doc, iframe = ateElement('iframe'),
f = document.body.appendChild(iframe);
iframe.id = "myIframe";
//iframe.style = "position:absolute;width:0;height:0;top:-10px;left:-10px;";
iframe.tAttribute('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.clo();
var _this = this
_Print(w);
tTimeout(function () {
veChild(iframe)
}, 100)
}
},
toPrint: function (frameWindow) {
try {
tTimeout(function () {
frameWindow.focus();
try {
if (!Command('print', fal, null)) {
英语作文常用句型
frameWindow.print();
}
} catch (e) {
frameWindow.print();
}
frameWindow.clo();
}, 10);
} catch (err) {
console.log('err', err);
}
},
isDOM: (typeof HTMLElement === 'object') ?
function (obj) {
deanreturn obj instanceof HTMLElement;
} :
function (obj) {
return obj && typeof obj === 'object' && deType === 1 && deName === 'string'; }
};
const MyPlugin = {}
MyPlugin.install = function (Vue, options) {
// 4. 添加实例⽅法
Vue.prototype.$print = Print
会计从业资格考试书
}
export default MyPlugin托福拼分
3.在main.js⾥⾯引⼊
4.最后就可以在⾃⼰的页⾯使⽤了
<ction ref="print" class="recordImg" id="printRecord">
<img :src="image" alt="">
</ction>
<span slot="footer" class="dialog-footer">
<el-button @click="recordVisible = fal">取消</el-button>
<el-button type="primary" @click="PrintRow">打印</el-button>
</span>
//打印
PrintRow(index, row){
this.$print(this.$refs.print) // 使⽤
},
好啦,到这⾥就可以啦,如果有⼩伙伴有更好的办法或者问题欢迎在下⾯留⾔讨论