This repository has been archived by the owner on Apr 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
117 lines (100 loc) · 2.7 KB
/
index.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
106
107
108
109
110
111
112
113
114
115
116
117
const chunk = require("lodash.chunk");
const fs = require("fs");
const parse = require("csv-parse/lib/sync");
const PDFDocument = require("pdfkit");
const pdfDPI = 72;
const badgeHeight = 2.5;
const badgeWidth = 4.1;
const paperHeight = 11;
const paperWidth = 8.5;
const pdfHeight = paperHeight * pdfDPI;
const pdfWidth = paperWidth * pdfDPI;
const badgeRowsPerPage = 4;
const badgesPerRow = 2;
const barHeight = 30;
const extraPageHorizontalMargin =
((paperWidth - badgeWidth * badgesPerRow) * pdfDPI) / 2;
const extraPageVerticalMargin =
((paperHeight - badgeHeight * badgeRowsPerPage) * pdfDPI) / 2;
const doc = new PDFDocument({ autoFirstPage: false });
doc.pipe(fs.createWriteStream("badges.pdf"));
const attendees = parse(fs.readFileSync("data.csv", "utf8"), { columns: true });
const blankBadges = new Array(20).fill({
Team: "",
"First Name": "",
"Last Name": ""
});
const attendeesByPage = chunk(attendees.concat(blankBadges), badgeRowsPerPage);
attendeesByPage.forEach(attendees => {
renderPage(attendees);
});
doc.end();
function fontSizeForString(string) {
if (string.length > 12) {
return 21;
}
if (string.length > 9) {
return 23;
}
return 28;
}
function renderBadge(x, y, attendee) {
doc.image("givecamp-logo.png", x + badgeWidth * pdfDPI - 90, y + 10, {
width: 80
});
if (!attendee) {
return;
}
doc
.fillColor("black")
.font("Helvetica-Bold")
.fontSize(fontSizeForString(attendee["First Name"]))
.text(attendee["First Name"].toUpperCase(), x + 10, y + 55, {
align: "left",
ellipsis: true,
height: 10,
indent: 0,
width: badgeWidth * pdfDPI - 20
})
.font("Helvetica-Bold")
.fontSize(fontSizeForString(attendee["Last Name"]))
.text(attendee["Last Name"].toUpperCase(), {
align: "left",
ellipsis: true,
height: 10,
indent: 0,
width: badgeWidth * pdfDPI - 20
})
.font("Helvetica")
.fontSize(20)
.fillColor("white")
.text(attendee.Team, x, y + 157, {
align: "center",
ellipsis: true,
height: 10,
indent: 0,
width: badgeWidth * pdfDPI - 10
});
}
function renderPage(attendees) {
doc.addPage();
for (let i = 0; i < badgeRowsPerPage; i++) {
const badgeY = extraPageVerticalMargin + i * badgeHeight * pdfDPI;
doc
.rect(
extraPageHorizontalMargin,
badgeY + badgeHeight * pdfDPI - barHeight,
pdfWidth - extraPageHorizontalMargin * 2,
barHeight
)
.fill("#00689d");
if (attendees[i]) {
renderBadge(extraPageHorizontalMargin, badgeY, attendees[i]);
renderBadge(
extraPageHorizontalMargin + badgeWidth * pdfDPI,
badgeY,
attendees[i]
);
}
}
}