1

HI Ive been trying to get puppeteer to take a screenshot of full pages, including all images. Unfortunately background images are getting omitted (see comparison below)... I can't figure out how to get them.

Here's my code


async function screeshotFullPage(url: string): Promise<string> {
  const browser = await puppeteer.launch({ headless: true });
  const page = await browser.newPage();
  await page.goto(url, { waitUntil: "networkidle0" });

  await page.evaluate(async () => {
    const selectors = Array.from(document.querySelectorAll("img"));

//https://stackoverflow.com/questions/46160929/puppeteer-wait-for-all-images-to-load-then-take-screenshot

    await document.body.scrollIntoView(false);
    await Promise.all(
      selectors.map((img) => {
        if (img.complete) return;
        return new Promise((resolve, reject) => {
          img.addEventListener("load", resolve);
          img.addEventListener("error", reject);
        });
      })
    );
  });

  await sleep(5000); // resolves in 5 sec

  const path = generateScreenshotPath();

  await page.screenshot({
    path,
    fullPage: true,
  });

  return await browser.close();
}

await screeshotFullPage("https://chesskid.com")

no background images

fotoflo
  • 821
  • 1
  • 9
  • 21

1 Answers1

0

Perhaps not a direct answer, but I had troubles with CSS backgrounds in PDFs generated by Puppeteer if they were direct links to S3 files. Switching links to CloudFront (Amazon's CDN) fixed the issue for me.

Paweł Gościcki
  • 9,066
  • 5
  • 70
  • 81