(async () => {
const baseUrl = 'example.com';
const browser = await puppeteer.launch({ headless: true});
const context = await browser.createIncognitoBrowserContext();
const page = await context.newPage();
function delay(time) {
return new Promise(function (resolve) {
setTimeout(resolve, time)
});
}
await page.goto(baseUrl);
await page.setViewport({
width: 1200,
height: 800
});
await page.waitForSelector('[name="loginfmt"]');
await page.type('[name="loginfmt"]', 'abc@amazon.com');
await page.click('[type="submit"]');
delay(1000);
await page.authenticate({ username: `abc@amazon.com`, password: `abc@123` });
await delay(2000);
async function autoScroll(page){
await page.evaluate(async () => {
await new Promise((resolve, reject) => {
var totalHeight = 0;
var distance = 10000;
var timer = setInterval(() => {
var scrollHeight = document.body.scrollHeight;
window.scrollBy(0, distance);
totalHeight += distance;
if(totalHeight >= scrollHeight){
clearInterval(timer);
resolve();
}
}, 100);
});
})
}
await autoScroll(page);
await delay(2000);
await page.pdf(
{
path: "1.pdf",
printBackground: true,
}
)
browser.close();
})()
This was an approach which I got from GitHub and Stack Overflow regarding auto scrolling of the page. But I was not able to get the entire page in my pdf after generated. I would like to get a solution for both horizontal and vertical scrolling so that none of the content gets missed in my generated pdf.
Currently have been using the below code to get full pdf. This is not the dynamic approach to get the full page of the PDF as I have given height 15000px manually.
For the present page it will work fine but for the other pages with the height less than 15000px it would result in an empty space at the end which is not the desired way.
await page.pdf(
{
path: "1.pdf",
printBackground: true,
height: "15000px"
}
)
Looking if we can dynamically render the height and width of the entire page so that I can pass them in the page.pdf by storing them in the variables before the page.pdf function which would give the correct dimensions of the individual page correctly.
I have been trying to fix this since a long time. It would be really helpful if I get a fix for this.