User Agents

Web browsers and everyday web tech, explained

Browsershot Long Screenshots with Headless Chrome

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Capturing extremely long screenshots with Browsershot and Headless Chrome allows you to render an entire webpage from top to bottom into a single image file, regardless of the page's actual height. This is achieved by leveraging the Puppeteer library via the Browsershot PHP wrapper, which instructs a headless Chrome instance to calculate the full scroll height of the DOM and capture the viewport accordingly. This process is essential for archiving long-form articles, auditing landing pages, or creating visual documentation of complex web applications.

What You'll Need

  • A PHP environment (version 7.4 or higher recommended).
  • Composer installed on your system.
  • Node.js and NPM installed (required to run Puppeteer).
  • Google Chrome or Chromium installed on the server or local machine.
  • The spatie/browsershot package installed via Composer.

Step-by-Step

  1. Install Browsershot: Run composer require spatie/browsershot in your project directory to add the package to your dependencies.
  2. Install Puppeteer: Since Browsershot is a wrapper for Puppeteer, you must install the Node library in your project root. Run npm install puppeteer. If you are on a Linux server, you may need to install additional system dependencies for Chrome to run in headless mode.
  3. Initialize the Browsershot Instance: In your PHP script, import the Browsershot class. Start by calling the static url() method with the target address you wish to capture.
  4. Configure Full Page Capture: To ensure the screenshot captures the entire length of the page rather than just the visible area, chain the fullPage() method to your instance. This tells Chrome to expand the viewport to match the total height of the HTML body.
  5. Handle Dynamic Content: Many modern websites use lazy-loading images or animations that only trigger upon scrolling. To capture these, use waitUntilNetworkIdle() or delay(). This ensures that all assets are fully loaded before the snapshot is taken.
  6. Set the Output Path: Use the save() method to specify the filename and directory where the image should be stored (e.g., ->save('screenshot.png')).
  7. Execute the Command: Run your PHP script. Browsershot will launch a headless Chrome process, navigate to the URL, expand the page, and save the resulting image to your disk.

Common Mistakes

  • Ignoring Node Path: On many servers, the node or npm binaries are not in the default system path. Failing to specify the path using setNodeBinary('/path/to/node') and setNpmBinary('/path/to/npm') often leads to "command not found" errors.
  • Overlooking Lazy Loading: Capturing a page immediately upon load often results in blank spaces where images should be. Forgetting to add a delay or wait for network idle means the screenshot is taken before the browser triggers the scroll-based loading of assets.
  • Memory Exhaustion: Extremely long pages (thousands of pixels high) can consume significant RAM. Attempting to capture an infinitely scrolling page without a defined limit can crash the PHP process or the Chrome instance.
  • Permission Issues: Not granting the web server write permissions to the destination folder will cause the save() method to fail silently or throw a permission denied error.

Tips

  • Use Custom Viewports: If the page layout changes based on width, use setViewport(width, height) to simulate a specific device resolution before calling fullPage().
  • Disable Sandbox: On certain Linux distributions or Docker containers, Chrome's sandbox prevents it from launching. Use addChromiumArguments(['--no-sandbox', '--disable-setuid-sandbox']) to resolve this.
  • Optimize Image Format: While PNG is default and high quality, very long screenshots result in massive file sizes. Consider post-processing the image or using a tool to compress the output if storage is a concern.
  • Hide Elements: If there are sticky headers or chat widgets that overlap content during the full-page scroll, you can use evaluate() to run a snippet of JavaScript that sets those elements to display: none before the capture.

Troubleshooting

If the screenshot is cut off or only shows the top fold, double-check that fullPage() is called before save(). If the image is completely blank, ensure that the website does not have a strict bot-detection system that blocks headless Chrome; in such cases, trying a custom User-Agent via setOption('userAgent', '...') may help.

If you encounter a "Timeout" error, the page may be too heavy or the server too slow. Increase the timeout limit using timeout(120) (where 120 is the number of seconds) to give Chrome more time to render the full DOM.

For errors regarding missing shared libraries (common on Ubuntu/Debian), install the necessary dependencies using apt-get install for libraries like libnss3, libatk1.0-0, and libcups2.

Frequently Asked Questions

Can I capture a page behind a login wall?

Yes, you can use setCookies() to pass session cookies to the headless browser, allowing it to access authenticated pages.

Does fullPage() work with sticky elements?

Yes, but sticky elements (like fixed navigation bars) may appear multiple times or overlap content in the final image because they stay fixed relative to the viewport as Chrome "scrolls" to capture the page.

Can I save the screenshot as a PDF instead?

Yes, Browsershot provides a pdf() method that works similarly to save(), allowing you to export the full page as a PDF document.

How do I handle pages with infinite scroll?

Browsershot cannot automatically "scroll forever." You must use evaluate() to run a JavaScript loop that scrolls to the bottom of the page several times before calling the capture method.

Will this work on shared hosting?

Generally, no. Most shared hosting providers do not allow the installation of Node.js or the execution of headless Chrome binaries due to resource and security restrictions.