-
Notifications
You must be signed in to change notification settings - Fork 1
/
badgePrint.js
105 lines (88 loc) · 2.99 KB
/
badgePrint.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
var fs = require('fs')
function printParticipant(doc, participant, side) {
doc.addPage();
var height = doc.page.height;
doc.image(participant.image, 0, 0, {height, width:doc.page.width});
if (!participant.sessionInfo || side === "back") {
var qrWidth = 40;
doc.image(
qr.imageSync(participant.contactCard, {type: 'png'}),
(doc.page.width-qrWidth)/2, height-80-qrWidth, { width: qrWidth });
}
var width = doc.page.width-20;
var margin = 10;
doc.font('fonts/Roboto/Roboto-Bold.ttf')
.fontSize(36)
.fillColor("#0E1131");
if (doc.widthOfString(participant.fullName) > width) {
doc.fontSize(30);
if (doc.widthOfString(participant.fullName) > width) {
doc.fontSize(26);
//console.log(participant.fullName, doc.widthOfString(participant.fullName));
}
}
doc
.text(participant.fullName, margin, 180, {align: "center", height, width});
if (participant.company) {
doc.font('fonts/Roboto/Roboto-Regular.ttf')
.fontSize(24)
.fillColor("#0E1131");
if (doc.widthOfString(participant.company) > width) {
doc.fontSize(20);
if (doc.widthOfString(participant.company) > width) {
doc.fontSize(16);
}
}
doc.text(participant.company, {align: "center", height, width});
}
var sessionInfo = participant.sessionInfo;
if (sessionInfo && side === "front") {
doc.moveDown();
var sessionTime = sessionInfo.date + ", " + sessionInfo.timeslot + ", " + sessionInfo.track;
doc.font('fonts/Roboto/Roboto-Medium.ttf')
.fontSize(14)
.fillColor("#0E1131");
if (doc.widthOfString(sessionInfo.title) > width) {
//console.log(sessionInfo.title, doc.widthOfString(sessionInfo.title));
doc.fontSize(10);
}
doc.text(sessionInfo.title, {align: "center", height, width});
doc.fontSize(12)
.fillColor("#6D6E6F")
.text(sessionTime, {align: "center", height, width});
}
}
var PDFDocument = require('pdfkit');
var qr = require('qr-image')
function badgePrint(participants, filename) {
// Create a document
doc = new PDFDocument({
size: 'A6',
autoFirstPage: false
});
doc.pipe(fs.createWriteStream(filename));
for (var participant of participants) {
printParticipant(doc, participant, "front");
// Back page
printParticipant(doc, participant, "back");
}
// Finalize PDF file
doc.end();
}
function blankBadgePrint(count, filename) {
doc = new PDFDocument({
size: 'A6',
autoFirstPage: false
});
doc.pipe(fs.createWriteStream(filename));
var image = 'images/badge-attendee.png';
for (var i=0; i<count; i++) {
doc.addPage();
var height = doc.page.height;
doc.image(image, 0, 0, {height, width:doc.page.width});
doc.addPage();
doc.image(image, 0, 0, {height, width:doc.page.width});
}
doc.end();
}
module.exports = {badgePrint, blankBadgePrint};