소스 검색

fix: merge with 3-5 files

ryanemax 2 달 전
부모
커밋
40056142c0
2개의 변경된 파일13개의 추가작업 그리고 8개의 파일을 삭제
  1. 2 0
      server/cloud/tbook/test/test-export.js
  2. 11 8
      server/lib/docs/index.js

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

@@ -26,6 +26,8 @@ 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,["tHhfIDBkhf"]))
+        // console.log(await exportProcessReportDocs(null,["cgTEtIvkK4"]))
         // console.log(await exportProcessReportDocs(null,["KvvN9lWhri"]))
         // console.log(await exportProcessReportDocs(null,["F8eiOAfuC3"]))
         

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

@@ -124,7 +124,7 @@ module.exports.createZip = createZip
 
 const download = require('download')
 async function downloadUrl(url) {
-    console.log(url)
+    // console.log(url)
     if(url?.startsWith("/")) {return url};
     let md5 = crypto.createHash('md5');
     let extname = path.extname(url)?.toLocaleLowerCase();
@@ -200,7 +200,7 @@ async function downloadUrl(url) {
             properties: {
                 // 设置页面属性,例如纸张大小和方向
                 pageSize: 'A4',
-                orientation: 'portrait',
+                // orientation: 'portrait',
                 margin: {
                     top: 0,
                     right: 0,
@@ -237,8 +237,8 @@ async function downloadUrl(url) {
         // 方式2:先合并pdf,后合并docx
         if(files?.length>4){
             let pdfList = files.slice(1);
-            let mergedFile = await mergePdfListReduce(pdfList,convertOpts) 
-            convertOpts.files = [files[0],mergedFile]
+            let mergedFileList = await mergePdfListReduce(pdfList,convertOpts) 
+            convertOpts.files = [files[0],...mergedFileList]
             // console.log(convertOpts)
             pdfBuffer = await LibreOffice.convert(convertOpts);
         }else{
@@ -269,7 +269,7 @@ const sizeOf = require("image-size");
  * @returns 
  */
 export async function mergePdfListReduce(pdfList,convertOpts){
-    // console.log("pdfList",pdfList)
+    console.log("pdfList",pdfList)
     let mergeList = []
     let plist = []
     let length = pdfList.length
@@ -277,7 +277,10 @@ 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()]; // 每次合并三个
+        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()
+        ]; // 每次合并四个
         files=files?.filter(item=>item);
         // console.log(files)
         plist.push(new Promise(async resolve=>{
@@ -300,8 +303,8 @@ export async function mergePdfListReduce(pdfList,convertOpts){
         mergeList = await Promise.all(plist);
     }
     // console.log("mergeList",mergeList)
-    if(mergeList?.length==1){
-        return mergeList[0];
+    if(mergeList?.length<=3){
+        return mergeList;
     }else{
         // console.log("mergePdfListReduce continue:",mergeList)
         return await mergePdfListReduce(mergeList,convertOpts)