Convert SVG to PDF in Python

Use SVGDocument to load an SVG file, create PdfSaveOptions, and call Converter.convert_svg(document, options, output_path) to save SVG as PDF. Use PdfSaveOptions to set page sizing, background color, resolution, JPEG quality, metadata, or encryption when the default output is not enough.

Aspose.SVG APIs Used

APIPurpose
SVGDocumentLoads the source SVG document
Converter.convert_svg()Converts SVG to PDF, XPS, or image formats
PdfSaveOptionsConfigures PDF output for converter-based workflows
PdfRenderingOptionsConfigures PDF output for rendering-device workflows
PdfDeviceReceives rendered SVG content and writes a PDF file

Convert SVG to PDF Programmatically

PDF is a common output format when SVG graphics must be printed, archived, attached to documents, or shared in a format that preserves layout across viewers. Aspose.SVG for Python via .NET lets you convert SVG to PDF with a high-level converter API or with a rendering device when you need explicit control over the rendering pipeline.

Use Converter.convert_svg() for most file conversion tasks. Use SVGDocument.render_to() with PdfDevice when your workflow already uses rendering devices or needs a device-based rendering pattern.

Before running the examples, install Aspose.SVG for Python via .NET in your Python environment.

Convert SVG to PDF Using Converter.convert_svg()

The simplest way to convert SVG to PDF is to load the SVG document, create PdfSaveOptions, and call Converter.convert_svg().

1from aspose.svg import SVGDocument
2from aspose.svg.converters import Converter
3from aspose.svg.saving import PdfSaveOptions
4
5# Convert an SVG file to PDF using the default save options
6options = PdfSaveOptions()
7
8with SVGDocument("document.svg") as document:
9    Converter.convert_svg(document, options, "document.pdf")

Convert SVG to PDF with Custom Save Options

Use PdfSaveOptions when you need to control PDF output. The following example sets a transparent background, fits the page to the SVG content, changes raster image resolution, and adjusts JPEG quality.

 1import os
 2from aspose.svg import SVGDocument
 3from aspose.svg.converters import Converter
 4from aspose.svg.drawing import Resolution
 5from aspose.svg.rendering import SizingType
 6from aspose.svg.saving import PdfSaveOptions
 7from aspose.pydrawing import Color
 8
 9# Convert SVG to PDF and customize page sizing and image quality
10input_folder = "data/"
11output_folder = "output/"
12input_path = os.path.join(input_folder, "document.svg")
13output_path = os.path.join(output_folder, "document.pdf")
14os.makedirs(output_folder, exist_ok=True)
15
16options = PdfSaveOptions()
17options.background_color = Color.from_argb(0, 255, 255, 255)
18options.page_setup.sizing = SizingType.FIT_CONTENT
19options.horizontal_resolution = Resolution.from_dots_per_inch(96.0)
20options.vertical_resolution = Resolution.from_dots_per_inch(96.0)
21options.jpeg_quality = 80
22
23with SVGDocument(input_path) as document:
24    Converter.convert_svg(document, options, output_path)

Common PdfSaveOptions Properties

PropertyWhat it controls
background_colorPage background color when the SVG has transparent areas
page_setupOutput page size, margins, and sizing mode
horizontal_resolutionHorizontal resolution for rasterized resources inside the PDF
vertical_resolutionVertical resolution for rasterized resources inside the PDF
jpeg_qualityJPEG compression quality for embedded raster images
document_infoPDF metadata such as title, author, subject, and keywords
encryptionPDF encryption and permission settings

PdfSaveOptions inherits PDF rendering settings from PdfRenderingOptions, so many output properties are shared between converter-based and device-based workflows.

Convert SVG to PDF Using render_to()

The render_to() method sends the loaded SVG document to a rendering device. This pattern is useful when your application already works with Aspose.SVG rendering devices or needs to choose the target device explicitly.

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

Converter.convert_svg() vs render_to()

MethodBest for
Converter.convert_svg()Direct SVG-to-PDF conversion with concise code and save options
SVGDocument.render_to()Rendering workflows that use output devices such as PdfDevice

For ordinary SVG to PDF conversion, start with Converter.convert_svg(). It is shorter and maps naturally to file conversion tasks. Use render_to() when you need the rendering-device pattern.

Common Mistakes and Fixes

ProblemLikely causeFix
The PDF page size is not what you expectedDefault page setup does not match the SVG viewport or content boundsSet options.page_setup.sizing and configure page size or margins explicitly
Transparent SVG areas appear with an unwanted backgroundThe PDF viewer or output workflow applies its own backgroundSet options.background_color to the required color before conversion
Embedded images look blurry or too largeResolution and JPEG quality settings do not match the output targetAdjust horizontal_resolution, vertical_resolution, and jpeg_quality
Output PDF is not createdThe target folder does not exist or the script saves to another working directoryCreate the output folder with os.makedirs() and build paths with os.path.join()
Text or external resources are missingThe SVG references fonts, images, or CSS files that cannot be resolvedUse correct file paths and keep external resources available relative to the SVG document

Online SVG to PDF Converter

You can test SVG to PDF conversion with the free online SVG to PDF Converter. Upload an SVG file, run the conversion, and compare the result before implementing the same workflow in Python.

                
            

Online SVG to PDF converter

FAQ

How do I convert SVG to PDF in Python?

Load the SVG with SVGDocument, create PdfSaveOptions, and call Converter.convert_svg(document, options, output_path).

Can I set the PDF page size during SVG conversion?

Yes. Use the page_setup property of PdfSaveOptions or PdfRenderingOptions to configure the page size, margins, and sizing mode.

Can I convert SVG to PDF without using the Converter class?

Yes. Create PdfRenderingOptions, open a PdfDevice, and call document.render_to(device).

Does SVG to PDF conversion preserve transparency?

Transparent SVG content can be preserved, but the visible result depends on the PDF viewer and workflow. Set background_color if you need a predictable page background.

How can I reduce the size of the generated PDF?

Lower jpeg_quality, reduce embedded raster image resolution, and simplify large or complex SVG content before conversion.

Related Articles