Last active
January 10, 2022 03:49
-
-
Save rcarmo/cf698b52832d0ec356c147cf9c9ad898 to your computer and use it in GitHub Desktop.
Trying to get a single-page PDF screenshot out of puppeteer
This file contains hidden or 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
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