The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →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. These are different outputs. Playwright’s PDF uses print CSS by default, so emulate screen media first if the PDF needs the page’s screen styling. The steps below show both approaches and how to choose between them.
Choose PDF or screenshot image
A PDF is generally the better fit when you need a document with paper-sized pages, margins, and pagination. A screenshot is a raster image of the page as rendered in the browser; use a full-page screenshot for the scrollable page or an element screenshot to isolate the invoice panel. A PDF made from a webpage is not simply a screenshot: it follows print layout and can paginate content.
As an Amazon Associate I earn from qualifying purchases.
- Need a document: use
page.pdf(), then set paper size, margins, and background behavior to suit the invoice. - Need a visual capture: use
page.screenshot(); setfullPageto include the scrollable page or capture a specific locator.
Generate a PDF with Playwright for Java
Playwright’s Java Page API documents PDF generation and its options, including format, margins, scale, background printing, and CSS page-size preference. PDF generation uses print media by default. If the invoice’s screen styles are the desired basis, emulate screen media before calling pdf(). See the Playwright Java Page API.
The following example assumes the page is publicly reachable. Replace the URL with the invoice URL and choose the media mode that matches your requirement:
import com.microsoft.playwright.*;
import java.nio.file.Paths;
public class InvoicePdf {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium().launch();
Page page = browser.newPage();
page.navigate("https://example.com/invoice");
// Keep this line for screen styling; omit it for print styling.
page.emulateMedia(new Page.EmulateMediaOptions().setMedia(Media.SCREEN));
page.pdf(new Page.PdfOptions()
.setPath(Paths.get("invoice.pdf"))
.setFormat("A4")
.setPrintBackground(true)
.setMargin(new Page.PdfMargins()
.setTop("12mm")
.setRight("12mm")
.setBottom("12mm")
.setLeft("12mm")));
browser.close();
}
}
}
Use a current Playwright for Java dependency and install its browser binaries as described in the Playwright Java getting-started documentation. The code uses A4 as an example, not as a requirement for every invoice. If the page defines its own print page size and that size should win, consult the Page API’s CSS page-size option rather than assuming the default paper settings will match it.
Control print layout and media
For a conventional printable document, leave print media active and inspect the result for page breaks, clipped columns, and missing backgrounds. For a PDF intended to resemble what a visitor sees on screen, call emulateMedia with Media.SCREEN before generating it. Screen styling can be wider than the chosen paper size, so a screen-media PDF may still need a suitable scale or layout adjustment.
Rank #2
setPrintBackground(true) asks Playwright to include background graphics. If the invoice relies on colored fills or shaded table headers, include them and verify the output. Set margins deliberately; overly large margins can squeeze a wide invoice, while no margin may put important content too near the edge.
Save a full-page or invoice-only screenshot
Use screenshot capture when the output should be an image rather than a paginated document. Playwright’s Java screenshot guide covers full-page capture, element capture, and returning image bytes: Playwright Java screenshots.
import com.microsoft.playwright.*;
import java.nio.file.Paths;
public class InvoiceScreenshot {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium().launch();
Page page = browser.newPage();
page.navigate("https://example.com/invoice");
// Whole scrollable page:
page.screenshot(new Page.ScreenshotOptions()
.setPath(Paths.get("invoice.png"))
.setFullPage(true));
// Alternatively, capture just an invoice element:
// page.locator(".invoice").screenshot(
// new Locator.ScreenshotOptions().setPath(Paths.get("invoice-only.png")));
browser.close();
}
}
}
Replace .invoice with a selector that identifies the actual invoice container. A selector that matches nothing will not produce the intended element capture. A full-page image can be very tall; if the consumer needs a printable, paginated document, generate a PDF instead.
Wait for the invoice to be ready
A successful navigation does not by itself prove that the invoice content has finished rendering. When the page loads invoice data or images after the initial document response, wait for a page-specific visible condition before capturing. For example, if the invoice has a stable container:
Rank #4
page.navigate("https://example.com/invoice");
page.locator(".invoice").waitFor();
Use a selector that is present only when the meaningful invoice content is ready, not a generic element that appears before data has loaded. If the page requires a sign-in, use an authorized session and follow the site’s authentication flow; do not put credentials in source code or expose session cookies in logs. No general recipe can guarantee behavior for every Indian invoice portal, login flow, or GST service, so validate the actual page and its permitted use.
When a Java PDF library is a better fit
Apache PDFBox
Apache PDFBox is a Java library for creating and manipulating PDFs, extracting content, and rendering PDFs as images. It is useful when you need to work with PDF content in Java, but it is not the browser capture approach shown above for preserving a live webpage’s browser layout. The Apache project page lists PDFBox 2.0.37, released July 15, 2026, and 3.0.8, released July 11, 2026; check the project page for the current version and choose the appropriate dependency line.
Best Value
OpenHTMLtoPDF
OpenHTMLtoPDF is a pure-Java/JVM renderer that can produce PDFs or images from a supported subset of HTML and CSS. It may suit a controlled invoice template that you generate yourself. Its project documentation warns that modern HTML may not render well without adapting markup and layout. For an existing dynamic webpage whose browser rendering matters, a real browser is the more direct choice.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Troubleshooting capture problems
- The PDF looks different from the browser: PDF output uses print media by default. If screen styling is required, emulate screen media before
page.pdf(); otherwise review the site’s print CSS and paper settings. - The invoice is blank or incomplete: wait for the invoice-specific content to appear before capturing. Check whether the page needs authentication or loads data asynchronously.
- Background colors or graphics are missing in the PDF: set
setPrintBackground(true)and check whether the page’s print styles retain those backgrounds. - Content is clipped or breaks awkwardly: verify paper format, margins, scale, and whether print or screen media is active. A wide screen layout may not fit an A4 page cleanly.
- The screenshot omits content below the fold: set
setFullPage(true). For a focused capture, use a locator screenshot with a selector that matches the invoice. - Browser launch or navigation fails: confirm Playwright’s browser installation is complete, the URL is reachable from the runtime, and the page’s authentication requirements are met.
Or skip the browser setup
ScreenshotNeo offers a website screenshot API and MCP server. A single GET request can return an image or PDF; see the API documentation for parameters. Example cURL request for an invoice page:
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com/invoice -o invoice.webp
ScreenshotNeo accepts cookie or consent banners like a visitor and removes 60+ known consent platforms, newsletter popups, and chat widgets before capture; those steps can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers report 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 screenshots per month with no card; paid plans start at $5 for 3,000 shots. Sign up for 1,000 free screenshots a month, with no card.
Frequently Asked Questions
Can Playwright generate both an invoice PDF and a screenshot image?
Yes. Use page.pdf() for a PDF and page.screenshot() for an image; they follow different layout and output behavior.
Does this method guarantee that a particular Indian invoice or GST portal will work?
No. Portal-specific behavior, authentication, and permitted retention or sharing are not established here; verify the page and your use case directly.
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.




