Skip to content

Instantly share code, notes, and snippets.

@WingEraser
Last active September 26, 2020 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WingEraser/93433a2a9ddbc167106597997686cd8c to your computer and use it in GitHub Desktop.
Save WingEraser/93433a2a9ddbc167106597997686cd8c to your computer and use it in GitHub Desktop.
const fs = require('fs-extra')
const express = require('express')
const nunjucks = require('nunjucks')
const puppeteer = require('puppeteer')
const { v4: uuidv4 } = require('uuid')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
nunjucks.configure('views', {
autoescape: true,
express: app,
noCache: false
})
let messages = {};
app.listen(3000, () => {
console.log('server running on port 3000')
})
app.get('/url', (req, res, next) => {
res.json(["Tony", "Lisa", "Michael", "Ginger", "Food"])
})
app.post('/messages', (req, res) => {
const id = uuidv4()
const message = {
id,
text: req.body.text
}
messages[id] = message
return res.send(message)
})
app.get('/messages', (req, res) => {
res.send(messages)
})
app.post('/invoice', (req, res) => {
(async () => {
const html = nunjucks.render('index.html', { foo: 'Ka Wing is awesome' })
await fs.writeFile('generate.html', html)
createPdf('')
res.send(html)
})()
})
function createPdf(html) {
(async () => {
console.log('createPDF')
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Emitted when the DOM is parsed and ready (without waiting for resources)
page.once('domcontentloaded', () => console.info('✅ DOM is ready'));
// Emitted when the page is fully loaded
page.once('load', () => console.info('✅ Page is loaded'));
// Emitted when the page attaches a frame
page.on('frameattached', () => console.info('✅ Frame is attached'));
// Emitted when a frame within the page is navigated to a new URL
page.on('framenavigated', () => console.info('👉 Frame is navigated'));
// Emitted when a script within the page uses `console.timeStamp`
page.on('metrics', data => console.info(`👉 Timestamp added at ${data.metrics.Timestamp}`));
// Emitted when a script within the page uses `console`
page.on('console', message => console[message.type()](`👉 ${message.text()}`));
// Emitted when the page emits an error event (for example, the page crashes)
page.on('error', error => console.error(`❌ ${error}`));
// Emitted when a script within the page has uncaught exception
page.on('pageerror', error => console.error(`❌ ${error}`));
// Emitted when a script within the page uses `alert`, `prompt`, `confirm` or `beforeunload`
page.on('dialog', async dialog => {
console.info(`👉 ${dialog.message()}`);
await dialog.dismiss();
});
// Emitted when a new page, that belongs to the browser context, is opened
page.on('popup', () => console.info('👉 New page is opened'));
// Emitted when the page produces a request
page.on('request', request => console.info(`👉 Request: ${request.url()}`));
// Emitted when a request, which is produced by the page, fails
page.on('requestfailed', request => console.info(`❌ Failed request: ${request.url()}`));
// Emitted when a request, which is produced by the page, finishes successfully
page.on('requestfinished', request => console.info(`👉 Finished request: ${request.url()}`));
// Emitted when a response is received
page.on('response', response => console.info(`👉 Response: ${response.url()}`));
// Emitted when the page creates a dedicated WebWorker
page.on('workercreated', worker => console.info(`👉 Worker: ${worker.url()}`));
// Emitted when the page destroys a dedicated WebWorker
page.on('workerdestroyed', worker => console.info(`👉 Destroyed worker: ${worker.url()}`));
// Emitted when the page detaches a frame
page.on('framedetached', () => console.info('✅ Frame is detached'));
// Emitted after the page is closed
page.once('close', () => console.info('✅ Page is closed'));
console.log('yoooooo')
await page.goto('https://example.com', { waitUntil: 'networkidle2' });
await page.pdf({ path: 'invoice.pdf', format: 'A4' });
console.log('done')
await browser.close();
})();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment