76 lines
2.0 KiB
JavaScript
76 lines
2.0 KiB
JavaScript
const pdf = require("pdf-creator-node");
|
|
const QRCode = require("qrcode");
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
// Read HTML Template
|
|
const html = fs.readFileSync(path.join(__dirname, "./template.html"), "utf8");
|
|
|
|
// from https://stackoverflow.com/questions/58325771/how-to-generate-random-hex-string-in-javascript
|
|
const genRanHex = (size) =>
|
|
[...Array(size)]
|
|
.map(() => Math.floor(Math.random() * 16).toString(16))
|
|
.join("");
|
|
|
|
const options = {
|
|
format: "A4",
|
|
orientation: "portrait",
|
|
border: "0mm",
|
|
};
|
|
|
|
function generate_qrcodes(urldata) {
|
|
return QRCode.toDataURL([{data: urldata, mode: 'Byte'}], { errorCorrectionLevel: 'L', margin: 0 });
|
|
}
|
|
|
|
const allPromises = [];
|
|
const promiseData = [];
|
|
for (let i = 0; i < 6 * 8; i++) {
|
|
const randData = genRanHex(8);
|
|
allPromises.push(generate_qrcodes("https://inventory.internal.thegreydiamond.de/" + randData));
|
|
promiseData.push(randData);
|
|
}
|
|
|
|
Promise.all(allPromises).then((results) => {
|
|
// console.log(results);
|
|
|
|
const data = [];
|
|
|
|
// Group qr codes into 8 per list element
|
|
for (let i = 0; i < results.length; i += 6) {
|
|
data.push({
|
|
q0: {url: results[i], data: promiseData[i]},
|
|
q1: {url: results[i + 1], data: promiseData[i + 1]},
|
|
q2: {url: results[i + 2], data: promiseData[i + 2]},
|
|
q3: {url: results[i + 3], data: promiseData[i + 3]},
|
|
q4: {url: results[i + 4], data: promiseData[i + 4]},
|
|
q5: {url: results[i + 5], data: promiseData[i + 5]},
|
|
});
|
|
}
|
|
|
|
// console.log(users);
|
|
|
|
const document = {
|
|
html: html,
|
|
data: { data: data },
|
|
path: "./output.pdf",
|
|
type: "",
|
|
};
|
|
// By default a file is created but you could switch between Buffer and Streams by using "buffer" or "stream" respectively.
|
|
|
|
/* var document = {
|
|
html: html,
|
|
data: {
|
|
users,
|
|
},
|
|
path: "./output.pdf",
|
|
type: "", // "stream" || "buffer" || "" ("" defaults to)
|
|
}; */
|
|
|
|
pdf.create(document, options)
|
|
.then((res) => {
|
|
console.log(res);
|
|
})
|
|
.catch((error) => {
|
|
console.error(error);
|
|
});
|
|
});
|