Fine-Tuning Converters – Convert HTML in Python
A Few Ways to Convert HTML in Python
You can convert HTML to various popular formats in Python online or programmatically. Converting from HTML to other formats can perform by using the
convert_html() methods of the Converter class, the
render_to () method of the HTMLDocument class, or the render()
method of the
Renderer class.
Why Use Fine-Tuning Converters?
In the Fine-Tuning Converters chapter, Aspose.HTML for Python via .NET offers alternative methods for rendering HTML-based documents, providing you with greater control over the rendering process in your Python application. Our Python library implements a set of rendering devices: PdfDevice, XpsDevice, DocDevice, and ImageDevice. Each device comes with its own unique options, implemented through the classes PdfRenderingOptions, XpsRenderingOptions, DocRenderingOptions, and ImageRenderingOptions, respectively. For instance, you can modify the page size, adjust margins and colors, reduce file size by changing image quality and resolution, and set a security password for PDF documents.
The current section describes supported scenarios of HTML-based files conversions to other popular formats by using the render_to () method of the HTMLDocument class and the render() methods of the Renderer class:
- Rendering Device – This article will provide an overview of rendering devices, including PdfDevice, XpsDevice, DocDevice, and ImageDevice. The methods
render_to()
andrender()
accept instances of these rendering devices as parameters, allowing you to convert HTML into PDF, XPS, DOCX, and image formats. - Rendering Options – You can customize the conversion process using various rendering options, such as page size, margins, image quality, orientation, background color, and more. These options are passed as parameters to the rendering devices, helping you achieve the desired output for your document.
- Renderers – This article discusses the supported renderers: HtmlRenderer, SvgRenderer, MhtmlRenderer, and EpubRenderer. Each is specifically designed to convert HTML, SVG, MHTML, and EPUB documents, respectively.
Render HTML to PDF using Python
This example demonstrates how to easily convert an online HTML page to a PDF document using Aspose.HTML for Python via .NET. Just a few lines of code allow you to load an HTML document from a URL and render it directly into a PDF file with default rendering options.
1import aspose.html as ah
2import aspose.html.rendering.pdf as rp
3
4doc = ah.HTMLDocument("https://docs.aspose.com/html/files/document.html")
5doc.render_to(rp.PdfDevice("output/document.pdf"))
What is Rendering Device in Aspose.HTML?
A rendering device in Aspose.HTML for Python via .NET represents a 2D drawing surface, implemented through the IDevice interface. It is responsible for converting HTML content into various output formats.
Aspose.HTML for Python via .NET provides the following rendering devices:
- PdfDevice – renders HTML to PDF. Supports rendering MHTML, SVG, and EPUB to PDF.
- XpsDevice – renders HTML to XPS. Supports rendering MHTML, SVG, and EPUB to XPS.
- DocDevice – renders HTML to DOCX. Supports rendering MHTML, SVG, and EPUB to DOCX.
- ImageDevice – renders HTML to image formats. Supports rendering MHTML, SVG, and EPUB to images.
Each device includes its own rendering options, available through classes like PdfRenderingOptions, XpsRenderingOptions, DocRenderingOptions, and ImageRenderingOptions. These options let you control output quality, layout, and other settings.
Aspose.HTML offers free Converters – an online collection of tools for converting HTML, XHTML, MHTML, EPUB, XML, or Markdown documents to PDF, XPS, DOCX, JPG, PNG, BMP, TIFF, GIF, Markdown, and other formats. These converters are compatible with any operating system and require no additional software installation. They’re a quick and easy way to convert HTML and HTML-based documents effectively.