Skip to content

Instantly share code, notes, and snippets.

@giladno
Created January 26, 2018 15:18
Show Gist options
  • Save giladno/952b1fe847de8f66fdc1a90b8536bb3e to your computer and use it in GitHub Desktop.
Save giladno/952b1fe847de8f66fdc1a90b8536bb3e to your computer and use it in GitHub Desktop.
app.get('/pdf', authenticated, async (req, res)=>{
let browser = await puppeteer.launch();
try {
let page = await browser.newPage();
await page.goto(url.format({
protocol: 'http:',
host: '127.0.0.1:3000',
pathname: '/',
query: req.query,
}), {waitUntil: 'networkidle'});
await page.evaluate(()=>new Promise(resolve=>{
if (window.tn_onload)
return resolve();
window.tn_onload = resolve;
setTimeout(resolve, 30000);
}));
let pdf = await page.pdf({format: 'A4'});
res.writeHead(200, {
'Content-Type': 'application/pdf',
'Content-Disposition': 'attachment; filename=claim.pdf',
'Content-Length': pdf.length,
});
res.end(pdf);
} finally { browser.close(); }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment