Convert SVG to PDF or PDF to SVG with Python

Alice Yang
4 min readNov 3, 2023

--

Convert SVG to PDF or PDF to SVG with Python
Convert SVG to PDF or PDF to SVG with Python

Scalable Vector Graphics (SVG) and Portable Document Format (PDF) are popular file formats widely utilized for creating and sharing visual content. SVG is specifically designed for web graphics and scalable illustrations, offering the advantage of resolution-independent and interactive graphics. On the other hand, PDF is a versatile format known for its ability to preserve document fidelity and support complex layouts, making it suitable for high-quality document representation. In certain scenarios, you may need to perform conversions between the SVG and PDF formats. In this article, we will explore how to convert SVG to PDF and PDF to SVG using Python.

We’ll discuss the following topics:

Python Library to Convert Between SVG and PDF

To convert between SVG and PDF with Python, we can use the Spire.PDF for Python library.

Spire.PDF for Python is a feature-rich and user-friendly library that enables creating, reading, editing, and converting PDF files within Python applications. With this library, you can perform a wide range of manipulations on PDFs, including adding text or images, extracting text or images, adding digital signatures, adding or deleting pages, merging or splitting PDFs, creating bookmarks, adding text or image watermarks, inserting fillable forms and many more. In addition, you are also able to convert PDF files to various file formats, such as Word, Excel, images, HTML, SVG, XPS, OFD, PCL, and PostScript.

You can install Spire.PDF for Python from PyPI using the following pip command:

pip install Spire.Pdf

For more detailed information about the installation, you can check this official documentation: How to Install Spire.PDF for Python in VS Code.

Convert SVG to PDF with Python

Converting SVG files to PDF allows you to transform web graphics and scalable illustrations into a format suitable for high-quality printing or sharing as a document.

Here is a simple example that shows how to convert an SVG file to PDF format:

from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument object
doc = PdfDocument()
# Load an SVG file
doc.LoadFromSvg("Example.svg")

# Save the SVG file to PDF format
doc.SaveToFile("ConvertSvgToPdf.pdf", FileFormat.PDF)
# Close the PdfDocument object
doc.Close()

Add SVG to PDF with Python

In addition to converting an SVG file to PDF, you can also draw an SVG file at a specific location on a PDF page.

Here is a simple example that shows how to draw an SVG file at a specific location on a PDF page:

from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument object
doc = PdfDocument()
# Load an SVG file
doc.LoadFromSvg("Example.svg")

# Create a template from the SVG
template = doc.Pages[0].CreateTemplate()

# Create another PdfDocument object
newDoc = PdfDocument()
# Load a PDF file
newDoc.LoadFromFile("Example.pdf")

# Draw the template at a specified location on the first page of the loaded PDF file
newDoc.Pages[0].Canvas.DrawTemplate(template, PointF(150.0, 120.0))

# Save the resulting PDF file
newDoc.SaveToFile("AddSvgToPdf.pdf", FileFormat.PDF)
# Close the PdfDocument objects
newDoc.Close()
doc.Close()

Convert PDF to SVG with Python

Converting PDF to SVG is valuable when you need to edit or reuse vector graphics from a PDF file, such as modifying individual elements, scaling without loss of quality, or integrating the artwork into web or interactive applications.

With Spire.PDF for Python, you can easily convert a PDF file to SVG. Furthermore, you can also control the dimensions of the resulting SVG files.

Here is a simple example that shows how to convert a PDF file to SVG with specific dimensions:

from spire.pdf.common import *
from spire.pdf import *

# Create a PDF document object
doc = PdfDocument()
# Load a PDF file
doc.LoadFromFile("Sample.pdf")

# Set the width and height of output SVG files
doc.ConvertOptions.SetPdfToSvgOptions(600.0, 600.0)

# Save each page of the file to a separate SVG file
doc.SaveToFile("ConvertPdfToSvg.svg", FileFormat.SVG)
# Close the PdfDocument object
doc.Close()

Convert Certain Pages of a PDF to SVG with Python

In addition to converting all pages of a PDF file to SVG, you can also convert a page or range of pages in a PDF file to SVG.

Here is a simple example that shows how to convert 3–4 pages in a PDF file to SVG:

from spire.pdf.common import *
from spire.pdf import *

# Create a PDF document object
doc = PdfDocument()
# Load a PDF file
doc.LoadFromFile("Sample.pdf")

# Convert 3-4 pages of the PDF file to SVG files
doc.SaveToFile("CertainPagesToSvg.svg", 2, 3, FileFormat.SVG)
# Close the PdfDocument object
doc.Close()

Conclusion

This article demonstrated how to convert SVG to PDF, add SVG to PDF, and convert PDF to SVG using Python. We hope you find it helpful.

Related Topics

--

--

Alice Yang

Skilled senior software developers with five years of experience in all phases of software development life cycle using .NET, Java and C++ languages.