2 次代碼提交 55b484caa6 ... d86a62106c

作者 SHA1 備註 提交日期
  ryanemax d86a62106c Merge branch 'master' of http://git.fmode.cn:3000/bin/edu-textbook 2 月之前
  ryanemax 353c246233 fix: merge with pdf filepath 2 月之前
共有 3 個文件被更改,包括 33 次插入13 次删除
  1. 8 1
      server/cloud/tbook/func-tbook-export.js
  2. 3 1
      server/cloud/tbook/test/test-export.js
  3. 22 11
      server/lib/docs/index.js

+ 8 - 1
server/cloud/tbook/func-tbook-export.js

@@ -219,7 +219,7 @@ export async function exportProcessReportDocs(processId,bookList) {
             fs.rmSync(zipPath)
             fileList.forEach(tempFile=>{
                 console.log("rm",tempFile)
-                // fs.rmSync(path.dirname(tempFile),{recursive:true,force:true});
+                fs.rmSync(path.dirname(tempFile),{recursive:true,force:true});
             })
         }
         docsList = docsList.map(item=>{return {code:item.code,title:item.title,url:item?.url,urlPdf:item?.urlPdf}})
@@ -590,6 +590,13 @@ function renderReportDocsByTextbook(textbook){
     
     Object.keys(fixData).forEach(key=>{
         bookData[key] = fixData[key] || bookData[key] || ""
+        if(typeof bookData[key]=="string"){
+            bookData[key] = bookData[key].replaceAll("\x02"," ")
+            bookData[key] = bookData[key].replaceAll("\u0002"," ") // 字段填写字符中,存在无法转码的乱码字符
+            if(key=="ac31"){
+                console.log(bookData[key])
+            }
+        }
     })
     // console.log(bookData)
     // console.log(json)

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

@@ -31,8 +31,10 @@ async function main(){
         // console.log(await exportProcessReportDocs(null,["R9r0XtMsK3"])) // CIP顺序问题,后两个pdf到了最前面
         // console.log(await exportProcessReportDocs(null,["MU5YpUizfW"]))
         // console.log(await exportProcessReportDocs(null,["UUGgHPHsIC"]))
-        console.log(await exportProcessReportDocs(null,["KxhErArSbB"]))
+        // console.log(await exportProcessReportDocs(null,["KxhErArSbB"]))
         // console.log(await exportProcessReportDocs(null,["7cf69rSawD"]))
+        // console.log(await exportProcessReportDocs(null,["kY6MbE1JXC"])) // 21个附件,超时
+        console.log(await exportProcessReportDocs(null,["O8kCxqdasp"]))
         
         // console.log(await exportProcessReportDocs(null,["3wJSkW9HNV"]))
         // console.log(await exportProcessReportDocs(null,["tHhfIDBkhf"]))

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

@@ -223,7 +223,7 @@ async function downloadUrl(url,options) {
         }
         // console.log("convertOpts",convertOpts)
 
-        let pdfBuffer
+        let pdfPath,pdfBuffer
         // 方式1:逐个合并
         // let pdfBuffer
         // for (let index = 1; index < files.length; index++) {
@@ -250,7 +250,7 @@ async function downloadUrl(url,options) {
             let pdfList = [mainPdfPath,...files.slice(1)];
             pdfList = pdfList.filter(item=>item)
             let mergedFileList = await mergePdfListReduce(pdfList,convertOpts) 
-            pdfBuffer = mergedFileList[0]?.data;
+            pdfPath = mergedFileList[0];
         // convertOpts.files = [files[0],...mergedFileList]
             // console.log(convertOpts)
             // pdfBuffer = await LibreOffice.convert(convertOpts);
@@ -261,9 +261,14 @@ async function downloadUrl(url,options) {
         // 方式3:全部合并
         // let pdfBuffer = await LibreOffice.convert(convertOpts);
 
+        if(pdfPath){
+            fs.cpSync(pdfPath,outputPath);
+        }
         // 将 Buffer 写入输出文件
-        fs.writeFileSync(outputPath, pdfBuffer);
-        console.log(`成功输出 ${outputPath}`);
+        if(pdfBuffer){
+            fs.writeFileSync(outputPath, pdfBuffer);
+            console.log(`成功输出 ${outputPath}`);
+        }
         return outputPath
     } catch (error) {
         console.error('转换失败:', error);
@@ -286,7 +291,7 @@ export async function mergePdfListReduce(pdfList,convertOpts){
     // 所有非PDF转PDF
     for (let index = 0; index < pdfList.length; index++) {
         let file = pdfList[index];
-        if(file?.toLocaleLowerCase()?.indexOf("pdf")==-1){
+        if(typeof file == "string" && file?.toLocaleLowerCase()?.indexOf("pdf")==-1){
             convertOpts.files = [file];
             let pdfBuffer = await LibreOffice.convert(convertOpts);
             fs.writeFileSync(file+".pdf",pdfBuffer)
@@ -301,10 +306,13 @@ export async function mergePdfListReduce(pdfList,convertOpts){
         let file = pdfList.shift();
         // console.log(file,index,length)
         if(!file) break;
-        let files = [file,pdfList.shift(),pdfList.shift(),pdfList.shift()
-            ,pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift()
-            ,pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift()
-            ,pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift()
+        let files = [file,pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),
+            pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),
+            // pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),
+            // pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),
+            // pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),
+            // ,pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift()
+            // ,pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift()
             // ,pdfList.shift(),pdfList.shift(),pdfList.shift()
             // ,pdfList.shift(),pdfList.shift(),pdfList.shift()
             // ,pdfList.shift(),pdfList.shift(),pdfList.shift(),pdfList.shift()
@@ -325,7 +333,9 @@ export async function mergePdfListReduce(pdfList,convertOpts){
                 // console.log("多文件合并",convertOpts)
                 let mergeBuffer = await PDFEngines.merge(convertOpts)
                     // let mergeBuffer = await LibreOffice.convert(convertOpts);
-                    resolve({data:mergeBuffer,ext:"pdf"})
+                    let mergeFilePath = files[0]+".merge.pdf"
+                    fs.writeFileSync(mergeFilePath,mergeBuffer)
+                    resolve(mergeFilePath)
             }
         }))
     }
@@ -333,7 +343,7 @@ export async function mergePdfListReduce(pdfList,convertOpts){
         mergeList = await Promise.all(plist);
     }
     // console.log("mergeList",mergeList)
-    if(mergeList?.length<=3){
+    if(mergeList?.length<=1){
         return mergeList;
     }else{
         // console.log("mergePdfListReduce continue:",mergeList)
@@ -342,6 +352,7 @@ export async function mergePdfListReduce(pdfList,convertOpts){
 }
 
 export function renderDocx(inputDocxPath, outputDocxName, data,options){
+
     let imageOptions = {
         getImage(tagValue,tagName) {
             if(!fs.existsSync(tagValue)){