HTML Rendering Options in Python

Use a format-specific rendering-options class with the matching output device. General settings control page layout, background color, CSS media, and resolution; format-specific settings control PDF properties, raster-image output, or DOCX font embedding.

Aspose.HTML for Python via .NET provides PdfRenderingOptions, ImageRenderingOptions, DocRenderingOptions, and XpsRenderingOptions. Each class inherits common settings from RenderingOptions and adds properties for its output format.

Rendering Options vs. Save Options

Rendering options and save options configure different API workflows:

API workflowOptionsTypical call
High-level conversionPdfSaveOptions, ImageSaveOptions, DocSaveOptions, or XpsSaveOptionsConverter.convert_html(...)
Rendering pipelinePdfRenderingOptions, ImageRenderingOptions, DocRenderingOptions, or XpsRenderingOptionsdocument.render_to(device) or renderer.render(device, source)

Do not pass save options to a rendering device. For ordinary format conversion, the high-level Converter API is usually the shorter workflow. Use rendering options when the application works directly with a rendering device.

General Rendering Options

The base RenderingOptions class supplies settings shared by PDF, image, DOCX, and XPS output.

SettingWhat it controls
page_setupOutput page size, margins, first/left/right page rules, and content-based page layout.
background_colorColor painted behind rendered content where the document does not supply its own background.
css.media_typeWhether @media screen or @media print rules are applied during rendering.
horizontal_resolution, vertical_resolutionOutput resolution. For raster output, DPI affects the pixel dimensions of pages defined in physical units. It does not restore detail missing from source bitmaps or sharpen vector text in PDF, XPS, or DOCX.

The following example configures a page size, margins, background color, print CSS, and automatic width adjustment before rendering HTML to PDF:

  1. Load the HTML document.
  2. Create the rendering-options object that matches the output device.
  3. Configure the required page and CSS settings.
  4. Create the output device with those options.
  5. Render the document to the device.
 1import os
 2import aspose.html as ah
 3import aspose.html.drawing as dr
 4import aspose.html.rendering as rn
 5import aspose.html.rendering.pdf as rp
 6import aspose.pydrawing as pd
 7
 8data_dir = "data"
 9output_dir = "output"
10os.makedirs(output_dir, exist_ok=True)
11
12input_path = os.path.join(data_dir, "document.html")
13output_path = os.path.join(output_dir, "document-layout.pdf")
14
15with ah.HTMLDocument(input_path) as document:
16    options = rp.PdfRenderingOptions()
17    options.page_setup.any_page = dr.Page(
18        dr.Size(800, 600),
19        dr.Margin(30, 30, 30, 30),
20    )
21    options.page_setup.adjust_to_widest_page = True
22    options.background_color = pd.Color.alice_blue
23    options.css.media_type = rn.MediaType.PRINT
24
25    with rp.PdfDevice(options, output_path) as device:
26        document.render_to(device)

The numeric page and margin values are pixels. Use Length.from_inches(), Length.from_millimeters(), or another explicit unit when physical dimensions are required. adjust_to_widest_page can expand the configured page width to fit the widest content, which helps prevent horizontal clipping but may produce a nonstandard page width.

PDF Rendering Options

PDF rendering options add document information, JPEG compression quality, form-field handling, encryption, and tagged-PDF output.

PropertyPurpose
document_infoSets PDF title, author, subject, keywords, and other document information.
jpeg_qualityControls JPEG compression quality when raster content is encoded as JPEG. It does not affect vector text or shapes.
form_field_behaviourKeeps generated PDF form fields interactive or flattens them into page content.
encryptionAccepts PdfEncryptionInfo with user and owner passwords, permissions, and an encryption algorithm. If it is not set, the PDF is not encrypted.
is_tagged_pdfAdds a tag structure to the generated PDF when set to True.

This concise configuration adds PDF metadata and flattens form fields. Use INTERACTIVE instead when users must complete the form after conversion:

