import pptxgen from 'pptxgenjs'
import html2canvas from 'html2canvas'
import JsPDF from 'jspdf'
import dayjs from 'dayjs'
const loadingHide = () => {
}
const loadingShow = () => {
}
function toPdf(imageList = [], fileName) {
const doc = new JsPDF({
orientation: 'l',
unit: 'mm',
format: 'a5',
putOnlyUsedFonts: true,
floatPrecision: 16,
})
imageList.forEach((imageItem, index) => {
const width = doc.internal.pageSize.getWidth()
const height = doc.internal.pageSize.getHeight()
const widthRatio = width / imageItem.imgWidth
const heightRatio = height / imageItem.imgHeight
const ratio = Math.min(widthRatio, heightRatio)
const w = imageItem.imgWidth * ratio
const h = imageItem.imgHeight * ratio
const position = 0
doc.addImage(imageItem.url, 'JPEG', 0, position, w, h)
if (index < imageList.length - 1) {
doc.addPage()
}
})
doc.save(fileName)
}
function toPPT(imageList = [], fileName) {
const pptx = new pptxgen()
imageList.forEach((imageItem) => {
const slide = pptx.addSlide()
let heightPercent = parseFloat(imageItem.imgHeight / imageItem.imgWidth * 100 * 16 / 9).toFixed(2)
if (heightPercent > 100) {
heightPercent = 100
} else if (heightPercent < 30) {
heightPercent = 30
}
slide.addImage({
data: imageItem.url,
x: 0,
y: 0,
w: '100%',
h: heightPercent + '%',
sizing: { type: 'cover' },
align: 'center',
fill: { color: 'F1F1F1' },
color: 'A1A1A1',
})
})
pptx.writeFile({ fileName })
}
export async function generateFile({
sourceList = [], // dom id 列表
sourceType = 'DOM_ID_ARRAY', // DOM_ID_ARRAY | IMAGE_ARRAY
exportType = 'ppt', // ppt | pdf
name = '导出文件',
antoDownload = true, // 是否自动下载
showLoading = true, // 是否显示loading
}) {
const fileName = `${name}-${dayjs().format('YYYYMMDDHHmmss')}`
if (!sourceList?.length === 0)
return console.log('exportToFile:导出数据源不能为空')
if (showLoading) {
loadingShow()
}
const imageList = []
const imageListAsync = sourceList.map((domIDItem) => {
const dom = document.getElementById(domIDItem)
console.log('dom', domIDItem, dom)
return html2canvas(dom, {
scale: 2,
backgroundColor: '#ffffff',
})
})
Promise.all(imageListAsync).then((res) => {
console.log('res', res)
res.forEach((canvasItem) => {
const chartImgUrl = canvasItem.toDataURL()
if (!chartImgUrl) return
imageList.push({
url: chartImgUrl,
imgWidth: canvasItem.width,
imgHeight: canvasItem.height,
})
})
if (exportType === 'ppt') {
toPPT(imageList, `${fileName}.pptx`)
}
if (exportType === 'pdf') {
toPdf(imageList, `${fileName}.pdf`)
}
if (showLoading) {
loadingHide()
}
})
}