|
@@ -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()
|
|
@@ -314,18 +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);
|
|
|
- resolve({data:mergeBuffer,ext:"pdf"})
|
|
|
+ // 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)
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
}))
|
|
|
}
|
|
@@ -333,7 +352,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)
|
|
@@ -341,7 +360,20 @@ 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 = {
|
|
|
getImage(tagValue,tagName) {
|
|
|
if(!fs.existsSync(tagValue)){
|