From d6ddbe5f6cd24909d83a072251cd433b9156d5ee Mon Sep 17 00:00:00 2001
From: Riku <risaku@163.com>
Date: 星期一, 17 十一月 2025 23:28:06 +0800
Subject: [PATCH] 2025.11.17

---
 src/utils/doc.js |  110 ++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 89 insertions(+), 21 deletions(-)

diff --git a/src/utils/doc.js b/src/utils/doc.js
index 35f3b88..631fc3c 100644
--- a/src/utils/doc.js
+++ b/src/utils/doc.js
@@ -6,7 +6,9 @@
 import FileSaver from 'file-saver';
 import { renderAsync } from 'docx-preview';
 import printJS from 'print-js';
-import pdfMake from 'pdfmake';
+import pdfMake from 'pdfmake/build/pdfmake';
+import vfs from 'pdfmake/build/vfs_fonts.js';
+import { jsPDF } from 'jspdf';
 
 /**
  * 绛夋瘮渚嬬缉鏀惧浘鐗�
@@ -209,24 +211,90 @@
   });
 }
 
-function preparePdf(tempDocpath, data, imageSize) {
-  return prepareDocxStr(tempDocpath, data, imageSize).then( (res) => {
-    // 灏� Word 鏂囨。杞崲涓� PDF 鏍煎紡骞朵繚瀛樺埌鏂囦欢
-    console.log(pdfMake);
+async function preparePdf(tempDocpath, data, imageSize) {
+  // return prepareDocxStr(tempDocpath, data, imageSize).then((res) => {
+  //   // 灏� Word 鏂囨。杞崲涓� PDF 鏍煎紡骞朵繚瀛樺埌鏂囦欢
+  //   console.log(pdfMake);
+  //   console.log('res', res);
+
+  //   // const printer = new PdfPrinter(vfs);
+  //   const docDefinition = {
+  //     content: [
+  //       {
+  //         text: res.getZip().generate({
+  //           type: 'blob',
+  //           mimeType:
+  //             'application/vnd.openxmlformats-officedocumnet.wordprocessingml.document'
+  //         })
+  //         // text: res.toString('utf8')
+  //       }
+  //     ]
+  //   };
+  //   const pdfDoc = pdfMake.createPdf(docDefinition);
+  //   // const pdfBuffer = await pdfDoc.getBuffer();
+  //   // return new Blob([pdfBuffer], { type: 'application/pdf' });
+  //   console.log(pdfDoc);
+  //   return pdfDoc;
+
+  //   // console.log('res', res.toString('utf8'));
+
+  //   // const doc = new jsPDF();
+  //   // doc.text(res.toString('utf8'), 10, 10);
+
+  //   // return doc;
+  // });
+
+  try {
+    // 绗竴姝ワ細浣跨敤docxtemplater澶勭悊docx妯℃澘
+    const doc = await prepareDocxStr(tempDocpath, data, imageSize);
     
-    // const printer = new pdfMake.PdfPrinter();
-    const docDefinition = {
-      content: [
-        {
-          text: res.toString('utf8')
-        }
-      ]
-    };
-    const pdfDoc = pdfMake.createPdf(docDefinition);
-    // const pdfBuffer = await pdfDoc.getBuffer();
-    // return new Blob([pdfBuffer], { type: 'application/pdf' });
-    return pdfDoc;
-  });
+    // 绗簩姝ワ細鑾峰彇娓叉煋鍚庣殑docx鍐呭
+    const docxBlob = doc.getZip().generate({
+      type: 'blob',
+      mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
+    });
+
+    return docxBlob
+    
+    // 绗笁姝ワ細璇诲彇docx鍐呭锛堟敞鎰忥細杩欎竴姝ラ渶瑕侀澶栫殑搴撴潵瑙f瀽docx鍐呭锛�
+    // 鐢变簬pdfmake涓嶈兘鐩存帴澶勭悊docx鏍煎紡锛屾垜浠渶瑕佸厛瑙f瀽docx鍐呭
+    // 杩欓噷鎻愪緵涓ょ鏂规锛�
+    
+    // 鏂规1锛氫娇鐢╠ocx-preview搴撳厛娓叉煋涓篐TML锛屽啀鐢╬dfmake鍒涘缓PDF
+    // const container = document.createElement('div');
+    // await renderAsync(docxBlob, container);
+    
+    // // 鎻愬彇HTML鍐呭骞惰浆鎹负pdfmake鍙敤鐨勬牸寮�
+    // const textContent = extractTextFromHtml(container.innerHTML);
+    
+    // // 绗洓姝ワ細浣跨敤pdfmake鍒涘缓PDF
+    // const docDefinition = {
+    //   content: [
+    //     {
+    //       text: textContent,
+    //       style: 'body'
+    //     }
+    //   ],
+    // };
+    
+    // const pdfDoc = pdfMake.createPdf(docDefinition);
+    // return pdfDoc;
+    
+  } catch (error) {
+    console.error('杞崲docx鍒皃df澶辫触:', error);
+    throw error;
+  }
+}
+
+/**
+ * 浠嶩TML涓彁鍙栨枃鏈唴瀹�
+ * @param {string} html HTML瀛楃涓�
+ * @returns {string} 鎻愬彇鐨勬枃鏈唴瀹�
+ */
+function extractTextFromHtml(html) {
+  const tempDiv = document.createElement('div');
+  tempDiv.innerHTML = html;
+  return tempDiv.textContent || tempDiv.innerText || '';
 }
 
 function exportDocx(tempDocpath, data, zipName, imageSize) {
@@ -257,8 +325,8 @@
     printable: ref,
     header: title || null,
     type: type || 'html',
-    headerStyle:
-      'font-size:6px;font-weight:600;text-align:center;padding:15px 0 10px 0;', //鏍囬璁剧疆
+    // headerStyle:
+    //   'font-size:6px;font-weight:600;text-align:center;padding:15px 0 10px 0;', //鏍囬璁剧疆
     properties: jsonData || [], //json鏁版嵁鍏�
     gridHeaderStyle:
       borderHeadStyle ||
@@ -268,7 +336,7 @@
       'font-size:1px;font-weight:200;border: 1px solid #ccc;padding:3px 5px 3px 5px;text-align:center;', //json鍚勫紡琛ㄥ摜澶
     scanStyles: false, //涓嶉�傜敤榛樿鏍峰紡
     repeatTableHeader: false, //鎵撳嵃json琛ㄥご鍙樉绀哄湪绗竴椤�
-    style: style || '@page{size:auto;margin: 0cm 1cm 0cm 1cm;}', //鍘婚櫎椤电湁椤佃剼
+    style: style || `@page{size:auto;margin: 0cm 1cm 0cm 1cm;}`, //鍘婚櫎椤电湁椤佃剼
     css: css || null //css url
   });
 }

--
Gitblit v1.9.3