-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
312 lines (273 loc) · 9.6 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
const PDFDocument = require("pdfkit")
const preview = require("./calendar")
const fs = require("fs")
let dayjs = require("dayjs")
let weekOfYear = require("dayjs/plugin/weekOfYear")
dayjs.extend(weekOfYear)
let duration = require("dayjs/plugin/duration");
dayjs.extend(duration)
let month = ["January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"]
const rightAlignmentDifferentFontText = ({
textBoxX,
textBoxY,
textBoxWidth,
startText,
middleText,
endText,
startTextFont,
middleTextFont,
endTextFont
}) => {
// get the endText width
doc.font(endTextFont)
const endTextWidth = doc.widthOfString(endText)
// take the given width of the whole text
// add the textBoxX
// and subtract the endTextWidth
const endTextX = textBoxWidth + textBoxX - endTextWidth
const endTextY = textBoxY
doc.text(endText, endTextX, endTextY)
doc.font(middleTextFont)
const middleTextWidth = doc.widthOfString(middleText)
// take the X of the endText computed above
// and compute the X of the replacementText
// based on the middleText width
const middleTextX = endTextX - middleTextWidth
// the Y is the same as the whole textbox
const middleTextY = textBoxY
// set the font
// draw the middleText
doc.text(middleText, middleTextX, middleTextY)
doc.font(startTextFont)
const startTextWidth = doc.widthOfString(startText)
// take the X of the middleText computed above
// and compute the X of the startText
// based on the startText width
const startTextX = middleTextX - startTextWidth
const startTextY = textBoxY
doc.text(startText, startTextX, startTextY)
}
/**
* This is for the new layout that handles a whole week on two pages.
* if i adjust the layout in any way, I need to make changes to drawTimeslot. they
* are connected by the sunday block.
* @param page
*/
function drawLayout2(page) {
page.lineWidth(1)
page.lineCap("drawHeader")
.moveTo(47, 150)
.lineTo(548, 150)
.stroke()
page.lineWidth(8)
page.lineCap("vert")
.moveTo(196, 150)
.lineTo(196, 700)
.stroke()
page.lineWidth(8)
page.lineCap("vert2")
.moveTo(392, 150)
.lineTo(392, 700)
.stroke()
page.lineWidth(1)
}
function drawLayout(page) {
page.lineCap("drawHeader")
.moveTo(47, 150)
.lineTo(548, 150)
.stroke()
page.lineCap("vert")
.moveTo(297.5, 150)
.lineTo(297.5, 700)
.stroke()
page.lineCap("drawHeader")
.moveTo(47, 700)
.lineTo(548, 700)
.stroke()
}
//Prints date, just needs to be edited to print the right things.
//function drawHeader(doc, date, sideOfPage){
// let monthName = month[date.month()];
// let monthDay = date.date();
// if(sideOfPage === "left") {
// doc.text(`From ${monthName} ${monthDay}`);
// }
// else{
// doc.text(`To ${monthName} ${monthDay}`, 450, 50);
// }
//}
function drawHeader(doc, date, sideOfPage) {
let monthName = month[date.month()]
let monthDay = date.date()
doc.fontSize(12)
if (sideOfPage === "left") {
doc.font("Helvetica")
.fontSize(12)
.text("from ", 45, 30, {continued: false, textBaseline: "alphabetic", align: "left"})
.font("Helvetica-Bold")
.fontSize(52)
.text(`${monthName} ${monthDay}`, {textBaseline: "alphabetic"})
} else {
let heading = `${monthName} ${monthDay}`
doc.font("Helvetica")
.fontSize(12)
.text("to ", 0, 30, {continued: false, textBaseline: "alphabetic", align: "right"})
.font("Helvetica-Bold")
.fontSize(52)
.text(`${monthName} ${monthDay}`, {continued: true, textBaseline: "alphabetic", align: "right"})
}
doc.fontSize(12)
}
/**
* Wish i could combine multiple numbers to a case so that there are less cases.This is where the
* coordinates are assigned for the days of the week... Your wish has been granted!
* @param date
*/
function dayPlaceHolders(doc, date) {
const days = [
{dow: "Sunday", x: 350, y: 465},
{dow: "Monday", x: 0, y: 0 },
{dow: "Tuesday", x: 155, y: 0 },
{dow: "Wednesday", x: 350, y:0 },
{dow: "Thursday", x: 0, y: 0 },
{dow: "Friday", x: 155, y: 0},
{dow: "Saturday", x: 350, y: 0 }
];
let info = days[date.day()];
let xOffset = 50;
let yOffset = 100;
doc.fontSize(64)
.text(date.date(), xOffset + info.x, yOffset + info.y)
.fontSize(28)
.text(info.dow, xOffset + info.x, yOffset + 60 + info.y)
}
function drawPreview(page) {
//page.text("this is the drawPreview",450);
}
//I need to add time increments to each line
//does not work well with createpdfkit1
function drawTimeSlot(page, sideOfPage) {
let jump = (700 - 150) / 12
if (sideOfPage === "left") {
for (let i = 0; i < 12; i++) {
//page.lineWidth(10);
page.lineCap("timeslot")
.moveTo(47, 150 + jump)
.lineTo(548, 150 + jump)
.stroke()
jump += 45.83
}
} else {
for (let i = 0; i < 12; i++) {
if (i > 8) {
page.lineCap("sunday")
.moveTo(47, 150 + jump)
.lineTo(392, 150 + jump)
.stroke()
} else {
page.lineCap("timeslot")
.moveTo(47, 150 + jump)
.lineTo(548, 150 + jump)
.stroke()
}
jump += 45.83
}
}
}
//This takes in a number that it prints to the pdf page.
function drawPageNumber(page, number, totalPages, sideOfPage) {
doc.fontSize(12)
if (sideOfPage === "left") {
page.text(`page ${number} of ${totalPages + 1}`, 50, 720, {align: "left"})
} else {
page.text(`page ${number} of ${totalPages + 1}`, 15, 720)
}
}
function drawNotes(page) {
page.text("This is the drawNotes Section", 320, 170)
}
const defaultOptions = {
margins: {top: 50, left: 50, right: 50, bottom: 50},
font: "Helvetica"
}
const doc = new PDFDocument({autoFirstPage: false})
//Eventually pages will be a not needed parameter, start and end should be able to give the amount of pdfs that are needed
//will also need to make a function that edits the default options or at least overwrites them
function createPdf(start, end) {
let startDate = dayjs(start)
let endDate = dayjs(end)
let pageNumber = 0
for (let currentDate = startDate; endDate.diff(currentDate, 'day') >= 0; currentDate = currentDate.add(1, "day")) {
doc.addPage(defaultOptions)
doc.lineWidth(0)
doc.fontSize(15)
drawLayout(doc)
drawHeader(doc, currentDate)//good
drawPreview(doc)
drawTimeSlot(doc)//todo: put time to each line
pageNumber += 1
drawPageNumber(doc, pageNumber, endDate.diff(startDate, "day") + 1)
drawNotes(doc)
preview(doc, currentDate)
}
}
function createPdf2(start, end) {
let startDate = dayjs(start).startOf("week")
//we want start date to begin on a monday.
startDate = startDate.add(1, "day")
let endDate = dayjs(end).endOf("week")
//let totalWeeks = (endDate.week() - startDate.week()) + 1
let totalWeeks = Math.round(dayjs.duration(endDate.diff(startDate)).asWeeks())
let totalPages = totalWeeks * 2;
doc.addPage(defaultOptions)
let title = '2021'
let x = doc.x, y = doc.y, h = 700, w = 530;
let options = {width:w, align:'center'};
doc.rect(x, y, w, h).stroke()
.font('Helvetica-Bold')
.fontSize(128)
.text(title, x, y + 0.5 * (h - doc.heightOfString(title, options)), options);
let pageNumber = 2
//totalPages;
while (endDate.diff(startDate, "day") >= 0) {
for (let weeks = totalWeeks; weeks > 0; weeks--) {
doc.addPage(defaultOptions)
doc.lineWidth(0)
drawLayout2(doc)
drawTimeSlot(doc, "left")
drawHeader(doc, startDate, "left")//I need to implement this in a better way.
//This is where i loop through and make the left side of the page.
for (let weekDay = 0; weekDay < 3; weekDay++) {
dayPlaceHolders(doc, startDate)
startDate = startDate.add(1, "day")
}
drawPageNumber(doc, pageNumber, totalPages, "left")
pageNumber += 1
doc.addPage(defaultOptions)
doc.lineWidth(0)
drawLayout2(doc)
drawTimeSlot(doc)
//This is where the right side of the page is made.
for (let weekDay = 3; weekDay < 6; weekDay++) {
dayPlaceHolders(doc, startDate)
startDate = startDate.add(1, "day")
}
//Added this extra day place holder to account for sunday. I wanna find a workaround for this.
dayPlaceHolders(doc, startDate)
drawHeader(doc, startDate)
startDate = startDate.add(1, "day")
drawPageNumber(doc, pageNumber, totalPages)
pageNumber += 1
}
}
}
const express = require('express')
const app = express()
const port = 8080
app.get('/', (req, res) => {
res.send('Watch this space...')
})
app.listen(port, () => {
console.log(`pdf-calendar listening on port ${port}`)
})