您可能會發現許多解決方案都提供以編程方式將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 的程序化轉換。本篇博文重點介紹了核心功能,您可以根據業務需求擴展功能范圍。我們一直在撰寫一些實用主題的文章。請持續關注,獲取定期更新。