Skip to content

Commit 9282ba0

Browse files
committed
add endpoint from-html
1 parent fe8aef8 commit 9282ba0

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"start": "node pdf-service.js"
44
},
55
"dependencies": {
6+
"body-parser": "^1.19.0",
67
"express": "^4.17.1",
78
"puppeteer": "^5.2.1"
89
},

pdf-service.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ const path = require('path');
44
const fs = require('fs');
55
const crypto = require('crypto');
66
const app = express();
7+
const bodyParser = require('body-parser');
8+
9+
app.use(bodyParser.json()); // support json encoded bodies
10+
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
711

812
let port = 3000;
913
let calls = {
@@ -20,7 +24,25 @@ app.get('/health', (req, res) => {
2024
deliverJson(res, {isReady, calls}, isReady ? 200 : 503);
2125
});
2226

23-
app.get('/from-html', async (req, res) => {
27+
app.post('/from-html', async (req, res) => {
28+
const html = req.body.html;
29+
const htmlFile = md5(html.substr(0, 100)) + '.html';
30+
const fullHtmlPath = path.join(__dirname, storagePath, htmlFile);
31+
32+
fs.writeFileSync(fullHtmlPath, html);
33+
34+
let pdfFilePath;
35+
try {
36+
pdfFilePath = await generatePdf(`file://${fullHtmlPath}`);
37+
} catch (err) {
38+
console.log('/from-html: error generating PDF', e);
39+
let msg = 'failure generating PDF';
40+
deliverJson(res, {msg, err}, 500);
41+
return;
42+
}
43+
44+
deliverPdfFile(res, pdfFilePath);
45+
fs.unlinkSync(fullHtmlPath);
2446
});
2547

2648
app.get('/from-url', async (req, res) => {
@@ -75,7 +97,7 @@ async function generatePdf(url) {
7597
waitUntil: 'networkidle2',
7698
});
7799

78-
let filename = crypto.createHash('md5').update(url).digest('hex') + '.pdf';
100+
let filename = md5(url) + '.pdf';
79101
const pdfFolder = path.join(__dirname, storagePath);
80102
if (!fs.existsSync(pdfFolder)) {
81103
fs.mkdirSync(pdfFolder);
@@ -95,6 +117,10 @@ async function generatePdf(url) {
95117
return pdfFilePath;
96118
}
97119

120+
function md5(seed) {
121+
return crypto.createHash('md5').update(seed).digest('hex');
122+
}
123+
98124
app.listen(port, () => {
99125
console.log(`Kool PDF service running at port ${port}`);
100126
console.log(`Going to start puppeter`);

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ bl@^4.0.1:
5151
inherits "^2.0.4"
5252
readable-stream "^3.4.0"
5353

54-
body-parser@1.19.0:
54+
body-parser@1.19.0, body-parser@^1.19.0:
5555
version "1.19.0"
5656
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a"
5757
integrity sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==

0 commit comments

Comments
 (0)