|
@@ -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 // 按原有顺序整理
|
|
@@ -341,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)){
|
|
@@ -356,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,
|
|
@@ -372,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({
|
|
@@ -412,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()
|
|
|
// 写出流
|