gaoluyang
2025-04-01 b6665b9f4913f5b31bd3379dd3e1032f8769f9a1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import html2canvas from "html2canvas";
import jsPDF from "jspdf";
 
export async function exportHtmlToPDF(element, name = "exported") {
  try {
    // 将 HTML 元素转换为 canvas
    console.log("正在将 HTML 转换为 canvas...", element);
    const canvas = await html2canvas(element, { useCORS: true });
    const imgData = canvas.toDataURL("image/png");
 
    // 创建 PDF
    const pdf = new jsPDF("p", "mm", "a4");
    const pdfWidth = pdf.internal.pageSize.getWidth();
    const pdfHeight = (canvas.height * pdfWidth) / canvas.width;
 
    pdf.addImage(imgData, "PNG", 10, 10, pdfWidth - 20, pdfHeight - 20);
    pdf.save(name + ".pdf");
    console.log("PDF 导出成功!");
  } catch (error) {
    console.error("导出 PDF 失败:", error);
  }
}