渲染器 – 用 Python 渲染 HTML、MHTML、EPUB 和 SVG

渲染器

Aspose.html.rendering 名称空间由许多呈现器对象和相应的底层选项类组成,它们负责将文档呈现到 IDevice 实现中。Aspose.HTML for Python 通过 .NET API 提供了以下呈现器实现: HtmlRendererSvgRendererMhtmlRendererEpubRenderer,它们分别用于呈现 HTML、SVG、MHTML 和 EPUB 文档。

本文介绍了使用 aspose.html.rendering 名称空间中的 render() 方法将基于 HTML 的文件转换为其他流行格式的支持场景。

HtmlRenderer

HtmlRenderer 类可用于将 HTML 文件渲染为各种流行格式,允许自定义渲染选项并控制过程的输出。让我们来看看如何使用 HtmlRenderer 类将 HTML 渲染成 PDF,并自定义输出设置:

  1. 使用 HTMLDocument 类初始化 HTML 文档。
  2. 创建一个新的 HtmlRenderer 对象。
  3. 创建 PdfRenderingOptions的实例,并自定义输出 PDF 文档的选项。例如,你可以设置 PageSetup 类的 any_page属性,创建一个尺寸为 600 像素乘 200 像素的新页面对象。此外,您还可以配置 encryption 属性,为输出 PDF 文件指定用户和所有者密码、允许的操作和加密算法。
  4. 使用 PdfDevice(options, savePath) 构造函数创建一个 PdfDevice 类对象。
  5. 调用 render(device, document) 方法,使用指定的呈现选项将 HTML 呈现为 PDF。
 1# Render HTML to PDF with custom page settings 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.drawing as dr
 8import aspose.html.rendering.pdf.encryption as rpe
 9
10# Setup input and output directories
11data_dir = "data/"
12output_dir = "output/"
13os.makedirs(output_dir, exist_ok=True)
14
15# Prepare path to the source HTML file
16document_path = os.path.join(data_dir, "document.html")
17
18# Initialize an HTML document from the file
19doc = ah.HTMLDocument(document_path)
20
21# Create an instance of the HTML Renderer
22renderer = rn.HtmlRenderer()
23
24# Prepare path to save the converted PDF
25save_path = os.path.join(output_dir, "render-html-with-options.pdf")
26
27# Create PDF rendering options and set custom page size
28options = rp.PdfRenderingOptions()
29options.page_setup.any_page = dr.Page(dr.Size(600, 200))
30
31# Setup PDF encryption
32options.encryption = rpe.PdfEncryptionInfo(
33    user_password="user_pwd",
34    owner_password="owner_pwd",
35    permissions=rpe.PdfPermissions.PRINT_DOCUMENT,
36    encryption_algorithm=rpe.PdfEncryptionAlgorithm.RC4_128
37)
38
39# Create the PDF device with options and output path
40device = rp.PdfDevice(options, save_path)
41
42# Render HTML to PDF
43renderer.render(device, doc)

渲染选项可让你对渲染过程进行额外控制。如需了解更多信息,请阅读 渲染选项 一文。

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

SvgRenderer

通过 SvgRenderer,您可以将 SVG 文件渲染为其他文件格式,如 PDF、XPS、DOCX 和图像格式。下面的示例演示了如何使用 SvgRenderer 类将 SVG 渲染成指定了自定义页面大小的 PNG:

  1. 初始化 SVG 文档。使用 SVGDocument() 构造函数之一创建 SVGDocument 实例。
  2. 创建一个新的 SvgRenderer 对象。
  3. 创建 ImageRenderingOptions 实例,并为输出的 PNG 图像设置自定义选项。在示例中,我们指定了 any_page 属性,该属性设置了一个页面大小为 400x300 像素的新页面对象。
  4. 使用 ImageDevice(options, savePath) 构造函数创建 ImageDevice 类对象。
  5. 调用 render(device, document) 方法,使用指定的呈现选项将 SVG 呈现为 PNG。
 1# Render SVG to PNG with custom page settings using Python
 2
 3import os
 4import aspose.html as ah
 5import aspose.html.rendering.image as ri
 6import aspose.html.rendering as rn
 7import aspose.html.drawing as dr
 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 SVG file
