Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Use
document.render_to() to send one loaded document directly to an output device. Use HtmlRenderer, SvgRenderer, MhtmlRenderer, or EpubRenderer when the application needs a source-specific renderer, several sources rendered to one output device, or a rendering timeout.
Renderers process source documents and send drawing operations to an IDevice implementation. The renderer must match the source type, while the device determines whether the result is written as PDF, XPS, DOCX, or a raster image.
| Source | Renderer | Expected input |
|---|---|---|
| HTML | HtmlRenderer | One or more HTMLDocument objects |
| SVG | SvgRenderer | One or more SVGDocument objects |
| MHTML | MhtmlRenderer | One or more binary streams |
| EPUB | EpubRenderer | One or more binary streams |
Choose the device separately according to the required output:
| Output | Device |
|---|---|
| PdfDevice | |
| Raster image | ImageDevice |
| DOCX | DocDevice |
| XPS | XpsDevice |
A renderer and a device therefore answer different questions: the renderer identifies what is being processed, while the device identifies the output format.
| API | Use it when |
|---|---|
document.render_to(device) | One HTML or SVG document is already loaded and should be sent directly to a device. |
renderer.render(device, source) | The application should explicitly select the renderer for an HTML, SVG, MHTML, or EPUB source. |
renderer.render(device, sources) | Several sources of the same supported type should be rendered to one output device. |
renderer.render(device, source, timeout) | The rendering operation needs a maximum waiting time. |
For common one-file conversions, the high-level Converter API is usually shorter. Renderers are most useful when their multi-source or timeout overloads are required.
HtmlRenderer accepts a list of HTMLDocument objects. The following example loads
document.html and
paragraphs.html, then sends both documents to one PdfDevice.
To render several HTML documents to one output:
HTMLDocument.HtmlRenderer instance.renderer.render(). 1import os
2import aspose.html as ah
3import aspose.html.rendering as rn
4import aspose.html.rendering.pdf as rp
5
6data_dir = "data"
7output_dir = "output"
8os.makedirs(output_dir, exist_ok=True)
9
10first_path = os.path.join(data_dir, "document.html")
11second_path = os.path.join(data_dir, "paragraphs.html")
12output_path = os.path.join(output_dir, "combined.pdf")
13
14with ah.HTMLDocument(first_path) as first_document, \
15 ah.HTMLDocument(second_path) as second_document:
16 with rp.PdfDevice(output_path) as device:
17 with rn.HtmlRenderer() as renderer:
18 renderer.render(device, [first_document, second_document])The result is one PDF containing the rendered output of both HTML documents. This operation does not combine their DOM trees and does not create a new HTML document.
The integer timeout overload limits how long the renderer waits for document lifecycle tasks during rendering. The value is measured in milliseconds; -1 means an indefinite wait. A timeout is useful when a document depends on remote resources, scripts, timers, or other work that may delay completion.
The following example gives HtmlRenderer up to 5,000 milliseconds to render an HTML document to PDF:
1import os
2import aspose.html as ah
3import aspose.html.rendering as rn
4import aspose.html.rendering.pdf as rp
5
6data_dir = "data"
7output_dir = "output"
8os.makedirs(output_dir, exist_ok=True)
9
10input_path = os.path.join(data_dir, "document.html")
11output_path = os.path.join(output_dir, "document-timeout.pdf")
12timeout = 5000
13
14with ah.HTMLDocument(input_path) as document:
15 with rp.PdfDevice(output_path) as device:
16 with rn.HtmlRenderer() as renderer:
17 renderer.render(device, document, timeout)If the rendering lifecycle does not finish within the specified interval, the resulting output may be incomplete. Choose a limit that allows expected resources and scripts to finish, and validate the generated file.
The same output device can be paired with different renderers. Match the renderer to the source and keep stream-based inputs open until render() completes:
HtmlRenderer with HTMLDocument objects;SvgRenderer with SVGDocument objects;MhtmlRenderer with MHTML streams opened in binary mode;EpubRenderer with EPUB streams opened in binary mode.For page size, margins, background color, image settings, PDF security, and DOCX font embedding, configure the corresponding device with the settings described in Rendering Options.
| Issue | Explanation and fix |
|---|---|
| A renderer rejects the source | Match HtmlRenderer to HTML documents, SvgRenderer to SVG documents, and the MHTML or EPUB renderer to binary streams of the corresponding format. |
| The output format is wrong | Choose the device by output format. For example, use PdfDevice for PDF and ImageDevice for raster images. |
| Several files do not become one HTML document | Multi-source rendering combines rendered output in the target device; it does not merge DOM trees or save a new HTML source file. |
| Output is incomplete when a timeout is used | Increase the timeout and check remote resources, scripts, timers, and other operations that delay completion. |
| An MHTML or EPUB stream cannot be read | Open the file in binary mode and keep the stream open until renderer.render() returns. |
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.