Rendering Devices – Render HTML to PDF, XPS, DOCX, and Images
In this article, we describe how to use rendering devices – PdfDevice, XpsDevice, DocDevice, and ImageDevice to render HTML to PDF, XPS, DOCX, and Images. In the Python examples, the rendering process is considered with default rendering options. To learn more about customizing output, please read the Rendering Options article.
Understanding Rendering Devices in Aspose.HTML
In Aspose.HTML for Python via .NET, a rendering device represents a 2D drawing surface, implemented through the IDevice interface. Its main purpose is to convert the content of HTML and supported web formats (MHTML, SVG, and EPUB) into different output formats.
The Python API provides the following rendering devices:
- PdfDevice – renders HTML, MHTML, SVG, and EPUB to PDF.
- XpsDevice – renders HTML, MHTML, SVG, and EPUB to XPS.
- DocDevice – renders HTML, MHTML, SVG, and EPUB to DOCX.
- ImageDevice – renders HTML, MHTML, SVG, and EPUB to image formats.
Each device has specific rendering options: PdfRenderingOptions, XpsRenderingOptions, DocRenderingOptions, and ImageRenderingOptions. These allow precise control over output quality, layout, and formatting.
PdfDevice
The Aspose.Html.Rendering.Pdf namespace in the Aspose.HTML for Python via .NET library provides functionality for rendering HTML and related formats directly into PDF documents. It includes classes and parameters tailored for PDF output. Key Components:
PdfDeviceis the base rendering device class. It is used to convert HTML (or MHTML, SVG, EPUB, etc.) to a PDF file.PdfRenderingOptionsis a class that defines customizable settings for PDF output, such as page size, margins, image compression, metadata, encryption, and other layout/formatting options.
The next Python example shows how to use PdfDevice to render HTML to PDF. The process is considered with default rendering options:
1# Render HTML to Pdf using Python
2
3import os
4import aspose.html as ah
5import aspose.html.rendering.pdf as rp
6
7# Prepare output directory and path for the PDF file
8output_dir = "output/"
9os.makedirs(output_dir, exist_ok=True)
10save_path = os.path.join(output_dir, "document.pdf")
11
12# Prepare HTML code
13code = "<span>Hello, World!!</span>"
14
15# Initialize an HTML document from the HTML code
16doc = ah.HTMLDocument(code, ".")
17
18# Create a PDF Device and specify the output file to render
19device = rp.PdfDevice(save_path)
20
21# Render HTML to PDF
22doc.render_to(device)Rendering options give you additional control over the rendering process. To learn more about them, please read the Rendering options article.
ImageDevice
The
Aspose.Html.Rendering.Image namespace provides a specific
ImageDevice class as well as ImageRenderingOptions class responsible for rendering HTML files to raster formats: JPG, PNG, BMP, GIF, and TIFF.
This example demonstrates how to convert an HTML file to a JPEG image using Aspose.HTML for Python via .NET. The code loads an HTML document from disk, configures the ImageRenderingOptions for the JPEG format, and then uses the ImageDevice to render the document to an output image file.
1# Render HTML to JPG using Python
2
3import os
4import aspose.html as ah
5import aspose.html.rendering.image as ri
6
7# Setup input and output directories
8data_dir = "data/"
9output_dir = "output/"
10os.makedirs(output_dir, exist_ok=True)
11
12# Prepare paths for input HTML and output JPG file
13document_path = os.path.join(data_dir, "drawing.html")
14save_path = os.path.join(output_dir, "drawing-output.jpg")
15
16# Initialize an HTML document from the file
17doc = ah.HTMLDocument(document_path)
18
19# Create rendering options for image format (JPEG in this case)
20image_options = ri.ImageRenderingOptions(ri.ImageFormat.JPEG)
21
22# Create an ImageDevice and specify options and output file
23device = ri.ImageDevice(image_options, save_path)
24
25# Render HTML to JPG
26doc.render_to(device)XpsDevice
The Aspose.Html.Rendering.Xps namespace provides the XpsDevice class, which is used to render HTML content (or MHTML, SVG, EPUB, etc.) into XPS documents. The following Python example demonstrates this process with the default rendering options:
1# Render HTML to XPS using Python
2
3import os
4import aspose.html as ah
5import aspose.html.rendering.xps as rx
6
7# Setup input and output directories
8data_dir = "data/"
9output_dir = "output/"
10os.makedirs(output_dir, exist_ok=True)
11
12# Prepare paths for input HTML and output XPS file
13document_path = os.path.join(data_dir, "drawing.html")
14save_path = os.path.join(output_dir, "drawing-output.xps")
15
16# Initialize an HTML document from the file
17doc = ah.HTMLDocument(document_path)
18
19# Create rendering options for XPS format
20xps_options = rx.XpsRenderingOptions()
21
22# Create an XpsDevice and specify options and output file
23device = rx.XpsDevice(xps_options, save_path)
24
25# Render HTML to XPS
26doc.render_to(device)DocDevice
The Aspose.Html.Rendering.Doc namespace includes the DocDevice class, which enables rendering of HTML, MHTML, SVG, and EPUB files into DOCX format. The following Python example demonstrates this process using the default rendering options:
1# Render HTML to DOCX using Python
2
3import os
4import aspose.html as ah
5import aspose.html.rendering.doc as rd
6
7# Prepare output directory and path for the DOCX file
8output_dir = "output/"
9os.makedirs(output_dir, exist_ok=True)
10save_path = os.path.join(output_dir, "document.doc")
11
12# Initialize an HTML document from URL
13doc = ah.HTMLDocument("https://docs.aspose.com/html/files/document.html")
14
15# Create a DocDevice and specify the output file to render
16device = rd.DocDevice(save_path)
17
18# Render HTML to DOCX
19doc.render_to(device)Aspose.HTML offers free online Converters that can convert HTML, XHTML, MHTML, EPUB, XML, and Markdown files to a range of popular formats. You can easily convert your HTML-based documents to PDF, XPS, DOCX, JPG, PNG, GIF, TIFF, and others. Just select a file, choose the format to convert, and you’re done. Best of all, it’s completely free!