-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
35 lines (28 loc) · 898 Bytes
/
index.ts
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
import express from 'express';
import bodyParser from "body-parser";
import { connect } from 'puppeteer';
interface Payload {
fromUrl?: string;
fromRawHTML?: string;
}
const app: express.Application = express();
app.use(bodyParser.json());
app.post('/api/pdf', async (req, res) => {
const browser = await connect({browserWSEndpoint: 'ws://localhost:3000'});
const page = await browser.newPage();
const payload: Payload = req.body;
if (payload.fromUrl) {
await page.goto(payload.fromUrl);
} else if(payload.fromRawHTML) {
await page.setContent(payload.fromRawHTML);
} else {
throw new Error('Not implemented!');
}
const pdf = await page.pdf({path: 'page.pdf'});
res.contentType('application/pdf');
res.set({'Content-length': pdf.length});
res.send(pdf);
});
app.listen(8000, () => {
console.log('Server rodando');
});