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