From a6d04ffa91653d055af2d16ac8bab44f5408bbdd Mon Sep 17 00:00:00 2001 From: wanglin2 <1013335014@qq.com> Date: Mon, 20 Nov 2023 18:06:14 +0800 Subject: [PATCH] =?UTF-8?q?Feat=EF=BC=9A=E5=AF=BC=E5=87=BApng=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E6=96=B0=E5=A2=9E=E5=8E=8B=E7=BC=A9=E5=8F=82?= =?UTF-8?q?=E6=95=B0=EF=BC=9B=E4=BC=98=E5=8C=96=E5=A4=A7=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E9=87=8F=E8=8A=82=E7=82=B9=E5=AF=BC=E5=87=BApdf=E6=97=B6?= =?UTF-8?q?=E4=BD=93=E7=A7=AF=E8=BF=87=E5=A4=A7=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-mind-map/src/plugins/Export.js | 42 ++++++++++++++++++++------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/simple-mind-map/src/plugins/Export.js b/simple-mind-map/src/plugins/Export.js index a8554222..55bfe3c7 100644 --- a/simple-mind-map/src/plugins/Export.js +++ b/simple-mind-map/src/plugins/Export.js @@ -2,7 +2,8 @@ import { imgToDataUrl, downloadFile, readBlob, - removeHTMLEntities + removeHTMLEntities, + resizeImgSize } from '../utils' import { SVG } from '@svgdotjs/svg.js' import drawBackgroundImageToCanvas from '../utils/simulateCSSBackgroundInCanvas' @@ -63,7 +64,8 @@ class Export { transparent, checkRotate = () => { return false - } + }, + compress ) { return new Promise((resolve, reject) => { const img = new Image() @@ -76,8 +78,19 @@ class Export { window.devicePixelRatio, this.mindMap.opt.minExportImgCanvasScale ) - const imgWidth = img.width - const imgHeight = img.height + let imgWidth = img.width + let imgHeight = img.height + // 压缩图片 + if (compress) { + const compressedSize = resizeImgSize( + imgWidth, + imgHeight, + compress.width, + compress.height + ) + imgWidth = compressedSize[0] + imgHeight = compressedSize[1] + } // 如果宽比高长,那么旋转90度 const needRotate = checkRotate(imgWidth, imgHeight) if (needRotate) { @@ -186,7 +199,7 @@ class Export { * 方法1.把svg的图片都转化成data:url格式,再转换 * 方法2.把svg的图片提取出来再挨个绘制到canvas里,最后一起转换 */ - async png(name, transparent = false, checkRotate) { + async png(name, transparent = false, checkRotate, compress) { let { node, str } = await this.getSvgData() // 如果开启了富文本,则使用htmltocanvas转换为图片 if (this.mindMap.richText) { @@ -215,19 +228,26 @@ class Export { // 转换成data:url数据 let svgUrl = await readBlob(blob) // 绘制到canvas上 - let res = await this.svgToPng(svgUrl, transparent, checkRotate) + let res = await this.svgToPng(svgUrl, transparent, checkRotate, compress) return res } // 导出为pdf - async pdf(name, useMultiPageExport) { + async pdf(name, useMultiPageExport, maxImageWidth) { if (!this.mindMap.doExportPDF) { throw new Error('请注册ExportPDF插件') } - let img = await this.png('', false, (width, height) => { - if (width <= a4Size.width && height && a4Size.height) return false - return width / height > 1 - }) + let img = await this.png( + '', + false, + (width, height) => { + if (width <= a4Size.width && height && a4Size.height) return false + return width / height > 1 + }, + { + width: maxImageWidth || a4Size.width * 2 + } + ) await this.mindMap.doExportPDF.pdf(name, img, useMultiPageExport) }