Skip to content

Instantly share code, notes, and snippets.

@aryehof
Created August 21, 2017 14:30
Show Gist options
  • Save aryehof/5eafe6fe8d48d7810921d696b1002a39 to your computer and use it in GitHub Desktop.
Save aryehof/5eafe6fe8d48d7810921d696b1002a39 to your computer and use it in GitHub Desktop.
Get a single-page PDF screenshot out of puppeteer
// via https://news.ycombinator.com/item?id=15031256
// using https://github.com/GoogleChrome/puppeteer
const puppeteer = require('puppeteer');
function sleep(ms){
return new Promise(resolve=>{
setTimeout(resolve,ms)
})
}
(async() => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setViewport({width: 1280, height: 1024, deviceScaleFactor: 1});
await page.goto('https://theverge.com', {waitUntil: 'networkidle'});
var innerHeight = await page.evaluate(_ => {return window.innerHeight}),
height = await page.evaluate(_ => {return document.body.clientHeight});
console.log(height);
console.log("Scrolling");
for(i=0; i<(height/innerHeight); i++) {
page.evaluate(_ => {
window.scrollBy(0, window.innerHeight);
});
await sleep(200);
console.log(i);
}
console.log("Waiting for transfers");
await page.waitForNavigation({networkIdleTimeout: 15000, waitUntil: 'networkidle'});
console.log("Done.");
var height = await page.evaluate(() => {return document.body.clientHeight});
console.log(height);
await page.pdf({path: 'theverge.pdf', width: "1280px", height: height + "px", printBackground: true});
browser.close();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment