-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
140 lines (117 loc) · 3.69 KB
/
Copy pathserver.js
File metadata and controls
140 lines (117 loc) · 3.69 KB
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
'use strict';//strict mode
const express = require("express");
const body = require("body-parser");
const app = express();
app.set('view engine', 'ejs');
app.use(express.static(__dirname + "/public"));
app.use(body.urlencoded({ extended: true }));
app.get("/", function(req, res){
res.render("home");
})
app.get("/submitForm", function(req,res){
res.render("submitForm");
})
app.post("/generatePDF", function(req, res){
/**
* Initialization for pdfkit library
*/
const PDFDocument = require('pdfkit');
const fs = require('fs');
console.log("generatePDF");
const items = req.body;
/**
* Create PDF document with appropriate functions and file name
*/
function createPdf(itemsJson, path) {
let doc = new PDFDocument({ margin: 50 });
generateHeader(doc);
generateTable(doc, itemsJson);
doc.end();
doc.pipe(fs.createWriteStream(path));
}
/**
* Generate a header for the pdf
*/
function generateHeader(doc) {
doc
.image("logo.png", 50, 45, { width: 140 })
.fillColor("#444444")
.fontSize(10)
.text("Name: " + items.name, 450, 65, {align: "left"})
.text("Phone: " + items.phone, 450, 80, { align: "left" })
.text("Email: " + items.email, 450, 95, { align: "left" })
.moveDown();
}
/**
* Generate each items as a row to the table into the pdf file
*/
function generateTableRow(doc, y, c1, c2, c3, c4, c5) {
if(c1 == "" || c1 == null)
{
doc
.fontSize(10)
.text(c2, 150, y)
.text(c3, 280, y, { width: 90, align: "right" })
.text(c4, 370, y, { width: 90, align: "right" })
.text(c5, 0, y, { align: "right" });
}
else{
doc
.fontSize(10)
.image(c1, 50, y, { width: 70 })
.text(c2, 150, y)
.text(c3, 280, y, { width: 90, align: "right" })
.text(c4, 370, y, { width: 90, align: "right" })
.text(c5, 0, y, { align: "right" });
}
}
/**
* Generate a table where for each loop the items in the json object.
* Create column name for items ex.("image", "item", "description", "quantity", "price")
*/
function generateTable(doc, items) {
let i,
itemsTableTop = 180;
doc.font("Helvetica-Bold");
doc
.fontSize(10)
.text("Image", 50, itemsTableTop)
.text("Item", 150, itemsTableTop)
.text("Description", 280, itemsTableTop, { width: 90, align: "right" })
.text("Quantity", 370, itemsTableTop, { width: 90, align: "right" })
.text("Price", 0, itemsTableTop, { align: "right" });
generateHr(doc, itemsTableTop + 20);
doc.font("Helvetica");
for (i = 0; i < items.items.length; i++) {
const item = items.items[i];
const position = itemsTableTop + (i + 1) * 70;
generateTableRow(
doc,
position,
item.image,
item.item,
item.description,
item.quantity,
item.amount
);
}
}
/**
* generate space
*/
function generateHr(doc, y) {
doc
.strokeColor("#aaaaaa")
.lineWidth(1)
.moveTo(50, y)
.lineTo(550, y)
.stroke();
}
createPdf(items, "SampleDocument.pdf");
})
// Take any port number of your choice which
// is not taken by any other process
app.listen(8080, function (error) {
if (error) throw error
console.log("Server created Successfully on PORT 8080")
})