|
@@ -194,8 +194,7 @@ async function downloadUrl(url) {
|
|
|
}
|
|
|
files = [...files,...filePathList]
|
|
|
|
|
|
- console.log("files",files)
|
|
|
- let pdfBuffer = await LibreOffice.convert({
|
|
|
+ let convertOpts = {
|
|
|
files,
|
|
|
properties: {
|
|
|
// 设置页面属性,例如纸张大小和方向
|
|
@@ -218,7 +217,35 @@ async function downloadUrl(url) {
|
|
|
// reduceImageResolution: false,
|
|
|
// quality: 90, // JPG 导出质量
|
|
|
// maxImageResolution: 300 // 最大图像分辨率
|
|
|
- });
|
|
|
+ }
|
|
|
+ // console.log("convertOpts",convertOpts)
|
|
|
+
|
|
|
+ let pdfBuffer
|
|
|
+ // 方式1:逐个合并
|
|
|
+ // let pdfBuffer
|
|
|
+ // for (let index = 1; index < files.length; index++) {
|
|
|
+ // let file = files[index];
|
|
|
+ // if(pdfBuffer){
|
|
|
+ // convertOpts.files = [{data:pdfBuffer,ext:"pdf"},file]
|
|
|
+ // }else{
|
|
|
+ // convertOpts.files = [file]
|
|
|
+ // }
|
|
|
+ // pdfBuffer = await LibreOffice.convert(convertOpts);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // 方式2:先合并pdf,后合并docx
|
|
|
+ if(files?.length>4){
|
|
|
+ let pdfList = files.slice(1);
|
|
|
+ let mergedFile = await mergePdfListReduce(pdfList,convertOpts)
|
|
|
+ convertOpts.files = [files[0],mergedFile]
|
|
|
+ // console.log(convertOpts)
|
|
|
+ pdfBuffer = await LibreOffice.convert(convertOpts);
|
|
|
+ }else{
|
|
|
+ pdfBuffer = await LibreOffice.convert(convertOpts);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 方式3:全部合并
|
|
|
+ // let pdfBuffer = await LibreOffice.convert(convertOpts);
|
|
|
|
|
|
// 将 Buffer 写入输出文件
|
|
|
fs.writeFileSync(outputPath, pdfBuffer);
|
|
@@ -234,6 +261,40 @@ module.exports.docxToPdf = docxToPdf
|
|
|
const ImageModule = require("@slosarek/docxtemplater-image-module-free");
|
|
|
const sizeOf = require("image-size");
|
|
|
|
|
|
+/**
|
|
|
+ * 每三个pdf合并一次,直到合并为一个pdf为止
|
|
|
+ * @param {} pdfList
|
|
|
+ * @param {*} convertOpts
|
|
|
+ * @returns
|
|
|
+ */
|
|
|
+export async function mergePdfListReduce(pdfList,convertOpts){
|
|
|
+ let mergeList = []
|
|
|
+ for (let index = 0; index < pdfList.length; index++) {
|
|
|
+ let file = pdfList.shift();
|
|
|
+ if(!file) break;
|
|
|
+ let files = [file,pdfList.shift(),pdfList.shift()]; // 每次合并三个
|
|
|
+ files=files?.filter(item=>item);
|
|
|
+
|
|
|
+ if(files?.length==1){ // 单文件直接加载 自动获取后缀
|
|
|
+ let onefile = files[0]
|
|
|
+ if(!onefile?.ext){
|
|
|
+ onefile = {data:fs.readFileSync(onefile),ext:path.extname(files[0]).slice(1)}
|
|
|
+ }
|
|
|
+ mergeList.push(onefile);
|
|
|
+ }else{ // 多文件合并
|
|
|
+ convertOpts.files = files;
|
|
|
+ // console.log("多文件合并",convertOpts)
|
|
|
+ let mergeBuffer = await LibreOffice.convert(convertOpts);
|
|
|
+ mergeList.push({data:mergeBuffer,ext:"pdf"});
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(mergeList?.length==1){
|
|
|
+ return mergeList[0];
|
|
|
+ }else{
|
|
|
+ // console.log("mergePdfListReduce continue:",mergeList)
|
|
|
+ return await mergePdfListReduce(mergeList,convertOpts)
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
export function renderDocx(inputDocxPath, outputDocxName, options){
|
|
|
let imageOptions = {
|