Created
August 21, 2017 14:30
-
-
Save aryehof/5eafe6fe8d48d7810921d696b1002a39 to your computer and use it in GitHub Desktop.
Get a single-page PDF screenshot out of puppeteer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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