1options = rp.PdfRenderingOptions()
2options.document_info.title = "Quarterly Report"
3options.document_info.author = "Example Company"
4options.document_info.subject = "Financial summary"
5options.form_field_behaviour = rp.FormFieldBehaviour.FLATTENED

The user password controls opening the encrypted PDF. The owner password and PdfPermissions control permitted operations. Choose an encryption algorithm that meets the security requirements of the application; do not treat legacy RC4 encryption as suitable for sensitive documents.

Image Rendering Options

Image rendering options select the raster format and control TIFF compression, graphics antialiasing, and text hinting.

PropertyPurpose
formatSelects PNG, JPEG, BMP, GIF, or TIFF. The default is PNG.
compressionSelects TIFF compression. The default is LZW; the property does not control JPEG quality.
use_antialiasingSmooths rasterized edges. It is enabled by default.
textProvides text-rendering settings such as use_hinting.

The next example renders HTML to TIFF with lossless LZW compression, 150 dpi output resolution, antialiasing, and text hinting:

 1import os
 2import aspose.html as ah
 3import aspose.html.drawing as dr
 4import aspose.html.rendering.image as ri
 5
 6data_dir = "data"
 7output_dir = "output"
 8os.makedirs(output_dir, exist_ok=True)
 9
10input_path = os.path.join(data_dir, "document.html")
11output_path = os.path.join(output_dir, "document.tiff")
12
13with ah.HTMLDocument(input_path) as document:
14    options = ri.ImageRenderingOptions(ri.ImageFormat.TIFF)
15    options.compression = ri.Compression.LZW
16    options.horizontal_resolution = dr.Resolution.from_dots_per_inch(150.0)
17    options.vertical_resolution = dr.Resolution.from_dots_per_inch(150.0)
18    options.use_antialiasing = True
19    options.text.use_hinting = True
20
21    with ri.ImageDevice(options, output_path) as device:
22        document.render_to(device)

Higher DPI can produce more pixels and a larger raster file when the page uses physical units. It cannot add detail to a low-resolution source image. Antialiasing smooths edges, while text hinting adjusts glyph placement for raster output; neither setting changes the selected page dimensions.

DOCX Rendering Options

DocRenderingOptions adds font_embedding_rule and document_format. DOCX is the supported document format, and font embedding is disabled by default.

The following example embeds the fonts used by the document to make the DOCX output more portable:

 1import os
 2import aspose.html as ah
 3import aspose.html.rendering.doc as rd
 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.docx")
11
12with ah.HTMLDocument(input_path) as document:
13    options = rd.DocRenderingOptions()
14    options.font_embedding_rule = rd.FontEmbeddingRule.FULL
15
16    with rd.DocDevice(options, output_path) as device:
17        document.render_to(device)

FULL can increase the output size, and a font’s license may restrict embedding. When fonts are not embedded, they must be installed on the system where the DOCX file is displayed to preserve the intended appearance.

XPS Rendering Options

XpsRenderingOptions uses the general page, background, CSS media, and resolution settings described above. It does not add format-specific properties comparable to PDF encryption, TIFF compression, or DOCX font embedding. Create XpsRenderingOptions, configure only the common settings required by the result, and pass it to XpsDevice.

Common Rendering Issues

ProblemLikely cause and fix
Content is clipped horizontallyThe page is narrower than the rendered content. Increase the page width, reduce margins, or enable adjust_to_widest_page when a variable page width is acceptable.
Fonts are missing or substitutedInstall the required fonts in the rendering environment. For DOCX, consider FontEmbeddingRule.FULL when the font license allows embedding.
Output uses the wrong CSS layoutSet options.css.media_type to MediaType.PRINT or MediaType.SCREEN to match the intended stylesheet rules.
Raster output looks blurredIncrease output resolution when more output pixels are needed, keep antialiasing enabled, and provide source images with sufficient pixel dimensions. DPI cannot recover detail absent from the source.

Related 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