mind-map/simple-mind-map/src/plugins/ExportPDF.js
2023-12-28 09:27:34 +08:00

72 lines
2.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// import JsPDF from '../utils/jspdf'
import { PDFDocument } from 'pdf-lib'
import { readBlob } from '../utils/index'
// 导出PDF插件需要通过Export插件使用
class ExportPDF {
// 构造函数
constructor(opt) {
this.mindMap = opt.mindMap
}
// 使用pdf-lib库导出为pdf
async pdf(img) {
return new Promise((resolve, reject) => {
const image = new Image()
image.onload = async () => {
const imageWidth = image.width
const imageHeight = image.height
// 创建pdf页面尺寸设置为图片的大小
const pdfDoc = await PDFDocument.create()
const page = pdfDoc.addPage()
page.setSize(imageWidth, imageHeight)
// 添加图片到pdf
const pngImage = await pdfDoc.embedPng(img)
page.drawImage(pngImage, {
x: 0,
y: 0,
width: imageWidth,
height: imageHeight
})
const pdfBytes = await pdfDoc.save()
const blob = new Blob([pdfBytes])
const res = await readBlob(blob)
resolve(res)
}
image.onerror = e => {
reject(e)
}
image.src = img
})
}
// 使用jspdf库导出为pdf
// async pdf(name, img) {
// return new Promise((resolve, reject) => {
// const image = new Image()
// image.onload = () => {
// const imageWidth = image.width
// const imageHeight = image.height
// const pdf = new JsPDF({
// unit: 'px',
// format: [imageWidth, imageHeight],
// compress: true,
// hotfixes: ['px_scaling'],
// orientation: imageWidth > imageHeight ? 'landscape' : 'portrait'
// })
// pdf.addImage(img, 'PNG', 0, 0, imageWidth, imageHeight)
// pdf.save(name)
// resolve()
// }
// image.onerror = e => {
// reject(e)
// }
// image.src = img
// })
// }
}
ExportPDF.instanceName = 'doExportPDF'
export default ExportPDF