From e888346ebcd8e3e099a15c7edd7bf367c057193a Mon Sep 17 00:00:00 2001
From: zouyu <2723363702@qq.com>
Date: 星期二, 04 三月 2025 15:33:33 +0800
Subject: [PATCH] Merge branch 'refs/heads/dev'

---
 src/utils/downHtmlToPDF.js |   22 ++++++++++++++++++++++
 1 files changed, 22 insertions(+), 0 deletions(-)

diff --git a/src/utils/downHtmlToPDF.js b/src/utils/downHtmlToPDF.js
new file mode 100644
index 0000000..3e83e09
--- /dev/null
+++ b/src/utils/downHtmlToPDF.js
@@ -0,0 +1,22 @@
+import html2canvas from "html2canvas";
+import jsPDF from "jspdf";
+
+export async function exportHtmlToPDF(element, name = "exported") {
+  try {
+    // 灏� HTML 鍏冪礌杞崲涓� canvas
+    console.log("姝e湪灏� 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);
+  }
+}

--
Gitblit v1.9.3