Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
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.
| API | Purpose |
|---|---|
| SVGDocument | Loads the source SVG document |
| Converter.convert_svg() | Converts SVG to PDF, XPS, or image formats |
| PdfSaveOptions | Configures PDF output for converter-based workflows |
| PdfRenderingOptions | Configures PDF output for rendering-device workflows |
| PdfDevice | Receives rendered SVG content and writes a PDF file |
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.
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")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)| Property | What it controls |
|---|---|
| background_color | Page background color when the SVG has transparent areas |
| page_setup | Output page size, margins, and sizing mode |
| horizontal_resolution | Horizontal resolution for rasterized resources inside the PDF |
| vertical_resolution | Vertical resolution for rasterized resources inside the PDF |
| jpeg_quality | JPEG compression quality for embedded raster images |
| document_info | PDF metadata such as title, author, subject, and keywords |
| encryption | PDF encryption and permission settings |
PdfSaveOptions inherits PDF rendering settings from
PdfRenderingOptions, so many output properties are shared between converter-based and device-based workflows.
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)| Method | Best 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.
| Problem | Likely cause | Fix |
|---|---|---|
| The PDF page size is not what you expected | Default page setup does not match the SVG viewport or content bounds | Set options.page_setup.sizing and configure page size or margins explicitly |
| Transparent SVG areas appear with an unwanted background | The PDF viewer or output workflow applies its own background | Set options.background_color to the required color before conversion |
| Embedded images look blurry or too large | Resolution and JPEG quality settings do not match the output target | Adjust horizontal_resolution, vertical_resolution, and jpeg_quality |
| Output PDF is not created | The target folder does not exist or the script saves to another working directory | Create the output folder with os.makedirs() and build paths with os.path.join() |
| Text or external resources are missing | The SVG references fonts, images, or CSS files that cannot be resolved | Use correct file paths and keep external resources available relative to the SVG document |
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.
Load the SVG with SVGDocument, create PdfSaveOptions, and call Converter.convert_svg(document, options, output_path).
Yes. Use the page_setup property of PdfSaveOptions or PdfRenderingOptions to configure the page size, margins, and sizing mode.
Yes. Create PdfRenderingOptions, open a PdfDevice, and call document.render_to(device).
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.
Lower jpeg_quality, reduce embedded raster image resolution, and simplify large or complex SVG content before conversion.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.