Fine-Tune HTML, SVG, MHTML, and EPUB Rendering in Python

Use the Converter class for common conversions with save options. Use document.render_to() to send one loaded document directly to an output device, or use a source-specific renderer when the application needs direct control over the rendering pipeline.

Aspose.HTML for Python via .NET provides a high-level conversion API and a lower-level rendering API. Both can produce PDF, XPS, DOCX, and raster images, but they expose different levels of control.

Converter API vs. Rendering API

ApproachUse it when
Converter methodsYou need a concise conversion workflow with format-specific save options. This is the usual choice for HTML, SVG, MHTML, EPUB, and Markdown conversion.
document.render_to(device)You already have a loaded document and want to render it directly to a configured output device.
Renderer.render()You need to work explicitly with HtmlRenderer, SvgRenderer, MhtmlRenderer, or EpubRenderer and a target device.

Save options belong to the high-level Converter workflow. Rendering options are passed to rendering devices in the lower-level workflow. Choose one approach for a conversion rather than combining save options and rendering options in the same call.

How the Rendering Pipeline Works

The rendering API separates the source type, rendering process, output format, and output settings:

ComponentPurposeExamples
SourceSupplies the document or source stream to renderHTMLDocument, SVGDocument, MHTML stream, EPUB stream
RendererProcesses the corresponding source typeHtmlRenderer, SvgRenderer, MhtmlRenderer, EpubRenderer
DeviceDefines the output format and destinationPdfDevice, XpsDevice, DocDevice, ImageDevice
Rendering optionsConfigure layout and format-specific output behaviorPdfRenderingOptions, XpsRenderingOptions, DocRenderingOptions, ImageRenderingOptions

The device determines the target format; it does not determine the source type. For example, PdfDevice can receive drawing operations produced from HTML, SVG, MHTML, or EPUB workflows and write them as PDF.

Render HTML to PDF with PdfDevice

The following example uses the lower-level rendering API with default PDF rendering options. It loads a webpage into an HTMLDocument, creates a PdfDevice, and sends the document to that device with render_to().

To render a loaded document to an output device:

  1. Load or create the source document.
  2. Create the device for the required output format and specify the destination.
  3. Call render_to() on the document and pass the device.
1import aspose.html as ah
2import aspose.html.rendering.pdf as rp
3
4with ah.HTMLDocument("https://docs.aspose.com/html/files/document.html") as document:
5    device = rp.PdfDevice("document.pdf")
6    document.render_to(device)

The result is document.pdf created with the default settings of PdfDevice. To customize page layout, PDF security, image settings, or DOCX output, create the matching rendering-options object and pass it to the corresponding device.

Choose a Fine-Tuning Guide

When to Use Fine-Tuning APIs

Use the rendering API when the application needs to:

For ordinary file, string, stream, or URL conversion, start with the appropriate Converter guide and move to the rendering API only when the lower-level workflow provides a practical advantage.

Related Conversion Guides

Other Platforms

Try Online Conversion Tools

Use the free online HTML applications for quick manual conversions. Use the Aspose.HTML for Python via .NET rendering API when output settings must be controlled in application code.

HTML Web Applications