春雨春来 發表於 2025-7-25 16:01:00

借助Aspose.HTML控件,在 Python 中将 SVG 转换为 PDF

<p><img src="https://image.evget.com/attachment/keditor/image/20250725/140323_3.png"></p>
<p>您可能会发现许多解决方案都提供以编程方式将SVG转换为PDF 的功能。但这篇博文将介绍一个功能强大的 SDK,供 Python 开发人员自动化文件转换和操作。本指南将<strong><em>重点介绍</em></strong><strong><em>通过 .NET 实现 Python 的&nbsp;<u>Aspose.HTML</u></em></strong>。此外,我们将逐步讲解相关步骤和代码片段,以了解其编程实现。本指南将用浅显易懂的语言解释整个过程。因此,请阅读本博文直至最后,学习如何使用通过 .NET 实现 Python 的<strong>&nbsp;Aspose.HTML</strong>将 SVG 转换为 PDF 。</p>
<div style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>Aspose.Html 正版免费试用下载,请联系Aspose中国区官方授权代理商<span style="color: rgba(0, 0, 0, 1)">慧都科技</span></strong></span></div>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong><em>加入Aspose技术交流QQ群(1041253375),与更多小伙伴一起探讨提升开发技能。</em></strong></span></p>
<h2 id="svg-to-pdf---setting-up-sdk">SVG 转 PDF - 设置 SDK</h2>
<p>在系统中打开终端/CMD并运行以下命令:</p>
<pre class="prettyprint lang-js highlighter-hljs"><code>pip install aspose-html-net </code></pre>
<p>或者,您可以<strong><span style="text-decoration: underline">联系慧都科技</span><u>下载 Aspose.HTML SDK</u></strong>。</p>
<div style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>Aspose.Html 正版免费试用下载,请联系Aspose中国区官方授权代理商<span style="color: rgba(0, 0, 0, 1)">慧都科技</span></strong></span></div>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong><em>加入Aspose技术交流QQ群(1041253375),与更多小伙伴一起探讨提升开发技能。</em></strong></span></p>
<h2 id="convert-svg-to-pdf-in-python---code-snippet">使用 Python 将 SVG 转换为 PDF - 代码片段</h2>
<p>至此,我们已经安装了 SDK。接下来,我们将编写一段代码片段,以编程方式将 SVG 转换为 PDF。</p>
<p>步骤:</p>
<ul>
<li>导入必要的模块。</li>
<li>通过初始化SVGDocument类的实例来加载源 SVG 文件。</li>
<li>创建PdfSaveOptions类的对象。</li>
<li>通过调用 c&nbsp;onvert_svg方法将 SVG 转换为 PDF。</li>
</ul>
<p>使用下面给出的代码片段在 Python 中将 SVG 转换为 PDF:</p>
<pre class="prettyprint lang-js highlighter-hljs"><code>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)</code></pre>
<p>输出:</p>
<p><img src="https://image.evget.com/attachment/keditor/image/20250725/140452_9.png"></p>
<h2 id="conclusion">结论</h2>
<p>综上所述,我们借助<strong><u>Aspose.HT</u></strong><strong><u>ML</u></strong>完成了 SVG 到 PDF 的程序化转换。本篇博文重点介绍了核心功能,您可以根据业务需求扩展功能范围。我们一直在撰写一些实用主题的文章。请持续关注,获取定期更新。</p>
<div style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>spose.Html 正版免费试用下载,请联系Aspose中国区官方授权代理商<span style="color: rgba(0, 0, 0, 1)">慧都科技</span></strong></span></div>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong><em>加入Aspose技术交流QQ群(1041253375),与更多小伙伴一起探讨提升开发技能。</em></strong></span></p><br><br>
来源:https://www.cnblogs.com/software-Development/p/19004890
頁: [1]
查看完整版本: 借助Aspose.HTML控件,在 Python 中将 SVG 转换为 PDF