共享一下几个后台办理体系比较常用的插件:下载、打印
html2canvas介绍
html2canvas是在浏览器上对网页进行截图操作,实际上是操作DOM,这个插件也有好长时刻了,比较稳定,现在运用还没有遇到什么bug
jspdf介绍
假设下载出来是pdf文件,可以加上jspdf插件,会先通过html2canvas把页面转化成base64图片,再通过jspdf导出
装置
npm i html2canvas jspdf
或
yarn add html2canvas jspdf
运用
<template>
<div>
<h1 ref="toPdf">
导出区域
</h1>
<button @click="toPdfFn">导出pdf</button>
</div>
</template>
<script>
import html2canvas from "html2canvas"
import JSPDF from "jspdf"
export default {
methods:{
toPdfFn(){
this.htmlToPdf('文件名','时刻')
},
htmlToPdf(name,time){
let element = this.$refs.toPdf
html2canvas(element, {
logging: false
}).then(function(canvas) {
let pdf = new JSPDF("p", "mm", "a4") // A4纸,纵向
let ctx = canvas.getContext("2d")
let a4w = 190;
let a4h = 277 // A4大小,210mm x 297mm,四边各保存20mm的边距,显现区域190x277
let imgHeight = Math.floor(a4h * canvas.width / a4w) // 按A4显现份额换算一页图画的像素高度
let renderedHeight = 0
while (renderedHeight < canvas.height) {
let page = document.createElement("canvas")
page.width = canvas.width
page.height = Math.min(imgHeight, canvas.height - renderedHeight) // 或许内容不足一页
// 用getImageData取舍指定区域,并画到前面创建的canvas方针中
page.getContext("2d").putImageData(ctx.getImageData(0, renderedHeight, canvas.width, Math.min(imgHeight,
canvas.height - renderedHeight)), 0, 0)
pdf.addImage(page.toDataURL("image/jpeg", 1.0), "JPEG", 10, 10, a4w, Math.min(a4h, a4w * page.height /
page.width)) // 添加图画到页面,保存10mm边距
// 假设后边还有内容,添加一个空页
renderedHeight += imgHeight
if (renderedHeight < canvas.height) {
pdf.addPage()
}
}
pdf.save(name + time)
})
}
}
}
</script>
留心点: 1、能运用ref来获取html结构就用ref,尽量不运用id。假设运用的当地比较多可以挂载到vue实例上 2、导出的pdf空白状况:查看dom结构有没有获取到,还有就是css款式要写在导出区域内的元素中
printjs介绍
之前是运用vue-print-nb插件的,但是这个插件有点猫病,有时候会呈现款式跨域的问题,有时候又正常,后边在GitHub上找到的一个,用到现在也没呈现过什么问题
在utils文件里面创建一个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>";
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
printjs源码在这里
在main.js中注册
import Vue from "vue";
import print from "./src/utils/print.js";
Vue.use(print)
在需要的当地运用
<template>
<div>
<h1 ref="print">
<div>打印区域</div>
</h1>
<button @click="printFn">打印</button>
</div>
</template>
<script>
export default {
methods: {
printFn() {
//传入dom结构即可
this.$print(this.$refs.print);
},
},
};
</script>
<style lang="less" scoped>
@media print {
@page {
size: auto; //打印可以选择布局:横向,纵向
// size: landscape;//横向
// size: portrait;//纵向
margin: 23.5mm; //默许边距
}
}
</style>
留心:需运用ref获取dom节点,若直接通过id或class获取则webpack打包布置后打印内容为空
指定不打印区域方法
方法一. 添加no-print款式类
<div class="no-print">不要打印我</div>
方法二. 自定义类名
<div class="do-not-print-me-xxx">不要打印我</div>
this.$print(this.$refs.print,{'no-print':'.do-not-print-me-xxx'}) // 运用
终究环节,假设各位大佬有更好用的插件,可以共享一下,让小弟学习学习
后续也会持续更新