Quellcode durchsuchen

fix: export order & 789 tpl

ryanemax vor 3 Wochen
Ursprung
Commit
c20c0bd5c4

+ 33 - 13
server/cloud/tbook/func-tbook-export.js

@@ -185,6 +185,7 @@ export async function exportProcessReportDocs(processId,bookList) {
         query.equalTo("eduProcess",processId);
         textbookList = await query.find();
     }
+    console.log(bookList)
     if(bookList?.length){ // 直接导出教材列表
         let query = new Parse.Query("EduTextbook")
         query.include("childrens","department")
@@ -383,6 +384,9 @@ function renderReportDocsByTextbook(textbook){
     console.log("renderReportDocsByTextbook")
     return new Promise(async (resolve)=>{
         let json = textbook.toJSON();
+        json.hasNoSign = false; // 未上传作者签名
+        json.hasNoUnit = false; // 未上传单位资料
+        json.hasNoNine = false; // 无需添加滞后九
     let bookid = json.code || json?.objectId;
 
     // 默认单册为第一分册
@@ -497,7 +501,7 @@ function renderReportDocsByTextbook(textbook){
     console.log(json?.approval)
     let isNotImpt = (json?.approval?.indexOf("101计划")==-1) && (json?.approval?.indexOf("中央")==-1) && (json?.approval?.indexOf("四新")==-1) && (json?.approval?.indexOf("建设")==-1);
     
-
+ 
     let fixData = {
         volumeList:volumeList,
         // 图片信息
@@ -568,6 +572,21 @@ function renderReportDocsByTextbook(textbook){
     // console.log(json)
     let tempFileName = path.join(`${bookid}_${json.title}_申报书及附件.docx`)
 
+
+    // mergeFiles 处理七八九是否签名或后置问题
+    let lastPageList = []
+    let docx7 = await getPageDocx(bookid,"七",bookData);
+    let docx8 = await getPageDocx(bookid,"八",bookData);
+    let docx9 = await getPageDocx(bookid,"九",bookData);
+    lastPageList.push(json?.authorSignPDF?.url||docx7);
+    lastPageList.push(json?.unitMaterial?.url||docx8);
+    lastPageList.push(docx9);
+    let lastPageFileName = path.join(tempDir,`${bookid}_${bookData.title}_申报书及附件_789.pdf`)
+    let lastPagePdf = await docxToPdf(null,lastPageFileName,{mergeFiles:lastPageList}) // 成功用pdf,失败继续用docx
+    // mergeFiles = [...lastPageList,...mergeFiles]
+    mergeFiles = [lastPagePdf,...mergeFiles]
+
+       
     // 开始文件合并导出
         let filePath,pdfPath,urlDocx,urlPdf
         let TemplateDocxPath
@@ -604,21 +623,22 @@ function renderReportDocsByTextbook(textbook){
             urlPdf,
         })
         return
-        replaceDocx(TemplateDocxPath,tempFileName,bookData,{onDocxComplete:async (filePath)=>{
-            // 需要API支持
-            let pdfPath = filePath.replaceAll(".docx",".pdf")
-            filePath = await docxToPdf(filePath,pdfPath) || filePath // 成功用pdf,失败继续用docx
-            let url = (await uploadFileToOSS(pdfPath))?.url || null
-            resolve({
-                code:bookid,
-                title:json?.title,
-                filePath,
-                url
-            })
-        }})
     })
 }
 
+async function getPageDocx(bookid,pageName,bookData){
+    let tplPath = path.join(TemplateDocxDir,`模板-本科教材申报书-${pageName}.docx`);
+    let tempFileName = path.join(`${bookid}_${bookData.title}_申报书及附件_${pageName}.docx`);
+    let filePath = renderDocx(tplPath,tempFileName,bookData);
+    return filePath;
+    // 转pdf
+    // let pdfPath = filePath.replaceAll(".docx",".pdf")
+    // let options = {}
+    // pdfPath = await docxToPdf(null,pdfPath,options) || filePath // 成功用pdf,失败继续用docx
+    // return pdfPath
+
+}
+
 function toYearMonth(date){
     // console.log("toYearMonth",date)
     if(typeof date == "string" && (date?.indexOf("年")>-1 || date?.indexOf("-")>-1)) return date

BIN
server/cloud/tbook/template/模板-本科教材申报书-七.docx


BIN
server/cloud/tbook/template/模板-本科教材申报书-九.docx


BIN
server/cloud/tbook/template/模板-本科教材申报书-全册.docx


BIN
server/cloud/tbook/template/模板-本科教材申报书-八.docx


BIN
server/cloud/tbook/template/模板-本科教材申报书-单册.docx


+ 3 - 1
server/cloud/tbook/test/test-export.js

@@ -22,7 +22,9 @@ async function main(){
         // console.log(await exportProcessReportDocs("FR7KZtefyR"))
         // console.log("测试列表")
         // console.log(await exportProcessReportDocs(null,["9V575dapEM"]))
-        console.log(await exportProcessReportDocs(null,["c2WUE5PgCo"]))
+        // console.log(await exportProcessReportDocs(null,["c2WUE5PgCo"]))
+        console.log(await exportProcessReportDocs(null,["KvvN9lWhri"]))
+        // console.log(await exportProcessReportDocs(null,["F8eiOAfuC3"]))
         
         // console.log(await exportProcessReportDocs(null,["Oa1yB6n64u","j4pt2MMTXM","jAMGxwrl1F","BwZpEoOtS8","X10kCktA0r"]))
         // console.log(await exportProcessReportDocs(null,["BwZpEoOtS8","X10kCktA0r"]))

+ 25 - 11
server/lib/docs/index.js

@@ -15,7 +15,7 @@ import { Chromiumly } from "chromiumly";
 // Chromiumly.configure({ endpoint: "http://8.140.98.43/docs" });
 Chromiumly.configure({ endpoint: "http://123.57.204.89/docs" });
 const { LibreOffice } = require("chromiumly");
-const { PDFEngines } = require("chromiumly");
+// const { PDFEngines } = require("chromiumly");
 const tempDir = path.join(__dirname , "temp");
 if(!fs.existsSync(tempDir)){fs.mkdirSync(tempDir)};
 
@@ -124,6 +124,8 @@ module.exports.createZip = createZip
 
 const download = require('download')
 async function downloadUrl(url) {
+    console.log(url)
+    if(url?.startsWith("/")) {return url};
     let md5 = crypto.createHash('md5');
     let filename = md5.update(url).digest('hex') + path.extname(url)
     let filepath = path.join(tempDir,filename)
@@ -139,7 +141,6 @@ async function downloadUrl(url) {
     }
 }
 
-
 /**
  * 将 DOCX 文件转换为 PDF
  * 
@@ -171,16 +172,29 @@ async function downloadUrl(url) {
     }
     let filePathList = mergeFiles?.map((item,index)=>mergeFileMap[index]).filter(item=>item)
 
-    console.log("DOWNLOADED:",filePathList)
+    // console.log("DOWNLOADED:",filePathList)
+    filePathList = filePathList.map((filepath,index)=>{
+        // 按顺序修改文件前缀数字为字母表顺序
+        let fileDir = path.dirname(filepath);
+        let abc = String.fromCharCode(96+(index+1)); // 字母顺序不会出现 把 1 10 11 12 放在一起的情况
+        let num = index+110; // 数字顺序从百位开始,避免首数字排序错乱
+        let fileName = num + "_" + path.basename(filepath)
+        let orderPath = path.join(fileDir,fileName)
+        fs.cpSync(filepath,orderPath);
+        fs.readFileSync(filepath);
+        return orderPath
+    })
     try {
-        let docxBuffer = fs.readFileSync(docxPath);
-
-        let files = [
-            // docxPath
-            { data: docxBuffer, ext: "docx" },
-            ...filePathList
-        ];
-        // console.log(files)
+        
+        let files = []
+
+        if(docxPath){
+            let docxBuffer = fs.readFileSync(docxPath);
+            files.push({ data: docxBuffer, ext: "docx" })
+        }
+        files = [...files,...filePathList]
+
+        console.log("files",files)
         let pdfBuffer = await LibreOffice.convert({
             files,
             properties: {