diff --git a/packages/plugins/@tachybase/plugin-print-template/package.json b/packages/plugins/@tachybase/plugin-print-template/package.json index 79e1a4c81..6b25615d5 100644 --- a/packages/plugins/@tachybase/plugin-print-template/package.json +++ b/packages/plugins/@tachybase/plugin-print-template/package.json @@ -19,6 +19,7 @@ }, "peerDependencies": { "@tachybase/client": "workspace:*", + "@tachybase/database": "workspace:*", "@tachybase/server": "workspace:*", "@tachybase/test": "workspace:*" } diff --git a/packages/plugins/@tachybase/plugin-print-template/src/server/actions/printTemplates.ts b/packages/plugins/@tachybase/plugin-print-template/src/server/actions/printTemplates.ts index abaa80ba7..d0a8efe0f 100644 --- a/packages/plugins/@tachybase/plugin-print-template/src/server/actions/printTemplates.ts +++ b/packages/plugins/@tachybase/plugin-print-template/src/server/actions/printTemplates.ts @@ -97,3 +97,17 @@ function generateDocxFromTemplate(templatePath, data): Buffer { 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); +}; diff --git a/packages/plugins/@tachybase/plugin-print-template/src/server/actions/producer.ts b/packages/plugins/@tachybase/plugin-print-template/src/server/actions/producer.ts index ba0ca88cb..c2bddcb91 100644 --- a/packages/plugins/@tachybase/plugin-print-template/src/server/actions/producer.ts +++ b/packages/plugins/@tachybase/plugin-print-template/src/server/actions/producer.ts @@ -46,10 +46,11 @@ export const addConversionJob = async (ctx: Context) => { 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 文件路径 ctx.body = { jobId: job.id }; diff --git a/packages/plugins/@tachybase/plugin-print-template/src/server/plugin.ts b/packages/plugins/@tachybase/plugin-print-template/src/server/plugin.ts index 33ed98e33..4813f851d 100644 --- a/packages/plugins/@tachybase/plugin-print-template/src/server/plugin.ts +++ b/packages/plugins/@tachybase/plugin-print-template/src/server/plugin.ts @@ -1,11 +1,12 @@ import { exec } from 'child_process'; import path from 'path'; import util from 'util'; +import Database from '@tachybase/database'; import { Plugin } from '@tachybase/server'; import { Worker } from 'bullmq'; -import { generate, getTags } from './actions/printTemplates'; +import { generate, getTags, readPDF } from './actions/printTemplates'; import { addConversionJob } from './actions/producer'; const execPromise = util.promisify(exec); @@ -16,7 +17,7 @@ const redisOptions = { password: process.env.REDIS_PASSWORD || '', }; -async function convertDocxToPdf(wordFilePath: string, outputDir: string, job: any): Promise { +async function convertDocxToPdf(wordFilePath: string, outputDir: string, job: any, db: Database): Promise { try { const fileName = path.basename(wordFilePath, '.docx') + '.pdf'; const pdfFilePath = path.join(outputDir, fileName); @@ -40,6 +41,15 @@ async function convertDocxToPdf(wordFilePath: string, outputDir: string, job: an // 更新进度到 100%:转换完成 job.updateProgress(100); + db.getRepository('templateManage').update({ + filter: { + id: job.data.id, + }, + values: { + pdf_SavePath: pdfFilePath, + }, + }); + return pdfFilePath; } catch (error) { console.error('Error during conversion:', error); @@ -61,6 +71,7 @@ export class PluginPrintTemplateServer extends Plugin { generate, getTags, addConversionJob, + readPDF, }, }); this.app.acl.allow('printTemplates', '*', 'public'); @@ -78,7 +89,7 @@ export class PluginPrintTemplateServer extends Plugin { const { wordFilePath, outputDir } = job.data; // 自动更新进度 - const pdfFilePath = await convertDocxToPdf(wordFilePath, outputDir, job); + const pdfFilePath = await convertDocxToPdf(wordFilePath, outputDir, job, this.app.db); // 返回 PDF 文件路径 return { pdfFilePath }; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c38e6bc6d..5bbc26d03 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3559,6 +3559,9 @@ importers: '@tachybase/client': specifier: workspace:* version: link:../../../core/client + '@tachybase/database': + specifier: workspace:* + version: link:../../../core/database '@tachybase/server': specifier: workspace:* version: link:../../../core/server