借助Aspose.HTML控件,在 Python 中将 SVG 转换为 PDF
您可能会发现许多解决方案都提供以编程方式将SVG转换为PDF 的功能。但这篇博文将介绍一个功能强大的 SDK,供 Python 开发人员自动化文件转换和操作。本指南将重点介绍通过 .NET 实现 Python 的 Aspose.HTML。此外,我们将逐步讲解相关步骤和代码片段,以了解其编程实现。本指南将用浅显易懂的语言解释整个过程。因此,请阅读本博文直至最后,学习如何使用通过 .NET 实现 Python 的 Aspose.HTML将 SVG 转换为 PDF 。
Aspose.Html 正版免费试用下载
SVG 转 PDF - 设置 SDK
在系统中打开终端/CMD并运行以下命令:
pip install aspose-html-net
或者,您可以打开此链接直接下载 Aspose.HTML SDK。
使用 Python 将 SVG 转换为 PDF - 代码片段
至此,我们已经安装了 SDK。接下来,我们将编写一段代码片段,以编程方式将 SVG 转换为 PDF。
步骤:
- 导入必要的模块。
- 通过初始化SVGDocument类的实例来加载源 SVG 文件。
- 创建PdfSaveOptions类的对象。
- 通过调用 c onvert_svg方法将 SVG 转换为 PDF。
使用下面给出的代码片段在 Python 中将 SVG 转换为 PDF:
import aspose.html as html from aspose.html.dom.svg import * from aspose.html.converters import * from aspose.html.saving import * # Path for the working files. inputDir = "aspose-logo.svg" outputDir = "file.pdf" # Load the license. License = html.License(); License.set_license("license.lic"); # Load the source SVG file by initializing an instance of the SVGDocument class. svgDocument = SVGDocument(inputDir) # Create an object of the PdfSaveOptions class. options = PdfSaveOptions() # Convert SVG to PDF by calling the convert_svg method. Converter.convert_svg(svgDocument, options, outputDir)
输出:
结论
综上所述,我们借助Aspose.HTML完成了 SVG 到 PDF 的程序化转换。本篇博文重点介绍了核心功能,您可以根据业务需求扩展功能范围。我们一直在撰写一些实用主题的文章。请持续关注,获取定期更新。