-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvoiceGen.js
133 lines (110 loc) · 3.13 KB
/
invoiceGen.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
import jsPDF from "jspdf";
import "jspdf-autotable";
import QRCode from "qrcode";
function generatePdf(type, data) {
return new Promise(async (resolve, reject) => {
try {
let doc;
if (type === "shipped") {
doc = await createShipmentPdf(data);
} else if (type === "delivered") {
doc = await createDeliveredPdf(data);
} else reject("Invalid type");
const pdfBlob = doc.output("blob");
const file = new File([pdfBlob], `${data.orderId}_${type}.pdf`, {
type: pdfBlob.type,
});
resolve(file);
} catch (error) {
console.log("generatePdf :: error", error);
reject(error);
}
});
}
async function createShipmentPdf(data) {
const doc = new jsPDF({ unit: "pt", format: "a5" });
doc.setFont('helvetica')
const options = {
errorCorrectionLevel: "H",
type: "image/png",
quality: 1,
margin: 1,
width: 300,
};
const qrCodeDataUrl = await QRCode.toDataURL(data.orderId, options);
doc.addImage(qrCodeDataUrl, "PNG", 20, 20, 150, 150);
doc.setFontSize(12);
doc.text(`Customer Name: ${data.customerName}`, 20, 190);
doc.text(`Address: ${data.address}`, 20, 210, { maxWidth: 500 });
doc.text(`Contact: ${data.contact}`, 20, 250);
doc.setFontSize(14);
doc.text("Product Details", 20, 280);
const products = data.products.map((product) => [
{ content: product.name, styles: { halign: "left" } },
product.quantity,
`RS. ${Number(product.price).toLocaleString("en-IN")}`,
]);
const total = data.products.reduce(
(acc, product) => acc + product.price * product.quantity,
0
);
doc.autoTable({
startY: 300,
head: [["Product", "Quantity", "Price"]],
body: [
...products,
[
"",
"",
{
content: `Total: RS. ${total.toLocaleString("en-IN")}`,
styles: { fontStyle: "bold" },
},
],
],
theme: "grid",
styles: { fontSize: 10 },
});
return doc;
}
async function createDeliveredPdf(data) {
const doc = new jsPDF({ unit: "pt", format: "a4" });
doc.setFont('helvetica')
doc.setFontSize(18);
doc.text(`Invoice for Order #${data.orderId}`, 20, 40);
doc.text(`Date: ${data.date}`, 20, 60);
doc.setFontSize(12);
doc.text(`Customer Name: ${data.customerName}`, 20, 100);
doc.text(`Address: ${data.address}`, 20, 120, { maxWidth: 500 });
doc.text(`Contact: ${data.contact}`, 20, 160);
doc.setFontSize(14);
doc.text("Product Details", 20, 190);
const products = data.products.map((product) => [
{ content: product.name, styles: { halign: "left" } },
product.quantity,
`RS. ${Number(product.price).toLocaleString("en-IN")}`,
]);
const total = data.products.reduce(
(acc, product) => acc + product.price * product.quantity,
0
);
doc.autoTable({
startY: 210,
head: [["Product", "Quantity", "Price"]],
body: [
...products,
[
"",
"",
{
content: `Total: RS. ${total.toLocaleString("en-IN")}`,
styles: { fontStyle: "bold" },
},
],
],
theme: "grid",
styles: { fontSize: 10 },
});
return doc;
}
export { generatePdf };