From b1ce70777fb52ba986b6a8cf1b00cce93c2e84be Mon Sep 17 00:00:00 2001
From: riku <risaku@163.com>
Date: 星期五, 14 十一月 2025 17:46:47 +0800
Subject: [PATCH] 1. 修改巡查单据的下载功能(待完成)
---
src/utils/doc.js | 127 +++++++++++++++++++++++++++++++++++++----
1 files changed, 113 insertions(+), 14 deletions(-)
diff --git a/src/utils/doc.js b/src/utils/doc.js
index a757cfc..35f3b88 100644
--- a/src/utils/doc.js
+++ b/src/utils/doc.js
@@ -4,6 +4,9 @@
import ImageModule from 'docxtemplater-image-module-free';
import Pizzip from 'pizzip';
import FileSaver from 'file-saver';
+import { renderAsync } from 'docx-preview';
+import printJS from 'print-js';
+import pdfMake from 'pdfmake';
/**
* 绛夋瘮渚嬬缉鏀惧浘鐗�
@@ -146,7 +149,7 @@
};
}
-export const exportDocx = (tempDocpath, data, zipName, imageSize) => {
+function prepareDocxtemplater(tempDocpath, data, imageSize) {
return new Promise((resolve, reject) => {
JSZipUtils.getBinaryContent(tempDocpath, (error, content) => {
if (error) {
@@ -164,20 +167,116 @@
}
doc.compile();
doc.resolveData(data).then(() => {
- try {
- doc.render();
- } catch (error) {
- console.log(error);
- throw error;
- }
- const out = doc.getZip().generate({
- type: 'blob',
- mimeType:
- 'application/vnd.openxmlformats-officedocumnet.wordprocessingml.document'
- });
- FileSaver.saveAs(out, zipName);
- resolve();
+ resolve(doc);
});
});
});
+}
+
+/**
+ * 鍑嗗docx鏂囨。锛岃繑鍥瀊lob瀵硅薄
+ * @param {*} tempDocpath 妯℃澘docx鏂囦欢璺緞
+ * @param {*} data 鏁版嵁瀵硅薄
+ * @param {*} zipName 鍘嬬缉鍖呭悕绉�
+ * @param {*} imageSize 鍥剧墖澶у皬閰嶇疆瀵硅薄
+ * @returns
+ */
+function prepareDocxBlob(tempDocpath, data, imageSize) {
+ return prepareDocxtemplater(tempDocpath, data, imageSize).then((doc) => {
+ try {
+ doc.render();
+ } catch (error) {
+ console.log(error);
+ throw error;
+ }
+ const out = doc.getZip().generate({
+ type: 'blob',
+ mimeType:
+ 'application/vnd.openxmlformats-officedocumnet.wordprocessingml.document'
+ });
+ return out;
+ });
+}
+
+function prepareDocxStr(tempDocpath, data, imageSize) {
+ return prepareDocxtemplater(tempDocpath, data, imageSize).then((doc) => {
+ try {
+ return doc.render();
+ } catch (error) {
+ console.log(error);
+ throw error;
+ }
+ });
+}
+
+function preparePdf(tempDocpath, data, imageSize) {
+ return prepareDocxStr(tempDocpath, data, imageSize).then( (res) => {
+ // 灏� Word 鏂囨。杞崲涓� PDF 鏍煎紡骞朵繚瀛樺埌鏂囦欢
+ console.log(pdfMake);
+
+ // 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;
+ });
+}
+
+function exportDocx(tempDocpath, data, zipName, imageSize) {
+ return prepareDocxBlob(tempDocpath, data, zipName, imageSize).then((blob) => {
+ FileSaver.saveAs(blob, zipName);
+ });
+}
+
+function previewDocx(blob, ref) {
+ return renderAsync(blob, ref);
+}
+
+function downloadDocx(blob, zipName) {
+ FileSaver.saveAs(blob, zipName);
+}
+
+function print(
+ ref,
+ title,
+ style,
+ type,
+ jsonData,
+ borderHeadStyle,
+ gridStyle,
+ css
+) {
+ printJS({
+ printable: ref,
+ header: title || null,
+ type: type || 'html',
+ headerStyle:
+ 'font-size:6px;font-weight:600;text-align:center;padding:15px 0 10px 0;', //鏍囬璁剧疆
+ properties: jsonData || [], //json鏁版嵁鍏�
+ gridHeaderStyle:
+ borderHeadStyle ||
+ 'font-size:6px;font-weight:400;height:40px;line-height:40px;border: 1px solid #ccc;padding:3px 5px 3px 5px;text-align:center;', //json鏍煎紡琛ㄥご鏍峰紡
+ gridStyle:
+ gridStyle ||
+ '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;}', //鍘婚櫎椤电湁椤佃剼
+ css: css || null //css url
+ });
+}
+export {
+ exportDocx,
+ prepareDocxBlob,
+ preparePdf,
+ previewDocx,
+ downloadDocx,
+ print
};
--
Gitblit v1.9.3