ThisCalculator

Everyday

How to Capture GST Invoice Webpages as PDFs with Python Playwright

Updated · Standard formula, shown below

This guide shows you how to use Python Playwright to automatically capture GST invoice webpages as PDF files. You’ll learn to set up a script that opens a webpage, waits for it to fully load, and saves a high-quality PDF—perfect for archiving or sharing invoices. You’ll need a basic understanding of Python and a working Python environment.

What You’ll Need

  • Python 3.7 or newer installed on your system.
  • Pip (Python’s package installer).
  • Access to the GST invoice webpage you want to capture (it may require login).
  • A code editor or terminal to write and run the script.

Step-by-Step

  1. Install Playwright. Open your terminal or command prompt and run: pip install playwright. This installs the Playwright Python library.
  2. Install the browser. Playwright needs a browser engine. Run playwright install chromium to download the Chromium browser. You can also use Firefox or WebKit, but Chromium is recommended for best PDF support.
  3. Create a new Python file. Name it something like capture_invoice.py and open it in your editor.
  4. Import Playwright. At the top of your file, add: from playwright.sync_api import sync_playwright. This imports the synchronous API, which is easier for simple scripts.
  5. Set up the script structure. Write a function that uses sync_playwright() as a context manager. Inside, launch Chromium with p.chromium.launch().
  6. Create a new page. Use browser.new_page() to open a new tab. If the invoice requires login, you can use page.goto('https://your-gst-portal.com/login') first, fill in credentials, and then navigate to the invoice URL. For this guide, we’ll assume you have a direct URL.
  7. Navigate to the invoice page. Use page.goto('https://your-invoice-url.com/invoice/12345'). Replace the URL with the actual invoice page.
  8. Wait for the page to load completely. Invoices often load data dynamically. Use page.wait_for_load_state('networkidle') to wait until all network requests finish. You can also add a small delay like page.wait_for_timeout(2000) to ensure images and fonts render.
  9. Generate the PDF. Use page.pdf(path='invoice.pdf', format='A4', print_background=True). The print_background option ensures background colors and images are included. You can also specify margins with margin={'top': '10mm', 'bottom': '10mm', 'left': '10mm', 'right': '10mm'}.
  10. Close the browser. After saving the PDF, call browser.close() to free resources.
  11. Run the script. Execute python capture_invoice.py in your terminal. Check the generated PDF file in the same directory.

Common Mistakes

  • Not waiting for dynamic content. If the invoice loads data via JavaScript, capturing too early results in a blank PDF. Always use networkidle or a timeout.
  • Using the wrong browser. Playwright’s PDF generation only works in headless Chromium. If you launch Firefox or WebKit, the pdf() method will not work.
  • Forgetting to install the browser. Running playwright install is essential; otherwise, you’ll get an error about missing executable.
  • Not handling login sessions. If the invoice is behind authentication, you need to log in first. Saving cookies or using a persistent context can help.
  • Ignoring print backgrounds. Without print_background=True, the PDF may lack colored headers or logos.

Tips

  • Use a persistent context to stay logged in. Launch with p.chromium.launch_persistent_context(user_data_dir='./profile') to save login state across runs.
  • Customize the PDF layout. You can set scale=0.8 to fit more content per page, or use prefer_css_page_size=True to respect the page’s CSS.
  • Add a timestamp to the filename. Use Python’s datetime module to generate unique names like invoice_20250101_1530.pdf.
  • Use headless mode for speed. By default, Playwright runs headless, but you can set headless=False to see the browser while debugging.
  • Handle multiple invoices in a loop. Put the URL in a list and iterate to batch-download all invoices.

Troubleshooting

  • PDF is blank. This usually means the page wasn’t fully loaded. Increase the wait time or use page.wait_for_selector('selector-of-invoice-element') to wait for a specific element.
  • Error: “pdf() is not supported in this browser”. Ensure you’re using Chromium. Launch with p.chromium.launch() and not Firefox or WebKit.
  • Login required but not working. If the login form is complex, use page.fill() and page.click() to interact. Alternatively, save cookies after manual login and reuse them with context.add_cookies().
  • PDF is cut off or too small. Adjust the format to ‘A4’ or ‘Letter’, and set scale to a value between 0.5 and 2.0. Also check the page’s CSS for fixed widths.
  • Script hangs. If the page never reaches networkidle, use a timeout: page.wait_for_load_state('networkidle', timeout=10000) and catch the exception.

Frequently Asked Questions

Can I capture a page that requires a login?

Yes, you can automate the login by filling in the username and password fields, then navigate to the invoice. For repeated use, save the login state using a persistent context.

Does Playwright support PDF generation in all browsers?

No, only Chromium (and Chrome) support the page.pdf() method. Firefox and WebKit do not have this feature in Playwright.

How can I capture only a specific section of the invoice?

You can use page.locator('selector').screenshot() to capture an element as an image, but for PDF, you’d need to clip the page or use CSS to hide other parts. A simpler approach is to use the browser’s print dialog with a custom CSS.

Can I run this script on a server without a display?

Yes, Playwright runs headless by default, so it works on any server with Python installed. Just ensure you’ve installed the necessary system dependencies (like libnss3) if on Linux.

How do I handle pages that open in new tabs?

Use page.expect_popup() to catch the new tab, then switch to it and generate the PDF from that page.

Is it possible to add headers and footers to the PDF?

Yes, you can pass display_header_footer=True and set header_template and footer_template with HTML. For example, to add page numbers: footer_template='

Page of

'.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.