From f2163ed27bf7f5a26b743f6e41838184aa22cab9 Mon Sep 17 00:00:00 2001
From: riku <risaku@163.com>
Date: 星期一, 17 十一月 2025 17:38:35 +0800
Subject: [PATCH] 1. 调试巡查单据通过浏览器批量下载功能(待完成)

---
 src/utils/doc.js |  104 +++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 86 insertions(+), 18 deletions(-)

diff --git a/src/utils/doc.js b/src/utils/doc.js
index 35f3b88..7d014aa 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) {

--
Gitblit v1.9.3