fix: 优化打印方法(Echarts

This commit is contained in:
zepeng 2023-01-02 17:09:08 +08:00 committed by GitHub
parent d0ed3be827
commit 1f9fe0ef29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 37 deletions

View File

@ -13,8 +13,6 @@ const Print = function (dom, options?: object): PrintFunction {
styleStr: "", styleStr: "",
// Elements that need to dynamically get and set the height // Elements that need to dynamically get and set the height
setDomHeightArr: [], setDomHeightArr: [],
// Echart dom List
echartDomArr: [],
// Callback before printing // Callback before printing
printBeforeFn: null, printBeforeFn: null,
// Callback after printing // Callback after printing
@ -73,6 +71,8 @@ Print.prototype = {
const inputs = document.querySelectorAll("input"); const inputs = document.querySelectorAll("input");
const selects = document.querySelectorAll("select"); const selects = document.querySelectorAll("select");
const textareas = document.querySelectorAll("textarea"); const textareas = document.querySelectorAll("textarea");
const canvass = document.querySelectorAll("canvas");
for (let k = 0; k < inputs.length; k++) { for (let k = 0; k < inputs.length; k++) {
if (inputs[k].type == "checkbox" || inputs[k].type == "radio") { if (inputs[k].type == "checkbox" || inputs[k].type == "radio") {
if (inputs[k].checked == true) { if (inputs[k].checked == true) {
@ -108,6 +108,15 @@ Print.prototype = {
} }
} }
for (let k4 = 0; k4 < canvass.length; k4++) {
const imageURL = canvass[k4].toDataURL("image/png");
const img = document.createElement("img");
img.src = imageURL;
img.setAttribute("style", "max-width: 100%;");
img.className = "isNeedRemove";
canvass[k4].parentNode.insertBefore(img, canvass[k4].nextElementSibling);
}
return this.dom.outerHTML; return this.dom.outerHTML;
}, },
/** /**
@ -130,6 +139,12 @@ Print.prototype = {
doc.open(); doc.open();
doc.write(content); doc.write(content);
doc.close(); doc.close();
const removes = document.querySelectorAll(".isNeedRemove");
for (let k = 0; k < removes.length; k++) {
removes[k].parentNode.removeChild(removes[k]);
}
// eslint-disable-next-line @typescript-eslint/no-this-alias // eslint-disable-next-line @typescript-eslint/no-this-alias
const _this = this; const _this = this;
iframe.onload = function (): void { iframe.onload = function (): void {
@ -137,42 +152,16 @@ Print.prototype = {
if (_this.conf.printBeforeFn) { if (_this.conf.printBeforeFn) {
_this.conf.printBeforeFn({ doc }); _this.conf.printBeforeFn({ doc });
} }
_this.toPrint(w);
_this.drawEchartImg(doc).then(() => { setTimeout(function () {
_this.toPrint(w); document.body.removeChild(iframe);
setTimeout(function () { // After popup, callback
document.body.removeChild(iframe); if (_this.conf.printDoneCallBack) {
// After popup, callback _this.conf.printDoneCallBack();
if (_this.conf.printDoneCallBack) { }
_this.conf.printDoneCallBack(); }, 100);
}
}, 100);
});
}; };
}, },
/**
* echarts printing
* @param {Object} doc iframe window
*/
drawEchartImg(doc): Promise<void> {
return new Promise<void>(resolve => {
if (this.conf.echartDomArr && this.conf.echartDomArr.length > 0) {
this.conf.echartDomArr.forEach(e => {
const dom = doc.querySelector("#" + e.$el.id);
const img = new Image();
const w = dom.offsetWidth + "px";
const H = dom.offsetHeight + "px";
img.style.width = w;
img.style.height = H;
img.src = e.imgSrc;
dom.innerHTML = "";
dom.appendChild(img);
});
}
resolve();
});
},
/** /**
Print Print
*/ */

View File

@ -156,7 +156,7 @@ const tableData: User[] = [
}" }"
> >
<p class="font-medium pt-1">Echart</p> <p class="font-medium pt-1">Echart</p>
<Line class="echart" style="margin: 0 auto; height: 300px" /> <Line class="echart" style="margin: 0 auto; height: 88%" />
</el-col> </el-col>
<el-col <el-col