|
@@ -6,11 +6,17 @@ const shell = require('shelljs');
|
|
|
|
|
|
const crypto = require('crypto');
|
|
|
|
|
|
+// 文档模板生成
|
|
|
+const PizZip = require("pizzip");
|
|
|
+const Docxtemplater = require("docxtemplater");
|
|
|
+
|
|
|
+// 文档转换
|
|
|
import { Chromiumly } from "chromiumly";
|
|
|
Chromiumly.configure({ endpoint: "http://8.140.98.43/docs" });
|
|
|
const { LibreOffice } = require("chromiumly");
|
|
|
-// LibreOffice.configure({ endpoint: "http://8.140.98.43/docs" });
|
|
|
-const tempDir = path.join(__dirname , "temp")
|
|
|
+const { PDFEngines } = require("chromiumly");
|
|
|
+const tempDir = path.join(__dirname , "temp");
|
|
|
+if(!fs.existsSync(tempDir)){fs.mkdirSync(tempDir)};
|
|
|
|
|
|
const OSS = require("ali-oss");
|
|
|
const ALI_OSS_BUCKET = process.env.ALI_OSS_BUCKET || "hep-textbook"
|
|
@@ -106,17 +112,25 @@ module.exports.createZip = createZip
|
|
|
|
|
|
let files = [
|
|
|
// docxPath
|
|
|
- { data: docxBuffer, ext: "doc" },
|
|
|
+ { data: docxBuffer, ext: "docx" },
|
|
|
];
|
|
|
console.log(files)
|
|
|
let pdfBuffer = await LibreOffice.convert({
|
|
|
files,
|
|
|
- // properties: {
|
|
|
- // // 你可以在这里设置页面属性
|
|
|
- // },
|
|
|
- pdfa: false, // PDF/A 选项
|
|
|
- // pdfUA: false, // PDF/UA 选项
|
|
|
- // merge: false, // 是否合并PDF
|
|
|
+ properties: {
|
|
|
+ // 设置页面属性,例如纸张大小和方向
|
|
|
+ pageSize: 'A4',
|
|
|
+ orientation: 'portrait',
|
|
|
+ margin: {
|
|
|
+ top: 10,
|
|
|
+ right: 10,
|
|
|
+ bottom: 10,
|
|
|
+ left: 10
|
|
|
+ }
|
|
|
+ },
|
|
|
+ pdfa: false, // 根据需要设置
|
|
|
+ pdfUA: false, // 根据需要设置
|
|
|
+ merge: false, // 如果只转换一个文件,设置为false
|
|
|
// metadata: {
|
|
|
// // 你可以在这里添加元数据
|
|
|
// },
|
|
@@ -129,7 +143,7 @@ module.exports.createZip = createZip
|
|
|
// 将 Buffer 写入输出文件
|
|
|
fs.writeFileSync(outputPath, pdfBuffer);
|
|
|
console.log(`成功输出 ${outputPath}`);
|
|
|
- return pdfPath
|
|
|
+ return outputPath
|
|
|
} catch (error) {
|
|
|
console.error('转换失败:', error);
|
|
|
return null
|
|
@@ -137,6 +151,38 @@ module.exports.createZip = createZip
|
|
|
}
|
|
|
module.exports.docxToPdf = docxToPdf
|
|
|
|
|
|
+
|
|
|
+export function renderDocx(inputDocxPath, outputDocxName, options){
|
|
|
+ let outputDocxPath = path.join(tempDir,outputDocxName)
|
|
|
+ // Load the docx file as binary content
|
|
|
+ let content = fs.readFileSync(
|
|
|
+ inputDocxPath,
|
|
|
+ "binary"
|
|
|
+ );
|
|
|
+
|
|
|
+ // Unzip the content of the file
|
|
|
+ let zip = new PizZip(content);
|
|
|
+ let doc = new Docxtemplater(zip, {
|
|
|
+ paragraphLoop: true,
|
|
|
+ linebreaks: true,
|
|
|
+ });
|
|
|
+
|
|
|
+ // Render the document (Replace {first_name} by John, {last_name} by Doe, ...)
|
|
|
+ doc.render(options);
|
|
|
+
|
|
|
+ // Get the zip document and generate it as a nodebuffer
|
|
|
+ let buf = doc.getZip().generate({
|
|
|
+ type: "nodebuffer",
|
|
|
+ // compression: DEFLATE adds a compression step.
|
|
|
+ // For a 50MB output document, expect 500ms additional CPU time
|
|
|
+ compression: "DEFLATE",
|
|
|
+ });
|
|
|
+
|
|
|
+ // buf is a nodejs Buffer, you can either write it to a
|
|
|
+ // file or res.send it with express for example.
|
|
|
+ fs.writeFileSync(outputDocxPath, buf);
|
|
|
+ return outputDocxPath
|
|
|
+}
|
|
|
/**
|
|
|
* docx 替换模板字符串内容
|
|
|
* @example
|