Save a Webpage as PDF in Java

To save a webpage as PDF in Java, load its URL with HTMLDocument, create PdfSaveOptions, and pass the document, options, and output path to Converter.convertHTML(). Aspose.HTML loads the page and its accessible linked resources before creating the PDF.

Saving a webpage as PDF creates a fixed-layout copy that is convenient for archiving, printing, reporting, and sharing. Unlike converting a standalone HTML string, converting a live URL can require network access to the page’s CSS, images, fonts, and other linked resources.

This article shows how to convert a public website URL to PDF and how to select screen CSS when the PDF should resemble the webpage’s on-screen layout.

Save a Webpage as PDF in Java

The following example loads a webpage directly from its URL and converts the resulting HTMLDocument to website.pdf with the default PDF settings.

  1. Specify the URL of the webpage to convert.
  2. Load the URL into an HTMLDocument.
  3. Create a PdfSaveOptions object.
  4. Call Converter.convertHTML() with the document, save options, and PDF output path.
 1import com.aspose.html.HTMLDocument;
 2import com.aspose.html.converters.Converter;
 3import com.aspose.html.saving.PdfSaveOptions;
 4
 5String url = "https://docs.aspose.com/html/";
 6String outputPath = "website.pdf";
 7
 8HTMLDocument document = new HTMLDocument(url);
 9PdfSaveOptions options = new PdfSaveOptions();
10
11Converter.convertHTML(document, options, outputPath);

The result is a paginated PDF created from the loaded webpage. Linked resources are included when their URLs can be resolved and the application is permitted to access them.

Use Screen CSS When Saving a Webpage

Websites can define different rules for screen and print media. Aspose.HTML uses the Print media type by default for PDF output. When the PDF should apply screen-specific rules, import com.aspose.html.rendering.MediaType and set the media type after creating PdfSaveOptions:

options.getCss().setMediaType(MediaType.Screen);

This setting changes the result only when the source page has CSS that behaves differently for screen and print media. For pages without such differences, MediaType.Screen and the default MediaType.Print can produce identical PDFs. Keep the default value when the website’s print stylesheet should control the PDF layout.

Configure Webpage PDF Output

Use PdfSaveOptions to control the generated PDF. The most relevant settings for webpage conversion include:

RequirementSetting
Apply screen or print CSSoptions.getCss().setMediaType()
Set page dimensions and marginsoptions.getPageSetup()
Fit content wider than the configured pageoptions.getPageSetup().setAdjustToWidestPage(true)
Control internal raster-image resolutionoptions.setHorizontalResolution() and options.setVerticalResolution()
Configure JPEG image qualityoptions.setJpegQuality()

Page dimensions and CSS media type solve different problems. Page setup controls the physical PDF pages, while the media type determines which CSS media rules are applied. For examples of page size, margins, resolution, and other settings, see Convert HTML to PDF in Java and Fine-Tuning Converters.

If users can supply the source URL, validate the URL scheme and allowlist permitted hosts before loading it. Do not allow unrestricted requests to loopback addresses, private-network resources, or local files.

Common Webpage to PDF Issues

IssueCause and fix
CSS or images are missingThe linked resources may be unreachable, blocked, or protected. Check network access, resource URLs, and authentication requirements.
The PDF looks different from the browser pagePDF conversion uses print CSS by default. Select MediaType.Screen when screen styles are required.
Web fonts are replacedThe required fonts may be unavailable or inaccessible in the runtime environment. Install the fonts or configure a font folder as described in Environment Configuration.
Wide content is clippedConfigure a suitable page size or use setAdjustToWidestPage(true) when expanding all output pages to the widest content is acceptable.
A long webpage is split across pagesPDF is a paginated format. Adjust the page setup and review the website’s print CSS when a different page break is required.
A protected page does not load correctlyThe basic URL example does not provide application-specific credentials. Configure the required network request workflow before loading protected content.

FAQ

Can Aspose.HTML convert a webpage URL directly to PDF?

Yes. Pass the URL to the HTMLDocument constructor and convert the loaded document with Converter.convertHTML(), as shown in the first example.

Does Aspose.HTML include CSS and images from the webpage?

Aspose.HTML loads linked resources that can be resolved and accessed by the application. Resources can be missing from the PDF when they require credentials, are blocked by network policy, or have invalid URLs.

How do I make the PDF use screen styles?

Call options.getCss().setMediaType(MediaType.Screen) before conversion. Leave the default print media type unchanged when the website’s print stylesheet should be used.

Is a webpage-to-PDF conversion the same as a browser screenshot?

No. Aspose.HTML renders the document into paginated PDF pages. A browser screenshot is a raster capture of a particular viewport and browser state.

How does Aspose.HTML handle a long webpage?

The renderer divides the content into PDF pages according to the selected page setup and applicable CSS rules. Configure page dimensions, margins, and print styles when the default pagination does not meet your requirements.

Can I save a webpage as PDF for free?

Aspose.HTML for Java is a commercial API that can be evaluated before licensing. For occasional manual conversions, use the free online HTML to PDF Converter.

Related Articles

Try Online HTML to PDF Conversion

Use the free online HTML to PDF Converter to convert a webpage or HTML source to PDF without installing software.