Convert SVG Files in Python – Python Examples

Converting SVG to other formats allows for better integration into workflows that require formats such as PDF for print or PNG for web use. Additionally, conversion to raster formats such as JPEG or PNG ensures consistent rendering and display across devices and browsers. Aspose.SVG for Python via .NET API allows you to convert SVG files to PDF, XPS, JPG, PNG, BMP, TIFF, and GIF file formats. You can use API in your Python projects to develop converter applications without getting into the details of underlying file formats.

Online SVG Converter

Using Aspose.SVG, you can convert SVG to other formats in real-time. Simply load the SVG from a local file system or URL, select the desired output format, and run the converter. The save options are set to default, and you will immediately receive the result as a separate file.

                
            

How to Convert SVG in Python

You can convert SVG to various popular formats in any way – online or programmatically. Converting from SVG to other formats can perform by using one of the convert_svg() methods of the Converter class or the render_to(device) method of the SVGDocument class.

The current section outlines the supported scenarios for converting SVG files to other popular formats using the Converter and SVGDocument classes:

  1. The static Converter class acts as a shared facade for converting SVG files to popular formats, making these operations convenient and straightforward. It provides a wide range of convert_svg() methods that convert SVG to PDF, XPS, or image files.
  2. The render_to() method of the SVGDocument class is used to render SVG to another format and sends the document to an output device. Aspose.SVG for Python via .NET API includes the following output device implementations: the PdfDevice, XpsDevice, and ImageDevice, which facilitate rendering to PDF, XPS, and image file formats respectively.

Let’s consider both scenarios of conversion SVG document to another file format:

Convert SVG to PNG Using the convert_svg() Method

Converting an SVG file to another format using the convert_svg() method is a series of steps:

 1import os
 2from aspose.svg import *
 3from aspose.svg.converters import *
 4from aspose.svg.drawing.skiasharp import *
 5from aspose.svg.saving import *
 6
 7# Activate the Aspose.SVG.Drawing.SkiaSharp feature
 8Configuration.set_extension(SkiaModule())
 9options = ImageSaveOptions()
10with SVGDocument("image.svg") as document:
11
12	# Convert SVG to PNG
13	Converter.convert_svg(document, options, "image.png")

This example shows how to convert an SVG file to an image using the default conversion options. If you want to control the conversion process and set your own saving options, please refer to the Convert SVG to Image article.

Convert SVG to PDF Using the render_to() Method

To convert SVG to PDF using the render_to() method, take the following steps:

 1import os
 2from aspose.svg import *
 3from aspose.svg.rendering import *
 4from aspose.svg.rendering.pdf import *
 5
 6# Initialize an SVG document from a file
 7input_folder = "data/"
 8output_folder = "output/"
 9src_file = os.path.join(input_folder, "document.svg")
10output_file = os.path.join(output_folder, "document.pdf")
11if not os.path.exists(output_folder):
12	os.makedirs(output_folder)
13
14with SVGDocument(src_file) as document:
15	# Initialize an instance of the PdfRenderingOptions class and set custom jpeg_quality properties
16	pdf_rendering_options = PdfRenderingOptions()
17	pdf_rendering_options.jpeg_quality = 10
18
19	# Initialize an instance of the PdfDevice class
20	with PdfDevice(pdf_rendering_options, output_file) as device:
21		# Render SVG to PDF and send the document to the rendering device
22		document.render_to(device)

General Options

The RenderingOptions class is used with specific device classes, such as PdfDevice, XpsDevice, and ImageDevice, representing the target output formats for rendered SVG content. By setting the properties of the RenderingOptions class, you can ensure that the rendered result meets their specific needs and quality standards. These options allow you to adjust various aspects of rendering, such as page setup, background color, and specific rendering options for different output formats. Here are some key properties of the RenderingOptions class:

Every output device PdfDevice, XpsDevice, and ImageDevice has its own unique set of options implemented with classes PdfRenderingOptions, XpsRenderingOptions, and ImageRenderingOptions respectively. These classes inherit basic properties from the RenderingOptions class and also have their own rendering properties that take into account format features.

Aspose.SVG provides a free online SVG Converter that allows you to easily convert SVG files to various popular formats. You can convert SVG to PDF, XPS, JPG, PNG, BMP, TIFF, and GIF by simply selecting a file, choosing the desired format, and completing the conversion. It’s quick, easy, and entirely free!

Text “Banner SVG Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.