南山颂 發表於 2025-10-10 14:23:00

CAD文件处理控件Aspose.CAD教程:在 Python 中将 SVG 转换为 PDF

<p><img src="https://image.evget.com/attachment/keditor/image/20251010/100135_1.png"></p>
<p>如果您处理二维图形,SVG文件格式是最广泛使用的文件格式。这种文件格式具有高度可扩展性,并且质量极佳。然而,在文档共享、打印或存档方面,&nbsp;PDF文件格式因其增强的可移植性而更受青睐。</p>
<p>在本文中,我们将使用 Python 以编程方式将 SVG 转换为 PDF。我们将通过 .NET 使用&nbsp;<strong><u>Aspose.CAD for Python</u></strong>,这款 CAD SDK 功能强大,足以顺利完成 SVG 到 PDF 的转换,我们将通过编写代码示例来演示其功能。</p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>Aspose.CAD 试用版下载,请联系Aspose官方授权代理商慧都科技</strong></span></p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong><em>加入Aspose技术交流QQ群(</em></strong><em>1041253375</em><strong><em>),与更多小伙伴一起探讨提升开发技能。</em></strong></span></p>
<h2 id="cad-sdk-installation">CAD SDK安装</h2>
<p>你可以通过在 CMD 中运行以下命令来通过 .NET 安装&nbsp;<strong><u>Aspose.CAD for Python</u></strong>&nbsp;:</p>
<pre class="prettyprint highlighter-hljs"><code>pip install aspose-cad</code></pre>
<p>如果您不喜欢使用 CMD 选项,您可以<strong>联系Aspose官方授权代理商慧都科技下载SDK 文件</strong>。</p>
<h2 id="code">使用 Python 将 SVG 转换为 PDF - 代码片段</h2>
<p>SDK 已安装,现在我们将实现该功能。</p>
<p>请按照以下步骤操作:</p>
<ul>
<li>定义工作目录的路径并应用&nbsp;<strong>Aspose.CAD</strong>&nbsp;许可证。</li>
<li>通过调用load方法加载源 SVG 文件。</li>
<li><strong>创建CadRasterizationOptions</strong>类的实例。</li>
<li>设置页面高度。</li>
<li>设置页面宽度。</li>
<li><strong>实例化PdfOptions</strong>类的实例。</li>
<li>调用保存方法将文件保存为 PDF。</li>
</ul>
<p>以下代码示例展示了如何以编程方式在 Python 中将 SVG 转换为 PDF:</p>
<pre class="prettyprint lang-py highlighter-hljs"><code>import aspose.cad as cad
from aspose.cad.imageoptions import PdfOptions
# Define the path for the working directory and apply Aspose.CAD license.
dataDir = "data"
license = cad.License()
license.set_license("latest.lic")
# Load the source SVG file by calling the load method.
image = cad.Image.load(dataDir+"paths.svg")
# Create an instance of the CadRasterizationOptions class.
cadRasterizationOptions = cad.imageoptions.CadRasterizationOptions()
# Set the page height.
cadRasterizationOptions.page_height = 800.5
# Set the page width.
cadRasterizationOptions.page_width = 800.5
cadRasterizationOptions.zoom = 0.5
cadRasterizationOptions.layers = "Layer"
# Instantiate an instance of the PdfOptions class.
options = PdfOptions()
options.vector_rasterization_options = cadRasterizationOptions
# Invoke the save method to save the file as PDF.
image.save(dataDir+"result.pdf", options)</code></pre>
<p>输出:</p>
<p><img src="https://image.evget.com/attachment/keditor/image/20251010/100345_3.png"></p>
<p>总之,<strong>Aspose.CAD for Python via .NET</strong>提供了一种简洁易用的方法,可以在 Python 中将 SVG 转换为 PDF。最重要的是,该解决方案不依赖任何第三方库。您可以在 Python 应用程序中无缝地实现此转换。</p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>Aspose.CAD 试用版下载,请联系Aspose官方授权代理商慧都科技</strong></span></p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong><em>加入Aspose技术交流QQ群(</em></strong><em>1041253375</em><strong><em>),与更多小伙伴一起探讨提升开发技能。</em></strong></span></p><br><br>
来源:https://www.cnblogs.com/software-Development/p/19132986
頁: [1]
查看完整版本: CAD文件处理控件Aspose.CAD教程:在 Python 中将 SVG 转换为 PDF