-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (42 loc) · 1.34 KB
/
index.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
const puppeteer = require('puppeteer');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const port = process.env.PORT || 8080;
async function getPDF (url, name) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url, {waitUntil: 'networkidle2'});
await page.pdf({
path: `docs/${name}.pdf`,
format: 'A4'
});
await browser.close();
return;
}
async function getScreenshot (url, name, format) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url, {waitUntil: 'networkidle2'});
await page.screenshot({
path: `docs/${name}.${format}`,
fullPage: true,
type: format
});
await browser.close();
return;
}
app.get('/pdf', (req, res) => {
getPDF(req.query.url, req.query.name).then(() =>{
res.sendFile(__dirname + `/docs/${req.query.name}.pdf`);
});
});
app.get('/screenshot', (req, res) => {
getScreenshot(req.query.url, req.query.name, req.query.format).then(() =>{
res.sendFile(__dirname + `/docs/${req.query.name}.${req.query.format}`);
});
});
app.listen(port);
console.log('Magic happens on port ' + port);