forked from devmridul/pdf-plus-railway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
130 lines (114 loc) · 4.49 KB
/
server.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
const fs = require('fs');
const path = require('path');
const utils = require('util');
const puppeteer = require('puppeteer');
const hb = require('handlebars');
const readFile = utils.promisify(fs.readFile);
const express = require('express');
const app = express();
const PORT = process.env.PORT || 8080;
app.get('/',function(req,res){
res.send("invoice-pdf-generator by Alledo Technologies Ai");
});
async function getTemplateHtml() {
console.log("Loading template file in memory");
try {
const invoicePath = path.resolve("./invoice.html");
return await readFile(invoicePath, 'utf8');
} catch (err) {
return Promise.reject("Could not load html template");
}
}
app.get('/invoice', async function (req, res, next) {
const { name, phone, date, paymentid, paymentmode, paymenttype, servicename, amount, receiptno, download, filename } = req.query;
let checker = 0;
let data = {};
getTemplateHtml().then(async (res) => {
// Now we have the html code of our template in res object
// you can check by logging it on console
console.log("Compiing the template with handlebars")
const template = hb.compile(res, { strict: true });
// we have compile our code with handlebars
const result = template(data);
// We can use this to add dyamic data to our handlebas template at run time from database or API as per need. you can read the official doc to learn more https://handlebarsjs.com/
let html = result;
html = html.split("Name of Customer").join(name);
html = html.split("Phone of Customer").join(phone);
html = html.split("Dated On").join(date);
html = html.split("paymentid").join(paymentid);
html = html.split("Payment Mode").join(paymentmode);
html = html.split("visa").join(paymenttype);
html = html.split("Service Name").join(servicename);
html = html.split("Service Amount").join(amount);
html = html.split("Total Amount").join(amount);
html = html.split("receiptno").join(receiptno);
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
ignoreDefaultArgs: ['--disable-extensions']
});
const page = await browser.newPage();
// We set the page content as the generated html by handlebars
await page.setContent(html);
// We use pdf function to generate the pdf in the same folder as this file.
await page.pdf({ path: 'invoice.pdf', format: 'A4' });
await browser.close();
console.log("PDF Generated");
checker = 1;
}).catch(err => {
console.error(err);
});
const timer = setInterval(() => {
if (checker === 1) {
clearInterval(timer);
let img = __dirname + "/invoice.pdf";
fs.access(img, fs.constants.F_OK, err => {
//check that we can access the file
console.log(`${img} ${err ? "does not exist" : "exists"}`)
});
fs.readFile(img, function (err, content) {
if (err) {
res.writeHead(404, {
"Content-type": "text/html"
});
res.end("<h1>No Invoice Found</h1>");
} else {
res.status(200);
res.contentType("application/pdf");
if (download == "true") {
res.setHeader("Content-Disposition", "attachment;filename=" + filename);
}
res.end(content);
}
});
}
}, 1);
});
app.get("/invoice/print", (req, res) => {
const { host } = req.query;
return res.status(200).send(`
<body>
<iframe></iframe>
</body>
<style>
body {
margin: 0px;
padding: 0px;
}
iframe {
width: 100%;
height: 100vh;
border: 0px;
}
</style>
<script>
document.querySelector("iframe").src = "https://` + host + req.originalUrl.split("/print").join("") + `";
document.querySelector("iframe").addEventListener("load", function () {
document.querySelector("iframe").contentWindow.print();
});
</script>
`);
});
app.listen(PORT, () => {
console.log("pdf-plus-railway running on PORT" + PORT);
});