Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
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.
The following example loads a webpage directly from its URL and converts the resulting HTMLDocument to website.pdf with the default PDF settings.
HTMLDocument.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.
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.
Use PdfSaveOptions to control the generated PDF. The most relevant settings for webpage conversion include:
| Requirement | Setting |
|---|---|
| Apply screen or print CSS | options.getCss().setMediaType() |
| Set page dimensions and margins | options.getPageSetup() |
| Fit content wider than the configured page | options.getPageSetup().setAdjustToWidestPage(true) |
| Control internal raster-image resolution | options.setHorizontalResolution() and options.setVerticalResolution() |
| Configure JPEG image quality | options.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.
| Issue | Cause and fix |
|---|---|
| CSS or images are missing | The linked resources may be unreachable, blocked, or protected. Check network access, resource URLs, and authentication requirements. |
| The PDF looks different from the browser page | PDF conversion uses print CSS by default. Select MediaType.Screen when screen styles are required. |
| Web fonts are replaced | The 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 clipped | Configure 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 pages | PDF 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 correctly | The basic URL example does not provide application-specific credentials. Configure the required network request workflow before loading protected content. |
Yes. Pass the URL to the HTMLDocument constructor and convert the loaded document with Converter.convertHTML(), as shown in the first example.
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.
Call options.getCss().setMediaType(MediaType.Screen) before conversion. Leave the default print media type unchanged when the website’s print stylesheet should be used.
No. Aspose.HTML renders the document into paginated PDF pages. A browser screenshot is a raster capture of a particular viewport and browser state.
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.
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.
Use the free online HTML to PDF Converter to convert a webpage or HTML source to PDF without installing software.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.