Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Use the low-level rendering API when a direct
Converter.convertHTML() call does not provide enough control. Combine an HTMLDocument with
renderTo() or a format-specific renderer and output device to control PDF, XPS, DOCX, or image output.
Aspose.HTML for Java provides two related approaches to format conversion. The static
Converter methods cover common conversion workflows. The renderTo() method and format-specific renderers expose the lower-level rendering pipeline.
| Approach | Use it when |
|---|---|
Converter.convertHTML() and other Converter methods | You need a concise conversion workflow with default or customized save options. |
| Document.renderTo(device) | You have one loaded document and want to send it directly to a configured output device. |
| Renderer.render(device, sources) | You need to process multiple source documents together or control the rendering timeout. |
For device-based output such as PDF, XPS, DOCX, and images, either approach can be used. The Converter class also covers conversions that are not represented by rendering devices. The main difference is the level at which your application controls the operation.
A rendering device implements the IDevice interface and receives drawing commands generated from the source document. Aspose.HTML for Java provides devices and matching option classes for the principal output types:
| Output | Device | Rendering options |
|---|---|---|
| PdfDevice | PdfRenderingOptions | |
| XPS | XpsDevice | XpsRenderingOptions |
| DOCX | DocDevice | DocRenderingOptions |
| Images | ImageDevice | ImageRenderingOptions |
To render a document to an output device:
renderTo(device) on the document.The following example initializes an HTMLDocument from an HTML string and renders it to output.pdf through PdfDevice with default rendering options.
1// Render HTML to PDF using Java
2
3// Prepare HTML code
4String code = "<span>Hello, World!!</span>";
5
6// Initialize an HTML document from HTML code
7try (HTMLDocument document = new HTMLDocument(code, ".")) {
8
9 // Create an instance of the PdfDevice class and specify the output file to render
10 try (PdfDevice device = new PdfDevice("output.pdf")) {
11
12 // Render HTML to PDF
13 document.renderTo(device);
14 }
15}Rendering options are passed to an output device and control the layout and format-specific behavior. General settings include page setup, resolution, CSS media type, and background color. PDF and image devices also expose settings specific to their output formats.
To apply custom PDF page dimensions:
PdfRenderingOptions.Page through PageSetup with the required dimensions.PdfDevice.The next example sets the PDF page to 5 × 2 inches and renders an inline HTML document to output.pdf.
1// Render HTML to PDF in Java with custom page size
2
3// Prepare HTML code
4String code = "<span>Hello, World!!</span>";
5
6// Initialize a HTML document from the HTML code
7try (HTMLDocument document = new HTMLDocument(code, ".")) {
8
9 // Create an instance of PdfRenderingOptions and set a custom page-size
10 PdfRenderingOptions options = new PdfRenderingOptions();
11 PageSetup pageSetup = new PageSetup();
12 Page anyPage = new Page();
13 anyPage.setSize(
14 new Size(
15 Length.fromInches(5),
16 Length.fromInches(2)
17 )
18 );
19 pageSetup.setAnyPage(anyPage);
20 options.setPageSetup(pageSetup);
21
22 // Create a PDF Device and specify options and output file
23 try (PdfDevice device = new PdfDevice(options, "output.pdf")) {
24
25 // Render HTML to PDF
26 document.renderTo(device);
27 }
28}The RenderingOptions base class provides settings shared by output devices. CssOptions controls how CSS media queries are resolved during rendering.
Horizontal and vertical resolution values are measured in pixels per inch. For PDF output, these settings affect internal raster images, including images used while processing filters; they do not simply increase the quality of vector text. For image output, resolution also affects the raster output.
The following example renders the same HTML document to two PDF files using horizontal and vertical resolution values of 50 dpi and 300 dpi.
1// Render HTML to PDF with custom resolution using Java
2
3// Prepare HTML code and save it to a file
4String code = "< style >\n" +
5 " p\n" +
6 " {\n" +
7 " background:\n" +
8 " blue;\n" +
9 " }\n" +
10 " @media(min - resolution:300dpi)\n" +
11 " {\n" +
12 " p\n" +
13 " {\n" +
14 " /* high resolution screen color */\n" +
15 " background:\n" +
16 " green\n" +
17 " }\n" +
18 " }\n" +
19 " </style >\n" +
20 " <p > Hello World !! </p >\n";
21
22try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.html")) {
23 fileWriter.write(code);
24}
25
26// Create an instance of the HTMLDocument class
27try (HTMLDocument document = new HTMLDocument("document.html")) {
28
29 // Create options for low-resolution screens
30 PdfRenderingOptions options = new PdfRenderingOptions();
31 options.setHorizontalResolution(Resolution.to_Resolution(50d));
32 options.setVerticalResolution(Resolution.to_Resolution(50d));
33
34 // Create an instance of the PdfDevice
35 try (PdfDevice device = new PdfDevice(options, "output_resolution_50.pdf")) {
36
37 // Render HTML to PDF
38 document.renderTo(device);
39 }
40
41 // Create options for high-resolution screens
42 options = new PdfRenderingOptions();
43 options.setHorizontalResolution(Resolution.to_Resolution(300d));
44 options.setVerticalResolution(Resolution.to_Resolution(300d));
45
46 // Create an instance of PDF device
47 try (PdfDevice device = new PdfDevice(options, "output_resolution_300.pdf")) {
48
49 // Render HTML to PDF
50 document.renderTo(device);
51 }
52}CSS media types let a document apply different style rules for screen and print output. They can be assigned to a linked style sheet or used in an @media rule:
Linked style sheet
1<link rel="stylesheet" media="print" href="style.css">Inline style sheet
1<style>
2@media print {
3 body { color: #000000; }
4}
5</style>Set the media type through options.getCss().setMediaType(). The example below selects MediaType.Screen before rendering HTML to PDF.
1// Render HTML to PDF with custom MediaType settings with Java
2
3// Prepare HTML code
4String code = "<span>Hello, World!!</span>";
5
6// Initialize an HTML document from the HTML code
7try (HTMLDocument document = new HTMLDocument(code, ".")) {
8
9 // Create an instance of the PdfRenderingOptions class
10 PdfRenderingOptions options = new PdfRenderingOptions();
11 // Set the 'screen' media-type
12 options.getCss().setMediaType(MediaType.Screen);
13
14 // Create a PDF Device and specify options and output file
15 try (PdfDevice device = new PdfDevice(options, "output.pdf")) {
16
17 // Render HTML to PDF
18 document.renderTo(device);
19 }
20}The default CSS media type is MediaType.Print. Use MediaType.Screen when the output should apply screen-specific media queries instead of print-specific rules.
The background color setting fills each output page behind the document content. Its default value is transparent. Set an explicit color when the target format or design requires a predictable page background.
The following example sets PdfRenderingOptions background color to cyan and renders document.html to output.pdf.
1// Render HTML to PDF with custom background color using Java
2
3// Prepare HTML code and save it to a file
4String code = "<p>Hello, World!!</p>";
5try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.html")) {
6 fileWriter.write(code);
7}
8
9// Create an instance of the HTMLDocument class
10try (HTMLDocument document = new HTMLDocument("document.html")) {
11
12 // Initialize options with 'cyan' as a background-color
13 PdfRenderingOptions options = new PdfRenderingOptions();
14 options.setBackgroundColor(Color.getCyan());
15
16 // Create an instance of the PdfDevice class
17 try (PdfDevice device = new PdfDevice(options, "output.pdf")) {
18
19 // Render HTML to PDF
20 document.renderTo(device);
21 }
22}PageSetup controls page dimensions, margins, page-specific configurations, and the interaction between conversion settings and CSS @page rules. When document content is wider than the configured page, setAdjustToWidestPage(true) can expand the page width instead of clipping the content.
The adjustment is applied only when the widest content exceeds the configured width, and the adjusted size is used for all output pages. This option can increase processing time.
The next example starts with a 500 × 200 pixel page and adjusts the PDF page width to fit the widest HTML content.
1// Render HTML to PDF and adjust to the widest page with Java
2
3// Prepare HTML code
4String code = " <style>\n" +
5 " div {\n" +
6 " page - break -after:always;\n" +
7 " }\n" +
8 " </style >\n" +
9 " <div style = 'border: 1px solid red; width: 400px' > First Page</div >\n" +
10 " <div style = 'border: 1px solid red; width: 600px' > Second Page</div >\n";
11// Initialize an HTML document from HTML code
12try (HTMLDocument document = new HTMLDocument(code, ".")) {
13
14 // Create an instance of the PdfRenderingOptions class and set a custom page-size
15 PdfRenderingOptions options = new PdfRenderingOptions();
16 options.getPageSetup().setAnyPage(new Page(new Size(500, 200)));
17
18 // Enable auto-adjusting for the page size
19 options.getPageSetup().setAdjustToWidestPage(true);
20
21 // Create an instance of the PdfDevice class and specify options and output file
22 try (PdfDevice device = new PdfDevice(options, "output.pdf")) {
23
24 // Render HTML to PDF
25 document.renderTo(device);
26 }
27}PdfRenderingOptions includes the general rendering settings and PDF-specific properties for document information, encryption, form-field behavior, and JPEG compression quality.
The following example configures user and owner passwords, permits printing, selects the RC4_128 encryption algorithm, and renders the HTML document to output.pdf.
1// Render HTML to PDF with password protection using Java
2
3// Prepare HTML code
4String code = "<div>Hello, World!!</div>";
5
6// Initialize an HTML document from the HTML code
7try (HTMLDocument document = new HTMLDocument(code, ".")) {
8
9 // Create the instance of the PdfRenderingOptions class
10 PdfRenderingOptions options = new PdfRenderingOptions();
11
12 // Set file permissions
13 options.setEncryption(
14 new PdfEncryptionInfo(
15 "user_pwd",
16 "owner_pwd",
17 PdfPermissions.PrintDocument,
18 PdfEncryptionAlgorithm.RC4_128
19 )
20 );
21
22 // Create a PDF Device and specify options and output file
23 try (PdfDevice device = new PdfDevice(options, "output.pdf")) {
24
25 // Render HTML to PDF
26 document.renderTo(device);
27 }
28}ImageRenderingOptions controls the raster image format, resolution, antialiasing, compression, background, and page setup.
The next example selects JPEG output, disables smoothing, sets horizontal and vertical resolution to 75 dpi, and renders the inline HTML document to output.jpg.
1// Render HTML to JPG with custom resolution and antialiasing settings with Java
2
3// Prepare HTML code
4String code = "<div>Hello, World!!</div>";
5
6// Initialize an instance of the HTMLDocument class based on prepared code
7try (HTMLDocument document = new HTMLDocument(code, ".")) {
8
9 // Create an instance of the ImageRenderingOptions class
10 ImageRenderingOptions options = new ImageRenderingOptions();
11 options.setFormat(ImageFormat.Jpeg);
12
13 // Disable smoothing mode
14 options.setSmoothingMode(SmoothingMode.None);
15
16 // Set the image resolution as 75 dpi
17 options.setVerticalResolution(Resolution.fromDotsPerInch(75));
18 options.setHorizontalResolution(Resolution.fromDotsPerInch(75));
19
20 // Create an instance of the ImageDevice class
21 try (ImageDevice device = new ImageDevice(options, "output.jpg")) {
22
23 // Render HTML to Image
24 document.renderTo(device);
25 }
26}The renderTo(device) method sends one loaded document to a device. Format-specific renderers can accept multiple sources:
HTMLDocument objects.The following example passes three HTMLDocument objects to HtmlRenderer and writes their rendered content to one output.pdf file. It combines the output in a PDF; it does not merge the source DOM trees into a new HTML document.
1// Merge HTML to PDF using Java
2
3// Prepare HTML code
4String code1 = "<br><span style='color: green'>Hello, World!!</span>";
5String code2 = "<br><span style='color: blue'>Hello, World!!</span>";
6String code3 = "<br><span style='color: red'>Hello, World!!</span>";
7
8// Create three HTML documents to merge later
9HTMLDocument document1 = new HTMLDocument(code1, ".");
10HTMLDocument document2 = new HTMLDocument(code2, ".");
11HTMLDocument document3 = new HTMLDocument(code3, ".");
12
13// Create an instance of HTML Renderer
14HtmlRenderer renderer = new HtmlRenderer();
15
16// Create an instance of the PdfDevice class
17try (PdfDevice device = new PdfDevice("output.pdf")) {
18
19 // Merge all HTML documents to PDF
20 renderer.render(device, new HTMLDocument[]{document1, document2, document3});
21}Renderer overloads with a timeout limit how long the rendering pipeline waits for conditions such as resource loading, active timers, and animation tasks. For the integer overload used by the example, the timeout value is measured in milliseconds; -1 represents an indefinite wait.
The example passes 5 as the timeout value while rendering an HTMLDocument to output.pdf.
1// Render HTML to PDF with timeout settings using Java
2
3// Prepare HTML code
4String code = "< script >\n" +
5 " var count = 0;\n" +
6 " setInterval(function()\n" +
7 " {\n" +
8 " var element = document.createElement('div');\n" +
9 " var message = (++count) + '. ' + 'Hello, World!!';\n" +
10 " var text = document.createTextNode(message);\n" +
11 " element.appendChild(text);\n" +
12 " document.body.appendChild(element);\n" +
13 " },1000);\n" +
14 "</script >\n";
15
16// Initialize an HTML document based on prepared HTML code
17try (HTMLDocument document = new HTMLDocument(code, ".");) {
18
19 // Create an instance of HTML Renderer
20 HtmlRenderer renderer = new HtmlRenderer();
21
22 // Create an instance of the PdfDevice class
23 try (PdfDevice device = new PdfDevice("output.pdf")) {
24
25 // Render HTML to PDF
26 renderer.render(device, 5, document);
27 }
28}Download complete Java examples and data files from GitHub.
Use the Converter class for concise conversions based on save options. Use renderTo() or a format-specific renderer when you need direct control over a device, multiple input documents, or a rendering timeout.
No. HtmlRenderer can send several HTMLDocument objects to one output device, such as a PDF device. It does not combine their DOM trees or save a new merged HTML document.
The low-level rendering API complements the Converter class. Rendering devices define the destination, rendering options control output behavior, and format-specific renderers support multi-source and timeout scenarios.
Use the free online HTML applications for quick manual conversions and checks. Use Aspose.HTML for Java rendering APIs when output settings must be controlled programmatically.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.