渲染选项 – 自定义 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。
| Property | Description |
|---|---|
| jpeg_quality | Specifies the quality of JPEG compression for images. The default value is 95. |
| document_info | This property contains information about the output PDF document. |
| encryption | This property gets or sets encryption details. If it is not set, then no encryption will be performed. |
| form_field_behaviour | This property specifies the behavior of form fields in the output PDF document. |
| is_tagged_pdf | If 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 PDF with password protection using 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# Setup input and output directories
10data_dir = "data/"
11output_dir = "output/"
12os.makedirs(output_dir, exist_ok=True)
13
14# Prepare path to the source HTML file
15document_path = os.path.join(data_dir, "document.html")
16
17# Initialize an HTML document from the file
18doc = ah.HTMLDocument(document_path)
19
20# Create an instance of the HTML Renderer
21renderer = rn.HtmlRenderer()
22
23# Prepare path to save the converted PDF
24save_path = os.path.join(output_dir, "convert-html-to-pdf-with-encryption.pdf")
25
26# Create PDF rendering options and set password protection
27options = rp.PdfRenderingOptions()
28options.encryption = rpe.PdfEncryptionInfo(
29 user_password="user_pwd",
30 owner_password="owner_pwd",
31 permissions=rpe.PdfPermissions.PRINT_DOCUMENT,
32 encryption_algorithm=rpe.PdfEncryptionAlgorithm.RC4_128
33)
34
35# Create the PDF device with options and output path
36device = rp.PdfDevice(options, save_path)
37
38# Render HTML to PDF
39renderer.render(device, doc)上例展示了如何创建一个新的 PdfRenderingOptions 实例,并为 PDF 输出文件设置加密选项。为此,你需要创建一个 PdfEncryptionInfo对象,它定义了 PDF 文件的加密设置。构造函数需要四个参数:
user_password和owner_password,这是打开和处理 PDF 文件所必需的;permissions– PDF 文件允许的权限集。在本例中,指定了 “PdfPermissions.PRINT_DOCUMENT”,使用户可以打印文档;encryption_algorithm– 用于保护 PDF 文件的加密算法。在本例中,使用的是PdfEncryptionAlgorithm.RC4_128128 位 RC4 加密算法。
ImageRenderingOptions
ImageRenderingOptions 类支持所有常规选项,并允许您配置特定选项,例如抗锯齿、文本渲染配置、图像格式选择和图像压缩。
| Property | Description |
|---|---|
| compression | Sets Tagged Image File Format (TIFF) compression. By default, this property is LZW. |
| format | Sets the
ImageFormat (JPG, PNG, BMP, TIFF, or GIF). By default, this property is PNG. |
| use_antialiasing | This property defines the rendering quality of the image by controlling the use of antialiasing. Antialiasing is enabled by default. |
| text | Gets a TextOptions object which is used for configuration of text rendering. |
让我们来看看如何使用专门的 ImageRenderingOptions 对象来设置图像的 TIFF 格式和压缩率:
1# Render HTML to TIFF with custom settings using Python
2
3import os
4import aspose.html as ah
5import aspose.html.rendering.image as ri
6
7# Setup input and output directories
8data_dir = "data/"
9output_dir = "output/"
10os.makedirs(output_dir, exist_ok=True)
11
12# Prepare paths for input HTML and output TIFF file
13document_path = os.path.join(data_dir, "html_file.html")
14save_path = os.path.join(output_dir, "document.tiff")
15
16# Initialize an HTML document from the file
17doc = ah.HTMLDocument(document_path)
18
19# Create rendering options for image format (TIFF in this case)
20image_options = ri.ImageRenderingOptions(ri.ImageFormat.TIFF)
21image_options.compression = ri.Compression.NONE
22
23# Create an ImageDevice and specify options and output file
24device = ri.ImageDevice(image_options, save_path)
25
26# Render HTML to TIFF
27doc.render_to(device)XpsRenderingOptions
XpsRenderingOptions继承了基本 RenderingOptions类的所有设置。在生成 XPS 文件时,你可以配置页面大小、页边距、背景颜色和其他常用选项。
1# Render HTML to XPS with custom page settings using Python
2
3import os
4import aspose.html as ah
5import aspose.html.rendering.xps as rx
6import aspose.html.drawing as dr
7
8# Setup input and output directories
9data_dir = "data/"
10output_dir = "output/"
11os.makedirs(output_dir, exist_ok=True)
12
13# Prepare a path to the source HTML file
14document_path = os.path.join(data_dir, "document.html")
15
16# Initialize the HTML document from the file
17doc = ah.HTMLDocument(document_path)
18
19# Create an instance of XPS Rendering Options
20options = rx.XpsRenderingOptions()
21
22# Set custom page size (5 x 2 inches)
23options.page_setup.any_page = dr.Page(dr.Size(dr.Length.from_inches(5.0), dr.Length.from_inches(2.0)))
24
25# Prepare a path to save the converted file
26save_path = os.path.join(output_dir, "document-options.xps")
27
28# Create an XpsDevice with the specified options and output file
29device = rx.XpsDevice(options, save_path)
30
31# Render HTML to XPS
32doc.render_to(device)DocRenderingOptions
DocRenderingOptions 类支持所有常规选项,并允许您自定义输出文件的 font_embedding_rule 和 document_format 属性。
| Property | Description |
|---|---|
| font_embedding_rule | This property gets or sets the font embedding rule. Available values are Full and None. The default value is None. |
| document_format | This property gets or sets the file format of the output document. The default value is DOCX. |
下面的示例展示了如何通过设置页面大小和字体嵌入规则,自定义输出 DOCX 文档的渲染选项:
1# Render HTML to DOCX with custom settings using Python
2
3import os
4import aspose.html as ah
5import aspose.html.rendering.doc as rd
6import aspose.html.drawing as dr
7
8# Setup input and output directories
9data_dir = "data/"
10output_dir = "output/"
11os.makedirs(output_dir, exist_ok=True)
12
13# Prepare a path to the source HTML file
14document_path = os.path.join(data_dir, "nature.html")
15
16# Initialize the HTML document from the file
17doc = ah.HTMLDocument(document_path)
18
19# Create an instance of DocRenderingOptions and set a custom page size
20options = rd.DocRenderingOptions()
21options.page_setup.any_page = dr.Page(dr.Size(dr.Length.from_inches(8.0), dr.Length.from_inches(10.0)))
22options.font_embedding_rule = rd.FontEmbeddingRule.FULL
23
24# Prepare a path to save the converted file
25save_path = os.path.join(output_dir, "nature-options.docx")
26
27# Create a DocDevice with the specified options and output file
28device = rd.DocDevice(options, save_path)
29
30# Render HTML to DOCX
31doc.render_to(device)Aspose.HTML 提供免费的在线 转换器,可将 HTML、XHTML、MHTML、EPUB、XML 和 Markdown 文件转换为各种流行格式。你可以轻松地将 HTML 文档转换为 PDF、XPS、DOCX、JPG、PNG、GIF、TIFF 等格式。只需选择文件,选择要转换的格式,就大功告成了。
