Convert SVG Files in Python

Quick Answer

Aspose.SVG for Python via .NET converts SVG to PDF, PNG, JPG, BMP, TIFF, GIF, and XPS. Use Converter.convert_svg() for direct file conversion, or load an SVGDocument and call render_to(device) when you need a rendering-device workflow.

Aspose.SVG APIs Used

APIPurpose
SVGDocumentLoads an SVG document for conversion or rendering
Converter.convert_svg()Converts SVG to PDF, XPS, or image formats
ImageSaveOptionsConfigures SVG-to-image conversion
PdfSaveOptionsConfigures SVG-to-PDF conversion
PdfDeviceReceives rendered SVG content for PDF output
ImageDeviceReceives rendered SVG content for image output

SVG Conversion in Python

SVG is ideal for scalable graphics, but many workflows require fixed output formats. Use PDF for print and document exchange, PNG or JPG for web previews and raster assets, TIFF or BMP for image pipelines, GIF for simple raster output, and XPS for fixed-layout document workflows.

This page gives a short overview of the SVG conversion API. For detailed options and format-specific examples, use the linked articles below.

Supported Output Formats

Output formatTypical useDetailed article
PDFPrint, archiving, document exchangeConvert SVG to PDF
PNGWeb images, transparent raster output, previewsConvert SVG to PNG
JPGRaster previews without transparencyConvert SVG to Image
BMPSimple bitmap outputConvert SVG to Image
TIFFMulti-purpose raster workflows and publishingConvert SVG to Image
GIFLightweight raster outputConvert SVG to Image
XPSFixed-layout document outputUse Converter.convert_svg() with XPS save options

Convert SVG Using Converter.convert_svg()

The Converter class is the easiest way to convert SVG files. Create save options for the target format, load the SVG document, and call Converter.convert_svg().

1from aspose.svg import SVGDocument
2from aspose.svg.converters import Converter
3from aspose.svg.saving import ImageSaveOptions
4
5# Convert SVG to PNG using the high-level converter API
6options = ImageSaveOptions()
7
8with SVGDocument("image.svg") as document:
9    Converter.convert_svg(document, options, "image.png")

Convert SVG Using render_to()

The render_to() method sends SVG content to a rendering device. Use this workflow when your application needs explicit control over a target device such as PdfDevice, XpsDevice, or ImageDevice.

 1import os
 2from aspose.svg import SVGDocument
 3from aspose.svg.rendering.pdf import PdfDevice, PdfRenderingOptions
 4
 5# Render SVG to PDF using a rendering device
 6input_folder = "data/"
 7output_folder = "output/"
 8input_path = os.path.join(input_folder, "document.svg")
 9output_path = os.path.join(output_folder, "document.pdf")
10os.makedirs(output_folder, exist_ok=True)
11
12options = PdfRenderingOptions()
13options.jpeg_quality = 80
14
15with SVGDocument(input_path) as document:
16    with PdfDevice(options, output_path) as device:
17        document.render_to(device)

Conversion Options

Most SVG conversion tasks use save options or rendering options to control output quality, page setup, background color, and resource handling.

Option groupUsed withExamples
ImageSaveOptionsPNG, JPG, BMP, TIFF, GIF outputImage format, background color, page setup, resolution
PdfSaveOptionsPDF output through Converter.convert_svg()Page setup, JPEG quality, metadata, encryption, background color
PdfRenderingOptionsPDF output through PdfDevicePage setup, JPEG quality, resolution, background color
RenderingOptionsDevice-based rendering workflowsCommon rendering settings inherited by format-specific options

Online SVG Converter

Use the free online SVG Converter to test SVG conversion before implementing it in Python. Upload an SVG file, select an output format, and compare the generated file.

                
            

Online SVG converter for PDF, PNG, JPG, BMP, TIFF, GIF, and XPS

Common Mistakes and Fixes

ProblemLikely causeFix
Output file is missingThe output folder does not exist or the file was saved in another working directoryCreate the folder with os.makedirs() and use explicit paths
Converted image has the wrong backgroundThe SVG contains transparent areas and no background was configuredSet the background color in the save or rendering options
PDF or image size is unexpectedPage setup does not match the SVG viewport or content boundsConfigure page size, margins, or sizing mode in the relevant options object
External images or fonts are missingRelative resources cannot be resolved during conversionKeep resources next to the SVG or use correct absolute paths/base paths
Output is too largeThe SVG is complex or raster settings are too highReduce resolution, JPEG quality, image size, or simplify the SVG before conversion

FAQ

How do I convert SVG files in Python?
Use SVGDocument to load the file, create save options for the target format, and call Converter.convert_svg().

Which SVG output formats are supported?
Aspose.SVG for Python via .NET supports conversion to PDF, XPS, PNG, JPG, BMP, TIFF, and GIF.

Should I use Converter.convert_svg() or render_to()?
Use Converter.convert_svg() for ordinary file conversion. Use render_to() when you need a rendering-device workflow with PdfDevice, XpsDevice, or ImageDevice.

Can I convert SVG from a URL?
Yes. Load the SVG URL with SVGDocument when network access is available, then convert it with the same API used for local files.

Can I change the output size during conversion?
Yes. Configure page setup, sizing, margins, and resolution through the target format’s save or rendering options.

Related Articles