Browse Source

fix: export with pdf merge

ryanemax 2 months ago
parent
commit
f79e45dc61
2 changed files with 21 additions and 9 deletions
  1. 2 2
      server/cloud/tbook/test/test-export.js
  2. 19 7
      server/lib/docs/index.js

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

@@ -32,9 +32,9 @@ async function main(){
         // console.log(await exportProcessReportDocs(null,["MU5YpUizfW"]))
         // console.log(await exportProcessReportDocs(null,["MU5YpUizfW"]))
         // console.log(await exportProcessReportDocs(null,["UUGgHPHsIC"]))
         // console.log(await exportProcessReportDocs(null,["UUGgHPHsIC"]))
         // console.log(await exportProcessReportDocs(null,["KxhErArSbB"]))
         // console.log(await exportProcessReportDocs(null,["KxhErArSbB"]))
-        console.log(await exportProcessReportDocs(null,["3wJSkW9HNV"]))
+        console.log(await exportProcessReportDocs(null,["7cf69rSawD"]))
         
         
-
+        // console.log(await exportProcessReportDocs(null,["3wJSkW9HNV"]))
         // console.log(await exportProcessReportDocs(null,["tHhfIDBkhf"]))
         // console.log(await exportProcessReportDocs(null,["tHhfIDBkhf"]))
         // console.log(await exportProcessReportDocs(null,["cgTEtIvkK4"]))
         // console.log(await exportProcessReportDocs(null,["cgTEtIvkK4"]))
         // console.log(await exportProcessReportDocs(null,["KvvN9lWhri"]))
         // console.log(await exportProcessReportDocs(null,["KvvN9lWhri"]))

+ 19 - 7
server/lib/docs/index.js

@@ -236,13 +236,24 @@ async function downloadUrl(url) {
         //     pdfBuffer = await LibreOffice.convert(convertOpts);
         //     pdfBuffer = await LibreOffice.convert(convertOpts);
         // }
         // }
 
 
+        let mainPdfPath = docxPath
+        if(docxPath){
+            convertOpts.files = [files[0]];
+            console.log(convertOpts)
+            let mainPdfBuffer = await LibreOffice.convert(convertOpts);
+            mainPdfPath = path.dirname(docxPath)+"/109_"+path.basename(docxPath)+".pdf"
+            fs.writeFileSync(mainPdfPath,mainPdfBuffer)
+        }
+
         // 方式2:先合并pdf,后合并docx
         // 方式2:先合并pdf,后合并docx
         if(files?.length>4){
         if(files?.length>4){
-            let pdfList = files.slice(1);
+            let pdfList = [mainPdfPath,...files.slice(1)];
+            pdfList = pdfList.filter(item=>item)
             let mergedFileList = await mergePdfListReduce(pdfList,convertOpts) 
             let mergedFileList = await mergePdfListReduce(pdfList,convertOpts) 
-            convertOpts.files = [files[0],...mergedFileList]
+            pdfBuffer = mergedFileList[0]?.data;
+        // convertOpts.files = [files[0],...mergedFileList]
             // console.log(convertOpts)
             // console.log(convertOpts)
-            pdfBuffer = await LibreOffice.convert(convertOpts);
+            // pdfBuffer = await LibreOffice.convert(convertOpts);
         }else{
         }else{
             pdfBuffer = await LibreOffice.convert(convertOpts);
             pdfBuffer = await LibreOffice.convert(convertOpts);
         }
         }
@@ -280,10 +291,11 @@ export async function mergePdfListReduce(pdfList,convertOpts){
         // console.log(file,index,length)
         // console.log(file,index,length)
         if(!file) break;
         if(!file) break;
         let files = [file,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()
         ]; // 每次合并四个
         ]; // 每次合并四个
         files=files?.filter(item=>item);
         files=files?.filter(item=>item);