-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
46 lines (36 loc) · 1.07 KB
/
app.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
const fs = require('fs');
const express = require('express');
const puppeteer = require('puppeteer');
const tmp = require('tmp');
const app = express();
const port = process.env.PORT || 3000;
app.use(express.text({ type: '*/*' }));
app.post('/create_ledger', (req, res) => {
// Content-Type validation
if (!req.is('text/html')) {
res.status(400);
return;
}
const html = req.body;
const tmpObj = tmp.fileSync({ postfix: '.html' });
fs.writeFileSync(tmpObj.fd, html, () => {
res.status(500);
});
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(`file://${tmpObj.name}`, {
waitUntil: 'networkidle2',
});
await page.emulateMediaType('screen');
// TODO: クエリで切り替えられるようにする
const buf = await page.pdf({ format: 'a4' });
res.set('Content-Type', 'application/pdf');
res.send(buf);
await browser.close();
tmpObj.removeCallback();
})();
});
app.listen(port, () => {
console.log(`App running at http://localhost:${port}`);
});