For an existing invoice webpage, use Playwright for Java: call page.pdf() to create a paginated PDF, or page.screenshot() to save the rendered page as an image. A PDF follows print layout by default; a screenshot captures pixels. Choose based on whether you need a document or a visual record of the page.
Choose a PDF or an image screenshot
“PDF screenshot” can mean two different outputs. A PDF is laid out for pages and can change when print styles, paper size, or margins differ. A screenshot is an image of the browser-rendered page.
- Use a PDF when you need a paginated document to file, print, or share.
- Use a full-page screenshot when you need one image containing the page’s scrollable content.
- Use an element screenshot when only the invoice panel or another specific region matters.
For a live page where the browser layout and rendered content matter, Playwright Java is the most direct fit: its Page API documents both PDF generation and screenshots (Playwright Java Page API; Playwright Java screenshots guide).
Capture an invoice page with Playwright Java
The example below opens a URL, waits for the invoice content to be visible, and writes a PDF. Replace the example URL and selector with the invoice page and a stable element on that page. If access requires authentication, use an authorized browser session or an approved sign-in flow; do not put credentials directly in source code.
- Add Playwright for Java to your project using the current installation instructions in the Playwright Java documentation. Install the browser binaries required by your Playwright version.
- Run this Java code, changing
invoiceUrlandreadySelectorto match the target page.
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.options.WaitForSelectorState;
import java.nio.file.Paths;
public class SaveInvoicePdf {
public static void main(String[] args) {
String invoiceUrl = "https://example.com/invoice";
String readySelector = "#invoice";
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium().launch(
new BrowserType.LaunchOptions().setHeadless(true));
Page page = browser.newPage();
page.navigate(invoiceUrl);
page.locator(readySelector).waitFor(
new com.microsoft.playwright.Locator.WaitForOptions()
.setState(WaitForSelectorState.VISIBLE));
page.pdf(new Page.PdfOptions()
.setPath(Paths.get("invoice.pdf"))
.setFormat("A4")
.setPrintBackground(true)
.setPreferCSSPageSize(true));
browser.close();
}
}
}
The Page API supports PDF output settings including paper format, margins, scale, printing backgrounds, and preferring CSS page size. In the example, CSS page sizing takes precedence where the page defines it, and background graphics are included; adjust those choices to match the intended document (Playwright Java Page API).
Choose print or screen styling for the PDF
page.pdf() uses print CSS media by default. This means a page may intentionally hide navigation, change colors, or rearrange content when printed. If the PDF should resemble screen styling instead, emulate screen media before calling page.pdf():
Rank #2
page.emulateMedia(new Page.EmulateMediaOptions().setMedia(com.microsoft.playwright.options.Media.SCREEN));
page.pdf(new Page.PdfOptions()
.setPath(Paths.get("invoice-screen-style.pdf"))
.setFormat("A4")
.setPrintBackground(true));
Screen media does not turn a PDF into a pixel screenshot: the result remains a paginated PDF. Review the output when page breaks, invoice totals, or background colors are important. The available controls and default print-media behavior are described in the Page API.
Save a full-page or targeted image
Use page.screenshot() when the required deliverable is an image. Set full-page capture for the full scrollable document, or take a locator screenshot to isolate the invoice area. Playwright’s screenshot guide documents full-page capture, element screenshots, and returning screenshot bytes (Playwright Java screenshots guide).
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchimport com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
import java.nio.file.Paths;
// Full scrollable page as a PNG image
page.screenshot(new Page.ScreenshotOptions()
.setPath(Paths.get("invoice-full.png"))
.setFullPage(true));
// Only the invoice element as a PNG image
page.locator("#invoice").screenshot(new Locator.ScreenshotOptions()
.setPath(Paths.get("invoice-panel.png")));
If you need a PDF containing image pixels rather than a browser-generated, print-laid-out PDF, capture the image and place it into a PDF as a separate step. That workflow preserves the captured appearance but does not provide the same text and page-layout behavior as generating a PDF directly from the page.
Wait for the invoice to be ready
Invoice pages may render after navigation, especially when content is loaded asynchronously. Waiting for a meaningful invoice element is more useful than assuming that a completed navigation means the invoice is ready. Choose a selector that appears only when the relevant invoice content is present. If the page updates that element before all required fields finish loading, wait for an additional stable indicator or a known application state.
Rank #4
- Navigation: navigate to the intended invoice URL, including any required query parameters.
- Readiness: wait for the invoice container or another page-specific marker before saving.
- Authentication: handle login through an authorized session or the site’s supported process. The cited Playwright references do not establish the behavior of any particular Indian invoice portal or GST service.
- Output: use PDF settings for pagination, or screenshot options for image dimensions and capture scope.
Alternative Java libraries for controlled documents
Apache PDFBox
Apache PDFBox is a Java library for creating and manipulating PDFs, extracting content, and saving PDFs as images. It is useful when you are building or processing a PDF document in Java. It is not the direct browser-driving route for faithfully capturing an existing live webpage. The Apache project page lists PDFBox 2.0.37, released July 15, 2026, and PDFBox 3.0.8, released July 11, 2026; check the project page for current versions before selecting a dependency.
OpenHTMLtoPDF
OpenHTMLtoPDF is a JVM renderer for a supported subset of HTML and CSS that can output PDFs or images. It can suit a controlled document whose markup you own, but its documentation warns that modern HTML may not render well without adapting the source. For a dynamic invoice page whose actual browser rendering matters, prefer a browser-based capture unless the page can be simplified to the renderer’s supported subset.
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Clear out junk files and repair common Windows errors3Scan for outdated or missing drivers - takes under a minuteBest Value
Troubleshoot common capture problems
- The PDF looks different from the webpage: check whether print CSS is active. It is the default for
page.pdf(); use screen media emulation if screen styling is the goal. Also review page format, margins, scale, background printing, and CSS page-size preference. - The PDF is blank or missing invoice details: verify that navigation reached the invoice rather than a login, error, or redirect page, then wait for a visible invoice-specific selector before capture.
- The screenshot cuts off content: use
setFullPage(true)for the full scrollable page, or capture the invoice element directly if only that region is needed. - A PDF has unexpected pagination: inspect print styles, CSS page sizing, and the chosen paper format and margins. A PDF is paginated, unlike a full-page image.
- OpenHTMLtoPDF omits or mislays page elements: the page may use HTML or CSS outside the library’s supported subset; adapt the source or use a browser renderer.
No cited source establishes that a particular Indian invoice portal, login flow, or GST service will work without site-specific validation. Test the target page with an authorized account and verify that the saved artifact contains the required invoice content.
Or skip the browser setup
ScreenshotNeo provides a screenshot API and MCP server. A single GET request can return an image or PDF; the example below requests a PDF for an invoice URL. See the ScreenshotNeo documentation for request options and response details.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com/invoice -d format=pdf -o invoice.pdf
ScreenshotNeo accepts cookie or consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each step can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers identify the page verdict and billing status. Its MCP server provides take_screenshot, get_page_info, and capture_pdf for AI agents. The Free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots.
Sign up free for 1,000 screenshots a month with no card.
Frequently Asked Questions
Can Playwright Java save both PDFs and screenshots?
Yes. Use page.pdf() for a paginated PDF and page.screenshot() for an image.
Does a full-page screenshot create a PDF?
No. Full-page screenshot capture creates an image of the scrollable page. Use PDF generation separately when the deliverable must be a PDF.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

