Fix:修复导出pdf时异步控制丢失的问题

This commit is contained in:
wanglin2 2023-11-20 17:49:05 +08:00
parent c716ec7294
commit 3d7f0fcbe7
2 changed files with 84 additions and 72 deletions

View File

@ -228,7 +228,7 @@ class Export {
if (width <= a4Size.width && height && a4Size.height) return false if (width <= a4Size.width && height && a4Size.height) return false
return width / height > 1 return width / height > 1
}) })
this.mindMap.doExportPDF.pdf(name, img, useMultiPageExport) await this.mindMap.doExportPDF.pdf(name, img, useMultiPageExport)
} }
// 导出为xmind // 导出为xmind

View File

@ -9,92 +9,104 @@ class ExportPDF {
} }
// 导出为pdf // 导出为pdf
pdf(name, img, useMultiPageExport = false) { async pdf(name, img, useMultiPageExport = false) {
if (useMultiPageExport) { if (useMultiPageExport) {
this.multiPageExport(name, img) await this.multiPageExport(name, img)
} else { } else {
this.onePageExport(name, img) await this.onePageExport(name, img)
} }
} }
// 单页导出 // 单页导出
onePageExport(name, img) { onePageExport(name, img) {
let pdf = new JsPDF('', 'pt', 'a4') return new Promise((resolve, reject) => {
let a4Ratio = a4Size.width / a4Size.height let pdf = new JsPDF('', 'pt', 'a4')
let image = new Image() let a4Ratio = a4Size.width / a4Size.height
image.onload = () => { let image = new Image()
let imageWidth = image.width image.onload = () => {
let imageHeight = image.height let imageWidth = image.width
let imageRatio = imageWidth / imageHeight let imageHeight = image.height
let w, h let imageRatio = imageWidth / imageHeight
if (imageWidth <= a4Size.width && imageHeight <= a4Size.height) { let w, h
// 使用图片原始宽高 if (imageWidth <= a4Size.width && imageHeight <= a4Size.height) {
w = imageWidth // 使用图片原始宽高
h = imageHeight w = imageWidth
} else if (a4Ratio > imageRatio) { h = imageHeight
// 以a4Height为高度缩放图片宽度 } else if (a4Ratio > imageRatio) {
w = imageRatio * a4Size.height // 以a4Height为高度缩放图片宽度
h = a4Size.height w = imageRatio * a4Size.height
} else { h = a4Size.height
// 以a4Width为宽度缩放图片高度 } else {
w = a4Size.width // 以a4Width为宽度缩放图片高度
h = a4Size.width / imageRatio w = a4Size.width
h = a4Size.width / imageRatio
}
pdf.addImage(
img,
'PNG',
(a4Size.width - w) / 2,
(a4Size.height - h) / 2,
w,
h
)
pdf.save(name)
resolve()
} }
pdf.addImage( image.onerror = e => {
img, reject(e)
'PNG', }
(a4Size.width - w) / 2, image.src = img
(a4Size.height - h) / 2, })
w,
h
)
pdf.save(name)
}
image.src = img
} }
// 多页导出 // 多页导出
multiPageExport(name, img) { multiPageExport(name, img) {
let image = new Image() return new Promise((resolve, reject) => {
image.onload = () => { let image = new Image()
let imageWidth = image.width image.onload = () => {
let imageHeight = image.height let imageWidth = image.width
// 一页pdf显示高度 let imageHeight = image.height
let pageHeight = (imageWidth / a4Size.width) * a4Size.height // 一页pdf显示高度
// 未生成pdf的高度 let pageHeight = (imageWidth / a4Size.width) * a4Size.height
let leftHeight = imageHeight // 未生成pdf的高度
// 偏移 let leftHeight = imageHeight
let position = 0 // 偏移
// a4纸的尺寸[595.28,841.89]图片在pdf中图片的宽高 let position = 0
let imgWidth = a4Size.width // a4纸的尺寸[595.28,841.89]图片在pdf中图片的宽高
let imgHeight = (a4Size.width / imageWidth) * imageHeight let imgWidth = a4Size.width
let pdf = new JsPDF('', 'pt', 'a4') let imgHeight = (a4Size.width / imageWidth) * imageHeight
// 有两个高度需要区分一个是图片的实际高度和生成pdf的页面高度(841.89) let pdf = new JsPDF('', 'pt', 'a4')
// 当内容未超过pdf一页显示的范围无需分页 // 有两个高度需要区分一个是图片的实际高度和生成pdf的页面高度(841.89)
if (leftHeight < pageHeight) { // 当内容未超过pdf一页显示的范围无需分页
pdf.addImage( if (leftHeight < pageHeight) {
img, pdf.addImage(
'PNG', img,
(a4Size.width - imgWidth) / 2, 'PNG',
(a4Size.height - imgHeight) / 2, (a4Size.width - imgWidth) / 2,
imgWidth, (a4Size.height - imgHeight) / 2,
imgHeight imgWidth,
) imgHeight
} else { )
// 分页 } else {
while (leftHeight > 0) { // 分页
pdf.addImage(img, 'PNG', 0, position, imgWidth, imgHeight) while (leftHeight > 0) {
leftHeight -= pageHeight pdf.addImage(img, 'PNG', 0, position, imgWidth, imgHeight)
position -= a4Size.height leftHeight -= pageHeight
// 避免添加空白页 position -= a4Size.height
if (leftHeight > 0) { // 避免添加空白页
pdf.addPage() if (leftHeight > 0) {
pdf.addPage()
}
} }
} }
pdf.save(name)
resolve()
} }
pdf.save(name) image.onerror = (e) => {
} reject(e)
image.src = img }
image.src = img
})
} }
} }