Files
przekroj/pages/api/generateDocx.js
Cyber Panel 5c11a289df Add uziom generation script and update wet input documents
- Created a new Python script `uziom.py` for generating DXF drawings of grounding systems based on user input parameters.
- Added detailed documentation in `wet_input_15.docx` and `wet_input_3.docx` for the design of grounding systems, including calculations for resistance and resistivity measurements.
- Included placeholders for dynamic data insertion in the documents to facilitate project-specific customization.
2025-07-01 11:14:49 +02:00

87 lines
2.0 KiB
JavaScript

import PizZip from "pizzip";
import Docxtemplater from "docxtemplater";
import fs from "fs";
import path from "path";
export default async (req, res) => {
if (req.method === "POST") {
// Get the data from the request body
const data = req.body;
// log stats
let ip = req.body.ip;
if (ip === undefined)
ip = req.headers["x-forwarded-for"] + " on " + req.headers["user-agent"];
let date_time = new Date();
let string =
fs.readFileSync("ground_log.txt", "utf8") +
"\n" +
"[" +
date_time +
"]" +
ip +
" : " +
data.geo_data +
" : " +
data.in_city;
fs.writeFile("ground_log.txt", string, function (err) {
if (err) {
return console.error(err);
}
});
//get wet coefficient
let input_docx;
if (data.wet_coef == 1.6) {
if (data.rod_len == 2) input_docx = "wet_input_15.docx";
else if (data.rod_len == 3) input_docx = "wet_input_3.docx";
} else {
if (data.rod_len == 2) input_docx = "dry_input_15.docx";
else if (data.rod_len == 3) input_docx = "dry_input_3.docx";
}
// Load the docx file as binary content
const content = fs.readFileSync(
path.resolve(process.cwd(), input_docx),
"binary"
);
const zip = new PizZip(content);
const doc = new Docxtemplater(zip, {
paragraphLoop: true,
linebreaks: true,
});
try {
// Render the document with data
console.log(data);
doc.render(data);
// Generate the document as a buffer
const buf = doc.getZip().generate({
type: "nodebuffer",
compression: "DEFLATE",
});
// Set headers for downloading the file
res.setHeader("Content-Disposition", "attachment; filename=opis.docx");
res.setHeader(
"Content-Type",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
);
// Send the buffer as a response
res.send(buf);
} catch (error) {
// Handle errors
console.error(error);
res.status(500).json({ error: error.message });
}
} else {
// Handle non-POST requests
res.setHeader("Allow", ["POST"]);
res.status(405).end(`Method ${req.method} Not Allowed`);
}
};