144 lines
3.2 KiB
JavaScript
144 lines
3.2 KiB
JavaScript
import { TemplateHandler } from "easy-template-x";
|
|
import fs from "fs";
|
|
import path from "path";
|
|
|
|
export async function generateDocxFromTemplate(
|
|
configPath,
|
|
bufferMap,
|
|
templateFolderName,
|
|
orderNo
|
|
) {
|
|
const templateHandler = new TemplateHandler();
|
|
const __dirname = path.dirname(new URL(import.meta.url).pathname);
|
|
const templateName= templateFolderName.split('_')[0];
|
|
const templateFile = fs.readFileSync(
|
|
path.join(__dirname, "templates", `${templateName}.docx`),
|
|
);
|
|
|
|
const content = fs.readFileSync(configPath, 'utf-8');
|
|
const lines = content.split(/\r?\n/).filter(line => line.trim() !== '');
|
|
|
|
if(lines.length < 3){
|
|
throw new Error(`Expected 3 or more lines in config file, but got ${lines.length}`);
|
|
}
|
|
|
|
const [line1 = '', line2 = '', line3= '', line4 = ''] = lines.map(line => line.trim());
|
|
|
|
const description = line1;
|
|
const location = line2;
|
|
const addInfo = line3;
|
|
const addInfo2 = line4;
|
|
|
|
let data = {};
|
|
switch (templateName) {
|
|
case "template1":
|
|
data = {
|
|
orderNumber: orderNo,
|
|
description: description,
|
|
location: location,
|
|
damagedColumnNo: addInfo,
|
|
};
|
|
break;
|
|
|
|
case "template2":
|
|
data = {
|
|
orderNumber: orderNo,
|
|
description: description,
|
|
location: location,
|
|
existingLight: addInfo,
|
|
};
|
|
break;
|
|
|
|
case "template3":
|
|
data = {
|
|
orderNumber: orderNo,
|
|
description: description,
|
|
location: location,
|
|
existingColumnNo: addInfo,
|
|
};
|
|
break;
|
|
|
|
case "template4":
|
|
data = {
|
|
orderNumber: orderNo,
|
|
description: description,
|
|
location: location,
|
|
existingColumnNo: addInfo,
|
|
};
|
|
break;
|
|
|
|
case "template5":
|
|
data = {
|
|
orderNumber: orderNo,
|
|
description: description,
|
|
location: location,
|
|
existingColumnNo: addInfo,
|
|
existingColumnNo2: addInfo2,
|
|
};
|
|
break;
|
|
|
|
case "template6":
|
|
data = {
|
|
orderNumber: orderNo,
|
|
description: description,
|
|
location: location,
|
|
existingJunctionNo: addInfo,
|
|
};
|
|
break;
|
|
|
|
case "template7":
|
|
data = {
|
|
orderNumber: orderNo,
|
|
description: description,
|
|
location: location,
|
|
rustyColumnNo: addInfo,
|
|
};
|
|
break;
|
|
|
|
case "template8":
|
|
data = {
|
|
orderNumber: orderNo,
|
|
description: description,
|
|
location: location,
|
|
rustyColumnNo: addInfo,
|
|
};
|
|
break;
|
|
|
|
case "template9":
|
|
data = {
|
|
orderNumber: orderNo,
|
|
description: description,
|
|
location: location,
|
|
rustyColumnNo: addInfo,
|
|
};
|
|
break;
|
|
|
|
default:
|
|
console.warn(`⚠️ Unknown template: ${templateFolderName}`);
|
|
}
|
|
|
|
for (const [filename, buffer] of Object.entries(bufferMap)) {
|
|
let justFilename = null;
|
|
if(filename.includes('_')){
|
|
justFilename = filename.split("_")[0];
|
|
}else{
|
|
justFilename = filename.split('.')[0];
|
|
}
|
|
|
|
data[`${justFilename}`] = {
|
|
_type: "image",
|
|
source: buffer,
|
|
format: "image/jpeg",
|
|
width: 246,
|
|
height: 186,
|
|
};
|
|
}
|
|
|
|
|
|
const doc = await templateHandler.process(templateFile, data);
|
|
fs.writeFileSync(
|
|
path.join("./reports", `${templateName}-${orderNo}-report.docx`),
|
|
doc,
|
|
);
|
|
}
|