|
@@ -77,11 +77,11 @@ module.exports.uploadFileToOSS = uploadFileToOSS
|
|
|
* @param {Array<string>} filePathList - 要打包的文件路径数组
|
|
|
* @param {string} outputZipName - 输出的zip文件名称
|
|
|
*/
|
|
|
- export function createZip(filePathList, outputZipName) {
|
|
|
+ export function createZip(filePathList, outputZipName,options) {
|
|
|
let zipStream = new compressing.zip.Stream();
|
|
|
return new Promise((resolve)=>{
|
|
|
try {
|
|
|
- let outputPath = path.join(tempDir,outputZipName)
|
|
|
+ let outputPath = path.join(options?.tempDir||tempDir,outputZipName)
|
|
|
// 遍历文件路径列表,将每个文件添加到zip流中
|
|
|
for (const filePath of filePathList) {
|
|
|
// 检查文件是否存在
|
|
@@ -125,13 +125,13 @@ module.exports.createZip = createZip
|
|
|
|
|
|
|
|
|
const download = require('download')
|
|
|
-async function downloadUrl(url) {
|
|
|
+async function downloadUrl(url,options) {
|
|
|
// console.log(url)
|
|
|
if(url?.startsWith("/")) {return url};
|
|
|
let md5 = crypto.createHash('md5');
|
|
|
let extname = path.extname(url)?.toLocaleLowerCase();
|
|
|
let filename = md5.update(url).digest('hex') + extname;
|
|
|
- let filepath = path.join(tempDir,filename)
|
|
|
+ let filepath = path.join(options?.tempDir||tempDir,filename)
|
|
|
// console.log(filename,filepath)
|
|
|
try{
|
|
|
// if(fs.existsSync(filepath)){fs.rmSync(filepath)} // 存在则删除
|
|
@@ -161,7 +161,7 @@ async function downloadUrl(url) {
|
|
|
let filePath
|
|
|
plist.push((async ()=>{
|
|
|
try{
|
|
|
- filePath = await downloadUrl(mergeFiles[index]);
|
|
|
+ filePath = await downloadUrl(mergeFiles[index],options);
|
|
|
}catch(err){}
|
|
|
if(filePath){
|
|
|
mergeFileMap[index] = filePath // 按原有顺序整理
|
|
@@ -236,13 +236,24 @@ async function downloadUrl(url) {
|
|
|
// 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
|
|
|
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)
|
|
|
- convertOpts.files = [files[0],...mergedFileList]
|
|
|
+ pdfBuffer = mergedFileList[0]?.data;
|
|
|
+ // convertOpts.files = [files[0],...mergedFileList]
|
|
|
// console.log(convertOpts)
|
|
|
- pdfBuffer = await LibreOffice.convert(convertOpts);
|
|
|
+ // pdfBuffer = await LibreOffice.convert(convertOpts);
|
|
|
}else{
|
|
|
pdfBuffer = await LibreOffice.convert(convertOpts);
|
|
|
}
|
|
@@ -272,6 +283,17 @@ const sizeOf = require("image-size");
|
|
|
*/
|
|
|
export async function mergePdfListReduce(pdfList,convertOpts){
|
|
|
console.log("pdfList",pdfList)
|
|
|
+ // 所有非PDF转PDF
|
|
|
+ for (let index = 0; index < pdfList.length; index++) {
|
|
|
+ let file = pdfList[index];
|
|
|
+ if(file?.toLocaleLowerCase()?.indexOf("pdf")==-1){
|
|
|
+ convertOpts.files = [file];
|
|
|
+ let pdfBuffer = await LibreOffice.convert(convertOpts);
|
|
|
+ fs.writeFileSync(file+".pdf",pdfBuffer)
|
|
|
+ pdfList[index] = file+".pdf"
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
let mergeList = []
|
|
|
let plist = []
|
|
|
let length = pdfList.length
|
|
@@ -280,10 +302,11 @@ export async function mergePdfListReduce(pdfList,convertOpts){
|
|
|
// 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()
|
|
|
]; // 每次合并四个
|
|
|
files=files?.filter(item=>item);
|
|
@@ -318,7 +341,7 @@ export async function mergePdfListReduce(pdfList,convertOpts){
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export function renderDocx(inputDocxPath, outputDocxName, options){
|
|
|
+export function renderDocx(inputDocxPath, outputDocxName, data,options){
|
|
|
let imageOptions = {
|
|
|
getImage(tagValue,tagName) {
|
|
|
if(!fs.existsSync(tagValue)){
|
|
@@ -333,7 +356,7 @@ export function renderDocx(inputDocxPath, outputDocxName, options){
|
|
|
},
|
|
|
};
|
|
|
|
|
|
- let outputDocxPath = path.join(tempDir,outputDocxName)
|
|
|
+ let outputDocxPath = path.join(options?.tempDir||tempDir,outputDocxName)
|
|
|
// Load the docx file as binary content
|
|
|
let content = fs.readFileSync(
|
|
|
inputDocxPath,
|
|
@@ -349,12 +372,12 @@ export function renderDocx(inputDocxPath, outputDocxName, options){
|
|
|
});
|
|
|
|
|
|
// Render the document (Replace {first_name} by John, {last_name} by Doe, ...)
|
|
|
- Object.keys(options).forEach(key=>{ // 除去空值
|
|
|
- if(options[key]==undefined){
|
|
|
- options[key] = ""
|
|
|
+ Object.keys(data).forEach(key=>{ // 除去空值
|
|
|
+ if(data[key]==undefined){
|
|
|
+ data[key] = ""
|
|
|
}
|
|
|
})
|
|
|
- doc.render(options);
|
|
|
+ doc.render(data);
|
|
|
|
|
|
// Get the zip document and generate it as a nodebuffer
|
|
|
let buf = doc.getZip().generate({
|
|
@@ -389,13 +412,13 @@ export function replaceDocx(inputDocxPath, outputDocxPath, options,eventMap) {
|
|
|
// 解压出来的临时目录
|
|
|
let md5 = crypto.createHash('md5');
|
|
|
let outmd5 = md5.update(outputDocxPath).digest('hex')
|
|
|
- let tempDocxPath = path.join(tempDir , outmd5)
|
|
|
+ let tempDocxPath = path.join(options?.tempDir||tempDir , outmd5)
|
|
|
// 要替换的xml文件位置
|
|
|
let tempDocxXMLName = path.join(tempDocxPath,`word/document.xml`)
|
|
|
|
|
|
// 压缩文件夹为文件
|
|
|
let dir_to_docx = (inputFilePath, outputFilePath) => {
|
|
|
- outputFilePath = path.join(tempDir,outputFilePath)
|
|
|
+ outputFilePath = path.join(options?.tempDir||tempDir,outputFilePath)
|
|
|
// 创建压缩流
|
|
|
let zipStream = new compressing.zip.Stream()
|
|
|
// 写出流
|