渲染选项 – 自定义 HTML 渲染

渲染选项提供了对渲染设备如何处理内容的详细控制。每个设备– PdfDevice、 XpsDevice、 DocDevice 和 ImageDevice –都有自己的选项类别: PdfRenderingOptions、 XpsRenderingOptions、 DocRenderingOptions 和 ImageRenderingOptions。通过这些选项,你可以调整页面大小、页边距和颜色,还可以应用特定于设备的设置,例如为 PDF 文件添加安全密码。无论是通过调整图像质量和分辨率来减小文件大小,还是通过优化页面布局和文本格式来增强可读性,自定义对于实现预期效果都至关重要。

本文将探讨 Aspose.HTML for Python via .NET 中的渲染选项,展示如何将 HTML 内容自定义转换为 PDF、XPS、DOCX 和图像等输出格式。这些选项包括页面大小、背景颜色和分辨率等常规设置,以及图像压缩、PDF 安全性和字体嵌入等特定于设备的设置,从而实现对最终输出的完全控制。

在这里,您可以找到演示如何使用设备特定参数的 Python 代码示例。一般选项的使用将在 “如何撰写文章"一章(编写中)中介绍。

要了解有关渲染过程的更多信息,请阅读 渲染设备 一文。

PdfRenderingOptions

除了常规选项外, PdfRenderingOptions类还支持几个特定参数,如 jpeg_quality、document_info、encryption、form_field_behaviour 和 is_tagged_pdf。

PropertyDescription
jpeg_qualitySpecifies the quality of JPEG compression for images. The default value is 95.
document_infoThis property contains information about the output PDF document.
encryptionThis property gets or sets encryption details. If it is not set, then no encryption will be performed.
form_field_behaviourThis property specifies the behavior of form fields in the output PDF document.
is_tagged_pdfIf is_tagged_pdf = true, a tag structure will be added to the PDF document during rendering.

form_field_behaviour “属性用于指定 PDF 文档中表单域的行为。要了解 PDF 文件扁平化的含义以及如何使用 Aspose.HTML for Python via .NET 库进行扁平化,请参阅 HTML 到 PDF 的转换和 PDF 扁平化 章节。

下面的 Python 代码演示了如何使用 PdfRenderingOptions 类为 PDF 输出文件添加加密:

 1# Render HTML to password-protected PDF in 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.rendering.pdf.encryption as rpe
 8
 9# Prepare input and output paths
10data_dir = "data"
11output_dir = "output"
12os.makedirs(output_dir, exist_ok=True)
13
14document_path = os.path.join(data_dir, "document.html")
15save_path = os.path.join(output_dir, "convert-html-to-pdf-with-encryption.pdf")
16
17# Load the HTML document
18with ah.HTMLDocument(document_path) as document:
19    with rn.HtmlRenderer() as renderer:
20        # Configure password-protected PDF output
21        options = rp.PdfRenderingOptions()
22        options.encryption = rpe.PdfEncryptionInfo(
23            user_password="user_pwd",
24            owner_password="owner_pwd",
25            permissions=rpe.PdfPermissions.PRINT_DOCUMENT,
26            encryption_algorithm=rpe.PdfEncryptionAlgorithm.RC4_128
27        )
28
29        # Render HTML to PDF
30        with rp.PdfDevice(options, save_path) as device:
31            renderer.render(device, document)

上例展示了如何创建一个新的 PdfRenderingOptions 实例,并为 PDF 输出文件设置加密选项。为此,你需要创建一个 PdfEncryptionInfo对象,它定义了 PDF 文件的加密设置。构造函数需要四个参数:

ImageRenderingOptions

ImageRenderingOptions 类支持所有常规选项,并允许您配置特定选项,例如抗锯齿、文本渲染配置、图像格式选择和图像压缩。

PropertyDescription
compressionSets Tagged Image File Format (TIFF) compression. By default, this property is LZW.
formatSets the ImageFormat (JPG, PNG, BMP, TIFF, or GIF). By default, this property is PNG.
use_antialiasingThis property defines the rendering quality of the image by controlling the use of antialiasing. Antialiasing is enabled by default.
textGets a TextOptions object which is used for configuration of text rendering.