15doc_path = os.path.join(data_dir, "flower.svg")
16save_path = os.path.join(output_dir, "render-svg-with-options.png")
17
18# Initialize an SVG document from the file
19doc = ah.dom.svg.SVGDocument(doc_path)
20
21# Create an instance of the SVG Renderer
22renderer = rn.SvgRenderer()
23
24# Create the instance of Rendering Options and set a custom page-size
25options = ri.ImageRenderingOptions()
26options.page_setup.any_page = dr.Page(dr.Size(400, 300))
27
28# Create the PNG device with options and output path
29device = ri.ImageDevice(options, save_path)
30
31# Render SVG to PNG
32renderer.render(device, doc)

MhtmlRenderer

本例说明了如何使用带有指定呈现选项的 MhtmlRenderer 类将 MHTML 文档转换为 PDF:

  1. 打开现有的 MHTML 文档。
  2. 使用 MhtmlRenderer() 构造函数创建一个 MhtmlRenderer 类实例。
  3. 初始化 PdfRenderingOptions 类并设置渲染选项。
  4. 创建 PdfDevice 类的实例。
  5. 调用 render(device, stream) 方法将 MHTML 转换为 PDF。
 1# Render MHTML to PDF with custom page settings using Python
 2
 3import os
 4import aspose.html.rendering.pdf as rp
 5import aspose.html.rendering as rn
 6import aspose.html.drawing as dr
 7import aspose.pydrawing as pd
 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 MHTML file
15epub_path = os.path.join(data_dir, "document.mht")
16save_path = os.path.join(output_dir, "render-mhtml-with-options.pdf")
17
18# Open the MHTML file in binary mode
19with open("document.mht", 'rb') as stream:
20
21    # Create an instance of MHTML Renderer
22    renderer = rn.MhtmlRenderer()
23
24    # Create PDF rendering options and set a custom page size and background color
25    options = rp.PdfRenderingOptions()
26    options.page_setup.any_page = dr.Page(dr.Size(800, 400))
27    options.background_color = pd.Color.bisque
28
29    # Create an instance of PdfDevice for output
30    device = rp.PdfDevice(options, save_path)
31
32    # Render MHTML to PDF
33    renderer.render(device, stream)

EpubRenderer

通过 EpubRenderer 类,您可以将 EPUB 文件转换为其他格式,如 PDF、XPS、DOCX 和图像。下面的 Python 示例展示了如何通过自定义页面大小设置将 EPUB 转换为 DOCX:

  1. 打开现有 EPUB 文件
  2. 创建 EpubRenderer 类的实例。
  3. 初始化 DocRenderingOptions 类并设置渲染选项。
  4. 创建 DocDevice 类的实例。
  5. 调用 render(device, stream) 方法将 EPUB 渲染为 DOCX。
 1# Render EPUB to DOCX with custom page settings using Python
 2
 3import os
 4import aspose.html.rendering.doc as rd
 5import aspose.html.rendering as rn
 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 path to the source EPUB file
14epub_path = os.path.join(data_dir, "input.epub")
15save_path = os.path.join(output_dir, "render-epub-with-options.docx")
16
17# Open the EPUB file in binary mode
18with open(epub_path, "rb") as stream:
19
20    # Create an instance of EPUB Renderer
21    renderer = rn.EpubRenderer()
22
23    # Create DOC rendering options and set a custom page size
24    options = rd.DocRenderingOptions()
25    options.page_setup.any_page = dr.Page(dr.Size(800, 400))
26
27    # Create an instance of DocDevice for output
28    device = rd.DocDevice(options, save_path)
29
30    # Render EPUB to DOCX
31    renderer.render(device, stream)

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

文本 “免费在线转换器”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.