|
@@ -5,16 +5,10 @@ const path = require("path");
|
|
const CsvReadableStream = require("csv-reader");
|
|
const CsvReadableStream = require("csv-reader");
|
|
const { Transform } = require("stream");
|
|
const { Transform } = require("stream");
|
|
|
|
|
|
-const template = fs.readFileSync(
|
|
|
|
- path.resolve(__dirname, "template.docx"),
|
|
|
|
- "binary"
|
|
|
|
-);
|
|
|
|
-const templateEmpty = fs.readFileSync(
|
|
|
|
- path.resolve(__dirname, "template-empty.docx"),
|
|
|
|
- "binary"
|
|
|
|
-);
|
|
|
|
|
|
+const [dataFilePath, templateFilePath] = process.argv.slice(2);
|
|
|
|
|
|
-const inputStream = fs.createReadStream("data.csv", "utf-8");
|
|
|
|
|
|
+const template = fs.readFileSync(templateFilePath, "binary");
|
|
|
|
+const inputStream = fs.createReadStream(dataFilePath, "utf-8");
|
|
|
|
|
|
fs.rmSync("./docx", { recursive: true, force: true });
|
|
fs.rmSync("./docx", { recursive: true, force: true });
|
|
fs.mkdirSync("./docx", { recursive: true });
|
|
fs.mkdirSync("./docx", { recursive: true });
|
|
@@ -22,65 +16,50 @@ fs.mkdirSync("./docx", { recursive: true });
|
|
fs.rmSync("./result", { recursive: true, force: true });
|
|
fs.rmSync("./result", { recursive: true, force: true });
|
|
fs.mkdirSync("./result", { recursive: true });
|
|
fs.mkdirSync("./result", { recursive: true });
|
|
|
|
|
|
-console.log("START");
|
|
|
|
|
|
+console.log("ЭТАП ГЕНЕРАЦИИ ПО ШАБЛОНУ");
|
|
|
|
|
|
// converting docx to pdf stream
|
|
// converting docx to pdf stream
|
|
-
|
|
|
|
-const COUNT = 507;
|
|
|
|
let counter = 0;
|
|
let counter = 0;
|
|
|
|
|
|
// Transform stream that takes csv data and converts it to docx
|
|
// Transform stream that takes csv data and converts it to docx
|
|
// and then to pdf
|
|
// and then to pdf
|
|
const pdfStream = new Transform({
|
|
const pdfStream = new Transform({
|
|
objectMode: true,
|
|
objectMode: true,
|
|
- transform: async (row, encoding, callback) => {
|
|
|
|
|
|
+ transform: async (row, _encoding, callback) => {
|
|
const templateZip = new PizZip(template);
|
|
const templateZip = new PizZip(template);
|
|
- const templateEmptyZip = new PizZip(templateEmpty);
|
|
|
|
-
|
|
|
|
- const [NAME, PLACE, NOMINATOIN, TEACHER] = row;
|
|
|
|
|
|
|
|
- const isEmpty = !NOMINATOIN || !TEACHER;
|
|
|
|
- const doc = new Docxtemplater(isEmpty ? templateEmptyZip : templateZip, {
|
|
|
|
|
|
+ const doc = new Docxtemplater(templateZip, {
|
|
paragraphLoop: true,
|
|
paragraphLoop: true,
|
|
linebreaks: true,
|
|
linebreaks: true,
|
|
errorLogging: true,
|
|
errorLogging: true,
|
|
});
|
|
});
|
|
|
|
|
|
- doc.render(
|
|
|
|
- isEmpty
|
|
|
|
- ? { NAME, PLACE }
|
|
|
|
- : {
|
|
|
|
- NAME,
|
|
|
|
- PLACE,
|
|
|
|
- NOMINATOIN,
|
|
|
|
- TEACHER,
|
|
|
|
- }
|
|
|
|
- );
|
|
|
|
|
|
+ doc.render(row);
|
|
|
|
|
|
const buffer = doc.getZip().generate({
|
|
const buffer = doc.getZip().generate({
|
|
type: "nodebuffer",
|
|
type: "nodebuffer",
|
|
});
|
|
});
|
|
- const fileName = `${isEmpty ? `e-${counter}` : counter}-${NAME.split(" ")
|
|
|
|
- .join("-")
|
|
|
|
- .toLowerCase()}`;
|
|
|
|
-
|
|
|
|
- // const html = await mammoth.convertToHtml({ buffer });
|
|
|
|
|
|
+ const fileName = `${row.NOMINATION.replace(
|
|
|
|
+ "Изобразительное искусство",
|
|
|
|
+ "ИЗО"
|
|
|
|
+ ).replace("Декоративно-прикладное искусство", "ДПИ")} ${row.NAME}`;
|
|
|
|
|
|
fs.writeFileSync(
|
|
fs.writeFileSync(
|
|
- path.resolve(__dirname, "result", `${fileName}.docx`),
|
|
|
|
|
|
+ path.resolve(__dirname, "docx", `${fileName}.docx`),
|
|
buffer
|
|
buffer
|
|
);
|
|
);
|
|
|
|
|
|
counter += 1;
|
|
counter += 1;
|
|
- console.log(`PROCESS ${counter}/${COUNT}`);
|
|
|
|
|
|
+ console.log(`ГЕНЕРИРОВАНО ${counter}`);
|
|
|
|
|
|
callback(null, null);
|
|
callback(null, null);
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
|
|
inputStream
|
|
inputStream
|
|
- .pipe(new CsvReadableStream({ trim: true, delimiter: ";", skipHeader: true }))
|
|
|
|
|
|
+ .pipe(new CsvReadableStream({ trim: true, delimiter: ";", asObject: true }))
|
|
.pipe(pdfStream)
|
|
.pipe(pdfStream)
|
|
- .on("end", () => {
|
|
|
|
- console.log("DONE");
|
|
|
|
|
|
+ .on("finish", () => {
|
|
|
|
+ console.log("ГЕНЕРАЦИЯ ЗАВЕРШЕНА");
|
|
|
|
+ console.log("--------------------------------------------");
|
|
});
|
|
});
|