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

如果您处理二维图形,SVG文件格式是最广泛使用的文件格式。这种文件格式具有高度可扩展性,并且质量极佳。然而,在文档共享、打印或存档方面, PDF文件格式因其增强的可移植性而更受青睐。

在本文中,我们将使用 Python 以编程方式将 SVG 转换为 PDF。我们将通过 .NET 使用 ++Aspose.CAD for Python++,这款 CAD SDK 功能强大,足以顺利完成 SVG 到 PDF 的转换,我们将通过编写代码示例来演示其功能。

Aspose.CAD 试用版下载

CAD SDK安装

你可以通过在 CMD 中运行以下命令来通过 .NET 安装 ++Aspose.CAD for Python++

复制代码
pip install aspose-cad

如果您不喜欢使用 CMD 选项,您可以下载SDK 文件。

使用 Python 将 SVG 转换为 PDF - 代码片段

SDK 已安装,现在我们将实现该功能。

请按照以下步骤操作:

  • 定义工作目录的路径并应用 Aspose.CAD 许可证。
  • 通过调用load方法加载源 SVG 文件。
  • 创建CadRasterizationOptions类的实例。
  • 设置页面高度。
  • 设置页面宽度。
  • 实例化PdfOptions类的实例。
  • 调用保存方法将文件保存为 PDF。

以下代码示例展示了如何以编程方式在 Python 中将 SVG 转换为 PDF:

复制代码
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)

输出:

总之,**Aspose.CAD for Python via .NET**提供了一种简洁易用的方法,可以在 Python 中将 SVG 转换为 PDF。最重要的是,该解决方案不依赖任何第三方库。您可以在 Python 应用程序中无缝地实现此转换。

相关推荐
默_笙2 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
qq_426003962 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫2 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技2 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读2 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时2 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
C语言小火车2 天前
C/C++ 为什么需要编译器?
开发语言·c++
奇思妙想聪明勤奋的小羊2 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
lpfasd1232 天前
2026年第38周GitHub趋势周报
python·科技·github