让我们来看看如何使用专门的 ImageRenderingOptions 对象来设置图像的 TIFF 格式和压缩率:

 1# Render HTML to TIFF in Python with custom settings
 2
 3import os
 4import aspose.html as ah
 5import aspose.html.rendering.image as ri
 6
 7# Prepare input and output paths
 8data_dir = "data"
 9output_dir = "output"
10os.makedirs(output_dir, exist_ok=True)
11
12document_path = os.path.join(data_dir, "html_file.html")
13save_path = os.path.join(output_dir, "document.tiff")
14
15# Load the HTML document
16with ah.HTMLDocument(document_path) as document:
17    # Configure TIFF output
18    options = ri.ImageRenderingOptions(ri.ImageFormat.TIFF)
19    options.compression = ri.Compression.NONE
20
21    # Render HTML to TIFF
22    with ri.ImageDevice(options, save_path) as device:
23        document.render_to(device)

XpsRenderingOptions

XpsRenderingOptions继承了基本 RenderingOptions类的所有设置。在生成 XPS 文件时,你可以配置页面大小、页边距、背景颜色和其他常用选项。

 1# Render HTML to XPS in Python with custom page settings
 2
 3import os
 4import aspose.html as ah
 5import aspose.html.rendering.xps as rx
 6import aspose.html.drawing as dr
 7
 8# Prepare input and output paths
 9data_dir = "data"
10output_dir = "output"
11os.makedirs(output_dir, exist_ok=True)
12
13document_path = os.path.join(data_dir, "document.html")
14save_path = os.path.join(output_dir, "document-options.xps")
15
16# Load the HTML document
17with ah.HTMLDocument(document_path) as document:
18    # Configure XPS output
19    options = rx.XpsRenderingOptions()
20    options.page_setup.any_page = dr.Page(
21        dr.Size(dr.Length.from_inches(5.0), dr.Length.from_inches(2.0))
22    )
23
24    # Render HTML to XPS
25    with rx.XpsDevice(options, save_path) as device:
26        document.render_to(device)

DocRenderingOptions

DocRenderingOptions 类支持所有常规选项,并允许您自定义输出文件的 font_embedding_rule 和 document_format 属性。

PropertyDescription
font_embedding_ruleThis property gets or sets the font embedding rule. Available values are Full and None. The default value is None.
document_formatThis property gets or sets the file format of the output document. The default value is DOCX.

下面的示例展示了如何通过设置页面大小和字体嵌入规则,自定义输出 DOCX 文档的渲染选项:

 1# Render HTML to DOCX in Python with custom settings
 2
 3import os
 4import aspose.html as ah
 5import aspose.html.rendering.doc as rd
 6import aspose.html.drawing as dr
 7
 8# Prepare input and output paths
 9data_dir = "data"
10output_dir = "output"
11os.makedirs(output_dir, exist_ok=True)
12
13document_path = os.path.join(data_dir, "nature.html")
14save_path = os.path.join(output_dir, "nature-options.docx")
15
16# Load the HTML document
17with ah.HTMLDocument(document_path) as document:
18    # Configure DOCX output
19    options = rd.DocRenderingOptions()
20    options.page_setup.any_page = dr.Page(
21        dr.Size(dr.Length.from_inches(8.0), dr.Length.from_inches(10.0))
22    )
23    options.font_embedding_rule = rd.FontEmbeddingRule.FULL
24
25    # Render HTML to DOCX
26    with rd.DocDevice(options, save_path) as device:
27        document.render_to(device)

Aspose.HTML 提供免费的在线 转换器,可将 HTML、XHTML、MHTML、EPUB、XML 和 Markdown 文件转换为各种流行格式。你可以轻松地将 HTML 文档转换为 PDF、XPS、DOCX、JPG、PNG、GIF、TIFF 等格式。只需选择文件,选择要转换的格式,就大功告成了。

文本 “HTML 网络应用程序”