Renderers – Render HTML, MHTML, EPUB, and SVG in Python
Renderers
The aspose.html.rendering namespace consists of numerous renderer objects and appropriate low-level options classes, which are responsible for rendering documents into IDevice implementation. Aspose.HTML for Python via .NET API provides the following realization of renderers: HtmlRenderer, SvgRenderer, MhtmlRenderer, and EpubRenderer, which are used to render HTML, SVG, MHTML, and EPUB documents, respectively.
The current article describes supported scenarios of HTML-based file conversion to other popular formats using render() methods of the
aspose.html.rendering namespace.
HtmlRenderer
The
HtmlRenderer class can be used to render HTML files into various popular formats, allowing for customization of rendering options and control over the output of the process. Let’s look at using the HtmlRenderer class to render HTML to PDF with custom output settings:
- Initialize an HTML document using the HTMLDocument class.
- Create a new HtmlRenderer object.
- Create an instance of the
PdfRenderingOptions and customize the options for your output PDF document. For example, you can set the
any_page property of the
PageSetupclass to create a new Page object with dimensions of 600 pixels by 200 pixels. Additionally, you can configure the encryption property, specifying user and owner passwords, permitted actions, and the encryption algorithm for the output PDF file. - Use PdfDevice(options, savePath) constructor to create an object of the PdfDevice class.
- Call the render(device, document) method to render HTML to PDF with the specified rendering options.
1# Render HTML to PDF with custom page settings using Python
2
3import os
4import aspose.html as ah
5import aspose.html.rendering as rn
6import aspose.html.rendering.pdf as rp
7import aspose.html.drawing as dr
8import aspose.html.rendering.pdf.encryption as rpe
9
10# Setup input and output directories
11data_dir = "data/"
12output_dir = "output/"
13os.makedirs(output_dir, exist_ok=True)
14
15# Prepare path to the source HTML file
16document_path = os.path.join(data_dir, "document.html")
17
18# Initialize an HTML document from the file
19doc = ah.HTMLDocument(document_path)
20
21# Create an instance of the HTML Renderer
22renderer = rn.HtmlRenderer()
23
24# Prepare path to save the converted PDF
25save_path = os.path.join(output_dir, "render-html-with-options.pdf")
26
27# Create PDF rendering options and set custom page size
28options = rp.PdfRenderingOptions()
29options.page_setup.any_page = dr.Page(dr.Size(600, 200))
30
31# Setup PDF encryption
32options.encryption = rpe.PdfEncryptionInfo(
33 user_password="user_pwd",
34 owner_password="owner_pwd",
35 permissions=rpe.PdfPermissions.PRINT_DOCUMENT,
36 encryption_algorithm=rpe.PdfEncryptionAlgorithm.RC4_128
37)
38
39# Create the PDF device with options and output path
40device = rp.PdfDevice(options, save_path)
41
42# Render HTML to PDF
43renderer.render(device, doc)Rendering options give you additional control over the rendering process. To learn more about them, please read the Rendering options article.
To learn more about the rendering process, see the article Rendering Device.
SvgRenderer
The SvgRenderer allows you to render SVG files to other file formats, such as PDF, XPS, DOCX, and image formats. The following example demonstrates how to use SvgRenderer class to render SVG to PNG with a custom page size specifying:
- Initialize an SVG document. Use one of the SVGDocument() constructors to create an SVGDocument instance.
- Create a new SvgRenderer object.
- Create an instance of the ImageRenderingOptions and set custom options for the output PNG image. In the example, we specify the any_page property that sets a new Page object with a page size of 400x300 pixels.
- Use the ImageDevice(options, savePath) constructor to create an object of the ImageDevice class.
- Call the render(device, document) method to render SVG to PNG with the specified rendering options.
1# Render SVG to PNG with custom page settings using Python
2
3import os
4import aspose.html as ah
5import aspose.html.rendering.image as ri
6import aspose.html.rendering as rn
7import aspose.html.drawing as dr
8
9# Setup input and output directories
10data_dir = "data/"
11output_dir = "output/"
12os.makedirs(output_dir, exist_ok=True)
13
14# Prepare path to the source SVG file
15doc_path = os.path.join(data_dir, "flower.svg")
16save_path = os.path.join(output_dir, "render-svg-with-options.png")
17
18# Initialize an SVG document from the file
19doc = ah.dom.svg.SVGDocument(doc_path)
20
21# Create an instance of the SVG Renderer
22renderer = rn.SvgRenderer()
23
24# Create the instance of Rendering Options and set a custom page-size
25options = ri.ImageRenderingOptions()
26options.page_setup.any_page = dr.Page(dr.Size(400, 300))
27
28# Create the PNG device with options and output path
29device = ri.ImageDevice(options, save_path)
30
31# Render SVG to PNG
32renderer.render(device, doc)MhtmlRenderer
This example illustrates how to convert an MHTML document to PDF using the MhtmlRenderer class with specified rendering options:
- Open an existing MHTML document.
- Create an instance of the MhtmlRenderer class using the MhtmlRenderer() constructor.
- Initialize the PdfRenderingOptions class and set rendering options.
- Create an instance of the PdfDevice class.
- Call the render(device, stream) method to render MHTML to PDF.
1# Render MHTML to PDF with custom page settings using Python
2
3import os
4import aspose.html.rendering.pdf as rp
5import aspose.html.rendering as rn
6import aspose.html.drawing as dr
7import aspose.pydrawing as pd
8
9# Setup input and output directories
10data_dir = "data/"
11output_dir = "output/"
12os.makedirs(output_dir, exist_ok=True)
13
14# Prepare path to the source MHTML file
15epub_path = os.path.join(data_dir, "document.mht")
16save_path = os.path.join(output_dir, "render-mhtml-with-options.pdf")
17
18# Open the MHTML file in binary mode
19with open("document.mht", 'rb') as stream:
20
21 # Create an instance of MHTML Renderer
22 renderer = rn.MhtmlRenderer()
23
24 # Create PDF rendering options and set a custom page size and background color
25 options = rp.PdfRenderingOptions()
26 options.page_setup.any_page = dr.Page(dr.Size(800, 400))
27 options.background_color = pd.Color.bisque
28
29 # Create an instance of PdfDevice for output
30 device = rp.PdfDevice(options, save_path)
31
32 # Render MHTML to PDF
33 renderer.render(device, stream)EpubRenderer
The EpubRenderer class allows you to convert EPUB files to other formats such as PDF, XPS, DOCX, and images. The following Python examples shows how to convert EPUB to DOCX with a custom page size setting:
- Open an existing EPUB file.
- Create an instance of EpubRenderer class.
- Initialize the DocRenderingOptions class and set rendering options.
- Create an instance of the DocDevice class.
- Call the render(device, stream) method to render EPUB to DOCX.
1# Render EPUB to DOCX with custom page settings using Python
2
3import os
4import aspose.html.rendering.doc as rd
5import aspose.html.rendering as rn
6import aspose.html.drawing as dr
7
8# Setup input and output directories
9data_dir = "data/"
10output_dir = "output/"
11os.makedirs(output_dir, exist_ok=True)
12
13# Prepare path to the source EPUB file
14epub_path = os.path.join(data_dir, "input.epub")
15save_path = os.path.join(output_dir, "render-epub-with-options.docx")
16
17# Open the EPUB file in binary mode
18with open(epub_path, "rb") as stream:
19
20 # Create an instance of EPUB Renderer
21 renderer = rn.EpubRenderer()
22
23 # Create DOC rendering options and set a custom page size
24 options = rd.DocRenderingOptions()
25 options.page_setup.any_page = dr.Page(dr.Size(800, 400))
26
27 # Create an instance of DocDevice for output
28 device = rd.DocDevice(options, save_path)
29
30 # Render EPUB to DOCX
31 renderer.render(device, stream)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.
