buechermarktHelpers/students.js

123 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-05-29 13:17:24 +00:00
var PDFDocument = require("pdfkit");
var doc = new PDFDocument({ size: "A4", autoFirstPage: false });
var fs = require("fs");
var bluebird = require("bluebird");
var ets = []
var height = 841.89;
var width = 595.28;
for (var i = 2000; i < 4 * 100 + 2000; i++) {
ets.push(i.toString());
}
//const barcode = require("barcode");
var bwipjs = require('bwip-js');
const margin = 40;
//A4: [595.28, 841.89],
// 28.29
function print(xo, yo, bc) {
xo = xo + 40;
yo = yo + 40;
doc.fontSize(14);
doc.text("Bitte alle Felder leserlich ausfüllen. \nDie Schüler-ID bitte auf \nALLE Bücher schreiben!!", xo, yo);
doc.fontSize(16);
yo += 45;
var h = yo + 40;
doc.moveTo(xo, h).lineTo(xo + 180, h);
doc.text("Vorame", xo, h + 5);
doc.stroke();
h += 50;
doc.moveTo(xo, h).lineTo(xo + 180, h);
doc.text("Nachname", xo, h + 5);
doc.stroke();
h += 50;
doc.moveTo(xo, h).lineTo(xo + 180, h);
doc.text("Klasse", xo, h + 5);
doc.stroke();
h += 50;
doc.moveTo(xo, h).lineTo(xo + 180, h);
doc.text("Telefon", xo, h + 5);
doc.stroke();
h += 50;
doc.moveTo(xo, h).lineTo(xo + 180, h);
doc.text("E-Mail", xo, h + 5);
doc.stroke();
//doc.rect(xo, yo, ewidth, eheight);
//doc.stroke();
//return new Promise((resolve)=>{resolve()});
//Barcode:
var imagewidth = 240;
var imageheight = 20;
return new Promise((resolve, reject) => {
bwipjs.toBuffer({
bcid: "code128",
text: bc,
scale: 1,
height: imageheight,
width: imagewidth,
includetext: true,
textxalign: "center",
textsize: 15
}, (err, png) => {
if (err) return console.log(err);
doc.image(png, xo + 23 - 40, h + 23, { height: 50, width: imagewidth });
resolve();
});
});
}
function page(items, callback, index) { //bekommt ein array mit 12 ids
doc.addPage({ margin: 0, size: "A4" });
doc.moveTo(width / 2, 0).lineTo(width / 2, height);
doc.stroke();
doc.moveTo(0, height / 2).lineTo(width, height / 2);
doc.stroke();
var i = 0;
var proms = [];
if (i < items.length) {
proms.push(print(0, 0, items[0]));
i++;
}
if (i < items.length) {
proms.push(print(width / 2, 0, items[1]));
i++;
}
if (i < items.length) {
proms.push(print(0, height / 2, items[2]));
i++;
}
if (i < items.length) {
proms.push(print(width / 2, height / 2, items[3]));
i++;
}
Promise.all(proms).then(() => {
callback(index + 4);
});
}
function printpack(index) {
if (index >= ets.length) {
doc.pipe(fs.createWriteStream(__dirname + "/student_form.pdf"));
doc.end();
return;
}
var i = [];
for (var g = 0; g < 4; g++) {
if (index + g >= ets.length) break;
i.push(ets[index + g]);
}
page(i, printpack, index);
}
printpack(0);