From 1f9fe0ef298b14dff108f32855add322d252837c Mon Sep 17 00:00:00 2001 From: zepeng <75007029+yj-liuzepeng@users.noreply.github.com> Date: Mon, 2 Jan 2023 17:09:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E6=96=B9=E6=B3=95=EF=BC=88`Echarts`=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/print.ts | 61 ++++++++++++++++------------------------ src/views/able/print.vue | 2 +- 2 files changed, 26 insertions(+), 37 deletions(-) diff --git a/src/utils/print.ts b/src/utils/print.ts index c828f7cce..333f6feb3 100644 --- a/src/utils/print.ts +++ b/src/utils/print.ts @@ -13,8 +13,6 @@ const Print = function (dom, options?: object): PrintFunction { styleStr: "", // Elements that need to dynamically get and set the height setDomHeightArr: [], - // Echart dom List - echartDomArr: [], // Callback before printing printBeforeFn: null, // Callback after printing @@ -73,6 +71,8 @@ Print.prototype = { const inputs = document.querySelectorAll("input"); const selects = document.querySelectorAll("select"); const textareas = document.querySelectorAll("textarea"); + const canvass = document.querySelectorAll("canvas"); + for (let k = 0; k < inputs.length; k++) { if (inputs[k].type == "checkbox" || inputs[k].type == "radio") { 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; }, /** @@ -130,6 +139,12 @@ Print.prototype = { doc.open(); doc.write(content); 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 const _this = this; iframe.onload = function (): void { @@ -137,42 +152,16 @@ Print.prototype = { if (_this.conf.printBeforeFn) { _this.conf.printBeforeFn({ doc }); } - - _this.drawEchartImg(doc).then(() => { - _this.toPrint(w); - setTimeout(function () { - document.body.removeChild(iframe); - // After popup, callback - if (_this.conf.printDoneCallBack) { - _this.conf.printDoneCallBack(); - } - }, 100); - }); + _this.toPrint(w); + setTimeout(function () { + document.body.removeChild(iframe); + // After popup, callback + if (_this.conf.printDoneCallBack) { + _this.conf.printDoneCallBack(); + } + }, 100); }; }, - /** - * echarts printing - * @param {Object} doc iframe window - */ - drawEchartImg(doc): Promise { - return new Promise(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 */ diff --git a/src/views/able/print.vue b/src/views/able/print.vue index 3866fd7de..a80884189 100644 --- a/src/views/able/print.vue +++ b/src/views/able/print.vue @@ -156,7 +156,7 @@ const tableData: User[] = [ }" >

Echart

- +