Choose a Rendering Device in Python

Choose a rendering device by the required output format, not by the source format. Use PdfDevice for PDF, ImageDevice for raster images, DocDevice for DOCX, and XpsDevice for XPS. The source is supplied through document.render_to() or a matching source-specific renderer.

A rendering device implements the IDevice interface and receives drawing operations produced while a document is rendered. The device writes those operations to a particular output format.

Choose a Rendering Device

Source, renderer, device, and rendering options have separate roles:

ComponentSelect it byExamples
SourceInput contentHTMLDocument, SVGDocument, MHTML stream, EPUB stream
RendererSource type and rendering workflowHtmlRenderer, SvgRenderer, MhtmlRenderer, EpubRenderer
DeviceOutput formatPdfDevice, ImageDevice, DocDevice, XpsDevice
Rendering optionsLayout and output-specific settingsPdfRenderingOptions, ImageRenderingOptions, DocRenderingOptions, XpsRenderingOptions

For one loaded HTML or SVG document, call document.render_to(device). When a source-specific renderer is required, pass the same device to renderer.render(). The device does not parse the source; it receives the rendered result and writes the output.

The two common paths are:

OutputDevice and optionsChoose it whenMost relevant controls
PDFPdfDevice with PdfRenderingOptionsThe result must be a paginated PDF document.PDF metadata, JPEG compression quality, encryption, form-field behavior, and tagged-PDF output.
Raster imageImageDevice with ImageRenderingOptionsThe rendered pages are needed as PNG, JPEG, BMP, GIF, or TIFF images.Raster format, TIFF compression, graphics antialiasing, text hinting, and output resolution.
DOCXDocDevice with DocRenderingOptionsThe result must be opened or edited as a Word DOCX document.Font embedding, page layout, background, and CSS media settings.
XPSXpsDevice with XpsRenderingOptionsThe result must be a fixed-layout XPS document.Common page setup, background, CSS media, and resolution settings.

Render HTML to PDF with PdfDevice

The following example loads document.html, creates a PdfDevice, and sends the document directly to the device. It demonstrates the same three-step workflow used with the other output devices.

To render a loaded document to a 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() and pass the device.
 1import os
 2import aspose.html as ah
 3import aspose.html.rendering.pdf as rp
 4
 5data_dir = "data"
 6output_dir = "output"
 7os.makedirs(output_dir, exist_ok=True)
 8
 9input_path = os.path.join(data_dir, "document.html")
10output_path = os.path.join(output_dir, "document.pdf")
11
12with ah.HTMLDocument(input_path) as document:
13    with rp.PdfDevice(output_path) as device:
14        document.render_to(device)

The result is document.pdf created with the default PDF rendering options. To customize a device, create its matching options object and pass the object and output destination to the device constructor. Common rendering settings include page setup, background color, CSS media, and resolution; the last column of the table highlights the controls most relevant to each output.

Do not pass save options such as PdfSaveOptions to PdfDevice. Save options belong to the high-level Converter workflow. Devices use rendering options such as PdfRenderingOptions. See Rendering Options for tested examples of page layout, PDF properties, TIFF output, and DOCX font embedding.

Common Device Selection Issues

IssueExplanation and fix
The source type and device appear mismatchedThe source and device are selected independently. Match the document or renderer to the input, then choose the device by output format.
A save-options object is rejected by a deviceUse the corresponding rendering-options class, such as PdfRenderingOptions with PdfDevice.
DocDevice output is saved with a .doc extensionDocDevice produces DOCX. Use the .docx extension.
The image format is not the one expectedSelect the required format in ImageRenderingOptions before creating ImageDevice.
Multiple sources must be written to one outputUse the appropriate source-specific renderer and pass the collection of sources to renderer.render().

Related Rendering Guides

Other Platforms