-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdfGenerator.js
169 lines (147 loc) · 4.03 KB
/
pdfGenerator.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
const PDFDocument = require('pdfkit');
const fs = require('fs');
const generateInvoice = async (order, path, res) => {
let pdfDoc = new PDFDocument({ size: "A4", margin: 50 });
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-disposition', 'attachment; filename=Invoice-' + order._id + '.pdf');
var writeStream = pdfDoc.pipe(fs.createWriteStream(path));
await pdfDoc.pipe(res);
writeStream.on('finish', () => {
fs.unlink(path, err => {
if(err) throw err;
});
});
headline(pdfDoc);
generateCustomerInformation(pdfDoc, order);
generateInvoiceTable(pdfDoc, order);
generateFooter(pdfDoc);
pdfDoc.end();
};
function headline(pdfDoc) {
pdfDoc
.image("icon.png", 40, 50, {width : 40})
.fontSize(15)
.text('Downtown Books', 85, 60)
.moveDown()
.fontSize(10)
.text('Where mind meets Knowledge', 50);
}
function generateCustomerInformation(doc, invoice) {
doc
.fillColor("#444444")
.fontSize(20)
.text("Invoice", 50, 160);
generateHr(doc, 185);
const customerInformationTop = 200;
doc
.fontSize(10)
.text("Invoice Number:", 50, customerInformationTop)
.font("Helvetica-Bold")
.text(invoice._id.toString(), 150, customerInformationTop)
.font("Helvetica")
.text("Invoice Date:", 50, customerInformationTop + 15)
.text((new Date()).toLocaleDateString('en-US'), 150, customerInformationTop + 15)
.text('Customer Name: ', 300, customerInformationTop)
.font("Helvetica-Bold")
.text(invoice.userInfo.username, 390, customerInformationTop)
.font("Helvetica")
.text(`Customer Email: ${invoice.userInfo.email}`, 300, customerInformationTop + 15)
.moveDown();
generateHr(doc, 252);
}
function generateTableRow(doc, y, c1, c2, c3, c4, c5) {
doc.fontSize(8)
.text(c1, 50, y)
.fontSize(10)
.text(c2, 170, y)
.text(c3, 280, y, { width: 90, align: 'right' })
.text(c4, 370, y, { width: 90, align: 'right' })
.text(c5, 0, y, { align: 'right' });
}
function generateInvoiceTable(doc, invoice) {
let i;
const invoiceTableTop = 330;
let totalPrice = 0;
doc.font("Helvetica-Bold");
generateTableRow(
doc,
invoiceTableTop,
"Item No.",
"Title",
"Unit Cost",
"Quantity",
"Line Total"
);
generateHr(doc, invoiceTableTop + 20);
doc.font("Helvetica");
for (i = 0; i < invoice.productInfo.length; i++) {
const item = invoice.productInfo[i];
const position = invoiceTableTop + (i + 1) * 30;
const amount = item.price*item.quantity;
generateTableRow(
doc,
position,
item.productId.toString(),
item.title,
formatCurrency(item.price),
item.quantity,
formatCurrency(amount)
);
totalPrice += amount;
generateHr(doc, position + 20);
}
const subtotalPosition = invoiceTableTop + (i + 1) * 30;
generateTableRow(
doc,
subtotalPosition,
"",
"",
"Subtotal",
"",
formatCurrency(totalPrice)
);
const paidToDatePosition = subtotalPosition + 20;
generateTableRow(
doc,
paidToDatePosition,
"",
"",
"Paid To Date",
"",
formatCurrency(totalPrice)
);
const duePosition = paidToDatePosition + 25;
doc.font("Helvetica-Bold");
generateTableRow(
doc,
duePosition,
"",
"",
"Balance Due",
"",
formatCurrency(0)
);
doc.font("Helvetica");
}
function generateFooter(doc) {
doc
.fontSize(10)
.text(
"Thank you for shopping with us. Wish to serve you again.",
50,
780,
{ align: "center", width: 500 }
);
}
function formatCurrency(cents) {
return "Rs. " + (cents).toFixed(2);
}
function generateHr(doc, y) {
doc
.strokeColor("#aaaaaa")
.lineWidth(1)
.moveTo(50, y)
.lineTo(550, y)
.stroke();
}
module.exports = generateInvoice;