User Agents

Web browsers and everyday web tech, explained

Screenshot GST Invoice Webpages with PHP and Headless Chrome

· 5 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

Automating the capture of GST invoice webpages allows businesses to archive tax documents as images or PDFs, ensuring that a visual record exists even if the source portal changes or the invoice is modified. To achieve this in PHP, the most reliable method is using the Chrome DevTools Protocol (CDP) via a library like chrome-php/chrome, which controls a headless instance of the Google Chrome browser to render the page and trigger a screenshot command.

What You'll Need

  • PHP 8.1 or higher: Ensure your environment is up to date for compatibility with modern libraries.
  • Composer: The PHP dependency manager is required to install the Chrome library.
  • Google Chrome or Chromium: The actual browser binary must be installed on the server.
  • Server Permissions: The PHP process must have permission to execute shell commands and write files to the local directory.
  • Headless Chrome binary path: You need to know where chrome or chromium-browser is located (e.g., /usr/bin/google-chrome).

Step-by-Step

  1. Install the Chrome PHP Library: Open your terminal in your project root and run composer require chrome-php/chrome. This library provides a high-level wrapper around the Chrome DevTools Protocol, removing the need to write raw JSON-RPC requests.
  2. Configure the Chrome Instance: Create a PHP script and initialize the browser. You must specify the path to the Chrome binary if it is not in your system's default PATH. Use the BrowserFactory class to create a new browser instance.
  3. Handle Authentication: Since GST invoices are usually behind a login portal, you cannot simply navigate to the invoice URL. You must first navigate to the login page, use Page\Page::evaluate to inject JavaScript that fills in the username and password fields, and then trigger the submit button.
  4. Navigate to the Invoice Page: Once authenticated, use the Page\Page::navigate method to go to the specific GST invoice URL. Wait for the page to load completely.
  5. Wait for Dynamic Content: GST portals often use JavaScript frameworks (like Angular or React) to load invoice data asynchronously. Use a loop or a specific wait command to ensure the invoice table and totals are visible before taking the shot.
  6. Set the Viewport Size: To ensure the invoice isn't cut off or scaled poorly, set a specific window size using Page\Page::setDeviceMetricsScaleFactor or similar viewport settings. A width of 1280px and a height that covers the full document is recommended.
  7. Capture the Screenshot: Use the Page\Page::screenshot method. You can choose between a visible viewport screenshot or a "full page" screenshot, which captures the entire length of the invoice regardless of the screen height.
  8. Save the Output: The screenshot method returns the image data as a string. Use file_put_contents('invoice_capture.png', $screenshotData) to save the file to your server's storage.
  9. Close the Browser: Always call $browser->close() to terminate the headless Chrome process. Failing to do so will lead to "zombie" processes that consume server RAM.

Common Mistakes

  • Ignoring Lazy Loading: Many modern invoice pages only load images or data as you scroll. If your screenshot is missing the bottom half of the invoice, you may need to script a "scroll to bottom" action before capturing.
  • Hardcoding Credentials: Placing GST portal passwords directly in the PHP script is a security risk. Use .env files or a secure vault.
  • Forgetting the --no-sandbox Flag: When running Chrome on Linux servers (especially as root), Chrome may fail to start. You must pass the --no-sandbox and --disable-setuid-sandbox arguments during initialization.
  • Overlooking Timeouts: GST government portals can be slow. Setting a short timeout will result in screenshots of "Loading..." screens rather than actual invoices.

Tips

  • Use PDF instead of PNG: If the goal is archiving, use the Page\Page::pdf method. PDFs are searchable and better for tax audits than image files.
  • CSS Injection: Use Page\Page::addStyleTag to hide unnecessary UI elements like navigation bars, footers, or sidebars. This creates a clean "print-style" screenshot of just the invoice.
  • Session Persistence: If you are capturing multiple invoices, don't log in for every single one. Keep the browser instance open and navigate through the list of invoices to save time and avoid being flagged for too many login attempts.
  • Headless Mode: Ensure you are running in --headless mode in production to save system resources, as rendering a GUI is unnecessary.

Troubleshooting

If the browser fails to launch, check the logs for "Chrome failed to start". This is usually due to missing system dependencies on Linux. Install the required libraries using apt-get install libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdrm2 libxkbcommon0 libxcomposite1 libxdamage1 libxrandr2 libgbm1.

If the screenshot is blank, it is likely because the page content is rendered via JavaScript after the initial load. Increase your wait time or use a custom function to check for the existence of a specific HTML element (like .invoice-container) before triggering the screenshot.

If you encounter "Access Denied" or CAPTCHA screens, headless Chrome may be detected as a bot. Try setting a realistic User-Agent string to mimic a standard desktop browser.

Frequently Asked Questions

Can I capture invoices from a site with a CAPTCHA?

Headless Chrome cannot solve CAPTCHAs automatically. You would need to either use a third-party CAPTCHA solving service API or manually authenticate and pass the session cookies to the headless browser.

Does this work on shared hosting?

Generally, no. Most shared hosting providers do not allow you to install the Chrome binary or execute the shell commands required to run a headless browser. A VPS or dedicated server is required.

How do I capture only a specific part of the invoice?

You can use the screenshot method with a clip parameter. This requires providing the X and Y coordinates and the width and height of the specific element you wish to capture.

Will this slow down my server?

Yes, running a browser instance is resource-intensive. To mitigate this, use a queue system (like Redis or RabbitMQ) to process screenshots in the background rather than during a synchronous HTTP request.

Is it legal to screenshot GST invoices?

Capturing your own business invoices for archival purposes is standard practice. However, always ensure you are complying with the terms of service of the portal and local data privacy laws.