From e00265b577119d4db378d719ad3b34196bd583fb Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Tue, 22 Sep 2020 10:35:59 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=B5=8B=E8=AF=95=E8=B7=9F=E8=B8=AA):=20?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E8=AE=A1=E5=88=92=E6=8A=A5=E5=91=8A=E5=AF=BC?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/main/java/io/metersphere/xpack | 2 +- frontend/package.json | 4 +- .../TemplateComponent/TemplateComponent.vue | 49 ++++++++++++++-- .../comonents/report/TestCaseReportView.vue | 56 +++++++------------ frontend/src/business/components/xpack | 2 +- frontend/src/common/js/utils.js | 55 ++++++++++++++++++ 6 files changed, 124 insertions(+), 44 deletions(-) diff --git a/backend/src/main/java/io/metersphere/xpack b/backend/src/main/java/io/metersphere/xpack index 321c869938..d5b4969642 160000 --- a/backend/src/main/java/io/metersphere/xpack +++ b/backend/src/main/java/io/metersphere/xpack @@ -1 +1 @@ -Subproject commit 321c869938357e8c2253e5bd86c963828664ae23 +Subproject commit d5b4969642fd8d10cc2f949d7377e0a0e5217a3a diff --git a/frontend/package.json b/frontend/package.json index cf5672e833..fa0ad64992 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -30,7 +30,9 @@ "md5": "^2.3.0", "sha.js": "^2.4.11", "js-base64": "^3.4.4", - "json-bigint": "^1.0.0" + "json-bigint": "^1.0.0", + "html2canvas": "^1.0.0-rc.7", + "jspdf": "^2.1.1" }, "devDependencies": { "@vue/cli-plugin-babel": "^4.1.0", diff --git a/frontend/src/business/components/track/plan/view/comonents/report/TemplateComponent/TemplateComponent.vue b/frontend/src/business/components/track/plan/view/comonents/report/TemplateComponent/TemplateComponent.vue index d2b48f293a..47da205f32 100644 --- a/frontend/src/business/components/track/plan/view/comonents/report/TemplateComponent/TemplateComponent.vue +++ b/frontend/src/business/components/track/plan/view/comonents/report/TemplateComponent/TemplateComponent.vue @@ -13,12 +13,12 @@
- - - - - - + + + + + +
@@ -31,6 +31,8 @@ import RichTextComponent from "./RichTextComponent"; import FailureResultComponent from "./FailureResultComponent"; import DefectListComponent from "./DefectListComponent"; + import html2canvas from 'html2canvas'; + export default { name: "TemplateComponent", components: { @@ -51,6 +53,41 @@ type: Boolean, default: true }, + index: { + type: Number, + default: 0 + }, + }, + methods: { + getCanvas(canvasList) { + let index = this.index; + let componentId = this.getComponentId(); + return new Promise(function(resolve, reject) { + html2canvas(document.getElementById(componentId), { + scale: 2 + }).then(function(canvas) { + //排序 + canvasList.splice(index, 0, canvas); + resolve('success'); + }); + }); + }, + getComponentId() { + switch (this.preview.id) { + case 1: + return "baseInfoComponent"; + case 2: + return "testResultComponent"; + case 3: + return "resultChartComponent"; + case 4: + return "failureResultComponent"; + case 5: + return "defectListComponent"; + default: + return "richTextComponent"; + } + }, } } diff --git a/frontend/src/business/components/track/plan/view/comonents/report/TestCaseReportView.vue b/frontend/src/business/components/track/plan/view/comonents/report/TestCaseReportView.vue index 7277e73c68..ce08b348c4 100644 --- a/frontend/src/business/components/track/plan/view/comonents/report/TestCaseReportView.vue +++ b/frontend/src/business/components/track/plan/view/comonents/report/TestCaseReportView.vue @@ -25,38 +25,33 @@ {{$t('test_track.plan_view.edit_component')}} - +
-
- +
+
-
diff --git a/frontend/src/business/components/xpack b/frontend/src/business/components/xpack index f2d5a342c8..0a375848d0 160000 --- a/frontend/src/business/components/xpack +++ b/frontend/src/business/components/xpack @@ -1 +1 @@ -Subproject commit f2d5a342c82e629f510550d5778d752bb73bf5e7 +Subproject commit 0a375848d034d20eaf05caf11769e1c75c39235c diff --git a/frontend/src/common/js/utils.js b/frontend/src/common/js/utils.js index 10a28eb25b..6a344e4a0c 100644 --- a/frontend/src/common/js/utils.js +++ b/frontend/src/common/js/utils.js @@ -9,6 +9,7 @@ import { LicenseKey } from "./constants"; import axios from "axios"; +import {jsPDF} from "jspdf"; export function hasRole(role) { let user = getCurrentUser(); @@ -203,3 +204,57 @@ export function getUUID() { } +export function exportPdf(canvasList) { + + let pdf = new jsPDF('', 'pt', 'a4'); + + // 当前页面的当前高度 + let currentHeight = 0; + for (let canvas of canvasList) { + if (canvas) { + let contentWidth = canvas.width; + let contentHeight = canvas.height; + + //a4纸的尺寸[595.28,841.89] + let a4Width = 592.28; + let a4Height = 841.89; + + // html页面生成的canvas在pdf中图片的宽高 + let imgWidth = a4Width; + let imgHeight = a4Width/contentWidth * contentHeight; + + let pageData = canvas.toDataURL('image/jpeg', 1.0); + + // 当前图片的剩余高度 + let leftHeight = imgHeight; + + // 当前页面的剩余高度 + let blankHeight = a4Height - currentHeight; + + if (leftHeight > blankHeight) { + //页面偏移 + let position = 0; + while(leftHeight > 0) { + // 本次添加占用的高度 + let occupation = a4Height - currentHeight; + pdf.addImage(pageData, 'JPEG', 0, position + currentHeight, imgWidth, imgHeight); + currentHeight = leftHeight; + leftHeight -= occupation; + position -= occupation; + //避免添加空白页 + if(leftHeight > 0) { + pdf.addPage(); + currentHeight = 0; + } + } + } else { + pdf.addImage(pageData, 'JPEG', 0, currentHeight, imgWidth, imgHeight); + currentHeight += imgHeight; + } + } + } + + pdf.save('stone.pdf'); + +} +