Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Aspose.html.rendering 名称空间由许多呈现器对象和相应的底层选项类组成,它们负责将文档呈现到 IDevice 实现中。Aspose.HTML for Python 通过 .NET API 提供了以下呈现器实现: HtmlRenderer、 SvgRenderer、 MhtmlRenderer 和 EpubRenderer,它们分别用于呈现 HTML、SVG、MHTML 和 EPUB 文档。
本文介绍了使用
aspose.html.rendering 名称空间中的 render() 方法将基于 HTML 的文件转换为其他流行格式的支持场景。
HtmlRenderer 类可用于将 HTML 文件渲染为各种流行格式,允许自定义渲染选项并控制过程的输出。让我们来看看如何使用 HtmlRenderer 类将 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,您可以将 SVG 文件渲染为其他文件格式,如 PDF、XPS、DOCX 和图像格式。下面的示例演示了如何使用 SvgRenderer 类将 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 类将 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 类,您可以将 EPUB 文件转换为其他格式,如 PDF、XPS、DOCX 和图像。下面的 Python 示例展示了如何通过自定义页面大小设置将 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 等格式。只需选择文件,选择要转换的格式,就大功告成了。
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.