Quellcode durchsuchen

fix: merge pdf with pdfunite

ryanemax vor 2 Monaten
Ursprung
Commit
0211f492f5
2 geänderte Dateien mit 29 neuen und 8 gelöschten Zeilen
  1. 2 2
      server/cloud/tbook/test/test-export.js
  2. 27 6
      server/lib/docs/index.js

+ 2 - 2
server/cloud/tbook/test/test-export.js

@@ -33,8 +33,8 @@ async function main(){
         // console.log(await exportProcessReportDocs(null,["UUGgHPHsIC"]))
         // 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,["kY6MbE1JXC"])) // 21个附件,超时
+        // console.log(await exportProcessReportDocs(null,["O8kCxqdasp"])) // \u0002 乱码
         
         // console.log(await exportProcessReportDocs(null,["3wJSkW9HNV"]))
         // console.log(await exportProcessReportDocs(null,["tHhfIDBkhf"]))

+ 27 - 6
server/lib/docs/index.js

@@ -322,20 +322,29 @@ export async function mergePdfListReduce(pdfList,convertOpts){
         plist.push(new Promise(async resolve=>{
             if(files?.length==1){ // 单文件直接加载 自动获取后缀
                 let onefile = files[0]
-                if(!onefile?.ext){
-                    let extname = path.extname(files[0]).slice(1)?.toLocaleLowerCase();
-                    onefile = {data:fs.readFileSync(onefile),ext:extname}
-                }
+                // if(!onefile?.ext){
+                //     let extname = path.extname(files[0]).slice(1)?.toLocaleLowerCase();
+                //     onefile = {data:fs.readFileSync(onefile),ext:extname}
+                // }
                 resolve(onefile);
             }else{ // 多文件合并
                 convertOpts = {}
                 convertOpts.files = files;
                 // console.log("多文件合并",convertOpts)
-                let mergeBuffer = await PDFEngines.merge(convertOpts)
-                    // let mergeBuffer = await LibreOffice.convert(convertOpts);
+                // pdfEngine合并
+                if(false){
+                    let mergeBuffer = await PDFEngines.merge(convertOpts)
                     let mergeFilePath = files[0]+".merge.pdf"
                     fs.writeFileSync(mergeFilePath,mergeBuffer)
                     resolve(mergeFilePath)
+                }
+                // pdfunite合并
+                if(true){
+                    let mergeFilePath = files[0]+".merge.pdf"
+                    pdfUnite(files,mergeFilePath)
+                    resolve(mergeFilePath)
+                }
+
             }
         }))
     }
@@ -351,6 +360,18 @@ export async function mergePdfListReduce(pdfList,convertOpts){
     }
 }
 
+function pdfUnite(pdfList,outputPath){
+    let params = ["pdfunite",...pdfList,outputPath].join(" ")
+    try{
+        shell.exec(params)
+    }catch(err){}
+    if(fs.existsSync(outputPath)){
+        return outputPath
+    }else{
+        throw "error: pdfunit merge error"
+    }
+}
+
 export function renderDocx(inputDocxPath, outputDocxName, data,options){
 
     let imageOptions = {