riku
2025-11-17 f2163ed27bf7f5a26b743f6e41838184aa22cab9
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内容(注意:这一步需要额外的库来解析docx内容)
    // 由于pdfmake不能直接处理docx格式,我们需要先解析docx内容
    // 这里提供两种方案:
    // 方案1:使用docx-preview库先渲染为HTML,再用pdfmake创建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到pdf失败:', error);
    throw error;
  }
}
/**
 * 从HTML中提取文本内容
 * @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) {