渲染设备 – 将 HTML 渲染为 PDF、XPS、DOCX 和图像

本文将介绍如何使用渲染设备 PdfDeviceXpsDeviceDocDeviceImageDevice 将 HTML 渲染为 PDF、XPS、DOCX 和图像。在 Python 示例中,渲染过程使用的是默认渲染选项。要了解有关自定义输出的更多信息,请阅读 Rendering Options 一文。

了解 Aspose.HTML 中的渲染设备 – Rendering Devices

在 Aspose.HTML for Python via .NET 中,渲染设备代表一个 2D 绘图表面,通过 IDevice 接口实现。其主要目的是将 HTML 内容和支持的网络格式(MHTML、SVG 和 EPUB)转换为不同的输出格式。

Python API 提供以下渲染设备:

每个设备都有特定的渲染选项: PdfRenderingOptionsXpsRenderingOptionsDocRenderingOptionsImageRenderingOptions。这些选项允许对输出质量、布局和格式进行精确控制。

PdfDevice

Aspose.HTML for Python via .NET 库中的 Aspose.Html.Rendering.Pdf命名空间提供了将 HTML 和相关格式直接渲染为 PDF 文档的功能。它包括专为 PDF 输出定制的类和参数。主要组件:

下一个 Python 示例展示了如何使用 PdfDevice 将 HTML 渲染成 PDF。该过程使用默认的渲染选项:

 1# Render HTML to Pdf using Python
 2
 3import os
 4import aspose.html as ah
 5import aspose.html.rendering.pdf as rp
 6
 7# Prepare output directory and path for the PDF file
 8output_dir = "output/"
 9os.makedirs(output_dir, exist_ok=True)
10save_path = os.path.join(output_dir, "document.pdf")
11
12# Prepare HTML code
13code = "<span>Hello, World!!</span>"
14
15# Initialize an HTML document from the HTML code
16doc = ah.HTMLDocument(code, ".")
17
18# Create a PDF Device and specify the output file to render
19device = rp.PdfDevice(save_path)
20
21# Render HTML to PDF
22doc.render_to(device)

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

图像设备

Aspose.Html.Rendering.Image 命名空间提供了一个特定的 ImageDevice类以及 ImageRenderingOptions 类,负责将 HTML 文件渲染为光栅格式:JPG、PNG、BMP、GIF 和 TIFF。

本示例演示了如何使用 Aspose.HTML for Python 通过 .NET 将 HTML 文件转换为 JPEG 图像。代码从磁盘加载 HTML 文档,为 JPEG 格式配置 ImageRenderingOptions,然后使用 ImageDevice 将文档渲染为输出图像文件。

 1# Render HTML to JPG 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 JPG file
13document_path = os.path.join(data_dir, "drawing.html")
14save_path = os.path.join(output_dir, "drawing-output.jpg")
15
16# Initialize an HTML document from the file
17doc = ah.HTMLDocument(document_path)
18
19# Create rendering options for image format (JPEG in this case)
20image_options = ri.ImageRenderingOptions(ri.ImageFormat.JPEG)
21
22# Create an ImageDevice and specify options and output file
23device = ri.ImageDevice(image_options, save_path)
24
25# Render HTML to JPG
26doc.render_to(device)

XpsDevice

Aspose.Html.Rendering.Xps 名称空间提供了 XpsDevice 类,用于将 HTML 内容(或 MHTML、SVG、EPUB 等)渲染为 XPS 文档。下面的 Python 示例使用默认渲染选项演示了这一过程:

 1# Render HTML to XPS using Python
 2
 3import os
 4import aspose.html as ah
 5import aspose.html.rendering.xps as rx
 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 XPS file
13document_path = os.path.join(data_dir, "drawing.html")
14save_path = os.path.join(output_dir, "drawing-output.xps")
15
16# Initialize an HTML document from the file
17doc = ah.HTMLDocument(document_path)
18
19# Create rendering options for XPS format
20xps_options = rx.XpsRenderingOptions()
21
22# Create an XpsDevice and specify options and output file
23device = rx.XpsDevice(xps_options, save_path)
24
25# Render HTML to XPS
26doc.render_to(device)

DocDevice

Aspose.Html.Rendering.Doc 名称空间包含 DocDevice 类,该类可将 HTML、MHTML、SVG 和 EPUB 文件渲染为 DOCX 格式。下面的 Python 示例使用默认呈现选项演示了这一过程:

 1# Render HTML to DOCX using Python
 2
 3import os
 4import aspose.html as ah
 5import aspose.html.rendering.doc as rd
 6
 7# Prepare output directory and path for the DOCX file
 8output_dir = "output/"
 9os.makedirs(output_dir, exist_ok=True)
10save_path = os.path.join(output_dir, "document.doc")
11
12# Initialize an HTML document from URL
13doc = ah.HTMLDocument("https://docs.aspose.com/html/files/document.html")
14
15# Create a DocDevice and specify the output file to render
16device = rd.DocDevice(save_path)
17
18# Render HTML to DOCX
19doc.render_to(device)

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.