fix: 批量生成pdf且可下载pdf (#1394)
Reviewed-on: daoyoucloud/tachybase#1394 Reviewed-by: sealday <zhanglin@daoyoucloud.com> Co-authored-by: yoona <1486343814@qq.com> Co-committed-by: yoona <1486343814@qq.com>
This commit is contained in:
parent
28ef9b315f
commit
0ca2b22c25
@ -19,6 +19,7 @@
|
|||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"@tachybase/client": "workspace:*",
|
"@tachybase/client": "workspace:*",
|
||||||
|
"@tachybase/database": "workspace:*",
|
||||||
"@tachybase/server": "workspace:*",
|
"@tachybase/server": "workspace:*",
|
||||||
"@tachybase/test": "workspace:*"
|
"@tachybase/test": "workspace:*"
|
||||||
}
|
}
|
||||||
|
@ -97,3 +97,17 @@ function generateDocxFromTemplate(templatePath, data): Buffer {
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const readPDF = async (ctx: Context) => {
|
||||||
|
const id = ctx.action.params.id;
|
||||||
|
const repo = ctx.db.getRepository('templateManage');
|
||||||
|
const template: Model = await repo.findOne({
|
||||||
|
filter: { id },
|
||||||
|
appends: ['template'],
|
||||||
|
});
|
||||||
|
|
||||||
|
const rawTemplate = template.get();
|
||||||
|
const path = rawTemplate.pdf_SavePath;
|
||||||
|
|
||||||
|
ctx.body = fs.readFileSync(path);
|
||||||
|
};
|
||||||
|
@ -46,10 +46,11 @@ export const addConversionJob = async (ctx: Context) => {
|
|||||||
|
|
||||||
console.log('Generated DOCX file path:', filePath);
|
console.log('Generated DOCX file path:', filePath);
|
||||||
|
|
||||||
const outputDir = '/root/tachybase/storage/uploads';
|
const id = ctx.action.params.id;
|
||||||
|
const outputDir = path.join(process.env.PWD, 'storage/uploads');
|
||||||
|
|
||||||
// 添加作业到队列
|
// 添加作业到队列
|
||||||
const job = await pdfConversionQueue.add('convert', { wordFilePath: filePath, outputDir });
|
const job = await pdfConversionQueue.add('convert', { wordFilePath: filePath, outputDir, id });
|
||||||
|
|
||||||
// 返回作业 ID 和 PDF 文件路径
|
// 返回作业 ID 和 PDF 文件路径
|
||||||
ctx.body = { jobId: job.id };
|
ctx.body = { jobId: job.id };
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
import { exec } from 'child_process';
|
import { exec } from 'child_process';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import util from 'util';
|
import util from 'util';
|
||||||
|
import Database from '@tachybase/database';
|
||||||
import { Plugin } from '@tachybase/server';
|
import { Plugin } from '@tachybase/server';
|
||||||
|
|
||||||
import { Worker } from 'bullmq';
|
import { Worker } from 'bullmq';
|
||||||
|
|
||||||
import { generate, getTags } from './actions/printTemplates';
|
import { generate, getTags, readPDF } from './actions/printTemplates';
|
||||||
import { addConversionJob } from './actions/producer';
|
import { addConversionJob } from './actions/producer';
|
||||||
|
|
||||||
const execPromise = util.promisify(exec);
|
const execPromise = util.promisify(exec);
|
||||||
@ -16,7 +17,7 @@ const redisOptions = {
|
|||||||
password: process.env.REDIS_PASSWORD || '',
|
password: process.env.REDIS_PASSWORD || '',
|
||||||
};
|
};
|
||||||
|
|
||||||
async function convertDocxToPdf(wordFilePath: string, outputDir: string, job: any): Promise<string> {
|
async function convertDocxToPdf(wordFilePath: string, outputDir: string, job: any, db: Database): Promise<string> {
|
||||||
try {
|
try {
|
||||||
const fileName = path.basename(wordFilePath, '.docx') + '.pdf';
|
const fileName = path.basename(wordFilePath, '.docx') + '.pdf';
|
||||||
const pdfFilePath = path.join(outputDir, fileName);
|
const pdfFilePath = path.join(outputDir, fileName);
|
||||||
@ -40,6 +41,15 @@ async function convertDocxToPdf(wordFilePath: string, outputDir: string, job: an
|
|||||||
// 更新进度到 100%:转换完成
|
// 更新进度到 100%:转换完成
|
||||||
job.updateProgress(100);
|
job.updateProgress(100);
|
||||||
|
|
||||||
|
db.getRepository('templateManage').update({
|
||||||
|
filter: {
|
||||||
|
id: job.data.id,
|
||||||
|
},
|
||||||
|
values: {
|
||||||
|
pdf_SavePath: pdfFilePath,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
return pdfFilePath;
|
return pdfFilePath;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error during conversion:', error);
|
console.error('Error during conversion:', error);
|
||||||
@ -61,6 +71,7 @@ export class PluginPrintTemplateServer extends Plugin {
|
|||||||
generate,
|
generate,
|
||||||
getTags,
|
getTags,
|
||||||
addConversionJob,
|
addConversionJob,
|
||||||
|
readPDF,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
this.app.acl.allow('printTemplates', '*', 'public');
|
this.app.acl.allow('printTemplates', '*', 'public');
|
||||||
@ -78,7 +89,7 @@ export class PluginPrintTemplateServer extends Plugin {
|
|||||||
const { wordFilePath, outputDir } = job.data;
|
const { wordFilePath, outputDir } = job.data;
|
||||||
|
|
||||||
// 自动更新进度
|
// 自动更新进度
|
||||||
const pdfFilePath = await convertDocxToPdf(wordFilePath, outputDir, job);
|
const pdfFilePath = await convertDocxToPdf(wordFilePath, outputDir, job, this.app.db);
|
||||||
|
|
||||||
// 返回 PDF 文件路径
|
// 返回 PDF 文件路径
|
||||||
return { pdfFilePath };
|
return { pdfFilePath };
|
||||||
|
@ -3559,6 +3559,9 @@ importers:
|
|||||||
'@tachybase/client':
|
'@tachybase/client':
|
||||||
specifier: workspace:*
|
specifier: workspace:*
|
||||||
version: link:../../../core/client
|
version: link:../../../core/client
|
||||||
|
'@tachybase/database':
|
||||||
|
specifier: workspace:*
|
||||||
|
version: link:../../../core/database
|
||||||
'@tachybase/server':
|
'@tachybase/server':
|
||||||
specifier: workspace:*
|
specifier: workspace:*
|
||||||
version: link:../../../core/server
|
version: link:../../../core/server
|
||||||
|
Loading…
Reference in New Issue
Block a user