Convert SVG to PDF – Python Examples
Converting SVG documents to other formats is a key feature of Aspose.SVG for Python via .NET API. Converting SVG to PDF is often necessary to take advantage of the PDF format for specific tasks. PDF is a universally supported file format used for presenting images, documents, and books, making it easy to view, print, and share files online. Additionally, PDFs provide a secure format for protecting intellectual property through encryption and permissions settings. This article provides information on supported SVG to PDF conversion scenarios and how to execute them using Aspose.SVG Python library.
Online SVG Converter
You can check the Aspose.SVG functionality and convert SVG 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.
If you want to convert SVG to PDF programmatically, please see the following Python code examples.
Convert SVG to PDF using convert_svg()
method
The static methods of the Converter class can convert SVG to PDF with a few lines of code. It is the easiest way for conversion:
- Use the
set_extension() method of the
Configuration
class to register theSkiaSharp
extension. TheSkiaSharp
module is a graphics library used for rendering SVG content. It ensures that the rendering engine supports the operations needed for the conversion. - Create an instance of the PdfSaveOptions class.
- Load an SVG document using the SVGDocument() class.
- Use one of the convert_svg() methods to save SVG as a PDF file.
The following code snippet can be used to convert an SVG to PDF with default save options
:
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 = PdfSaveOptions()
10with SVGDocument("document.svg") as document:
11
12 # Convert SVG to PDF
13 Converter.convert_svg(document, options, "document.pdf")
PDF Save Options – PdfSaveOptions class
The
PdfSaveOptions class in Aspose.SVG for Python via .NET is used to specify various options for saving SVG documents as PDF files. This class allows you to customize the PDF output as per your requirements. Here are some key properties of the PdfSaveOptions
class:
Property | Description |
---|---|
jpeg_quality | Set the quality level for JPEG images within the PDF. A higher quality setting improves image fidelity but increases file size. The default value is 95. |
css | Gets a CssOptions object which is used for configuration of CSS properties processing. |
document_info | This property contains information about the output PDF document. Add metadata to the PDF, such as title, author, subject, and keywords. |
background_color | This property sets the color that will fill the background of every page. By default, this property is Transparent. |
page_setup | This property gets a page setup object and uses it for configuration output page-set. |
horizontal_resolution | Sets the horizontal resolution for output images in pixels per inch. The default value is 300 dpi. |
vertical_resolution | Sets the vertical resolution for output images in pixels per inch. The default value is 300 dpi. |
encryption | This property gets or sets encryption details. If it is not set, then no encryption will be performed. |
form_field_behaviour | This property specifies the behavior of form fields in the output PDF document. |
Note: The options that are implementing with the PdfSaveOptions class are inheriting from the PdfRenderingOptions class.
The following Python code example shows how you can use save options when converting SVG to PDF.
1import os
2import aspose
3from aspose.svg import *
4from aspose.svg.converters import *
5from aspose.svg.drawing.skiasharp import *
6from aspose.svg.rendering import *
7from aspose.svg.drawing import *
8from aspose.svg.saving import *
9
10# Initialize an SVG document from a file
11input_folder = "data/"
12output_folder = "output/"
13src_file = os.path.join(input_folder, "document.svg")
14output_file = os.path.join(output_folder, "document.pdf")
15if not os.path.exists(output_folder):
16 os.makedirs(output_folder)
17
18# Activate the Aspose.SVG.Drawing.SkiaSharp feature
19Configuration.set_extension(SkiaModule())
20
21with SVGDocument(src_file) as document:
22 options = PdfSaveOptions()
23 options.background_color = aspose.pydrawing.Color.transparent
24 options.page_setup.sizing = SizingType.FIT_CONTENT
25 options.horizontal_resolution = Resolution.from_dots_per_inch(96.0)
26 options.vertical_resolution = Resolution.from_dots_per_inch(96.0)
27 options.jpeg_quality = 80
28
29 # Convert SVG to PDF
30 Converter.convert_svg(document, options, output_file)
Convert SVG to PDF using render_to()
method
Consider SVG to PDF conversion scenario using
render_to(device
) method. The following code snippet can be used to convert an SVG to PDF with explicitly specified rendering options. In addition, the Python example will show how to configure the paths to the source and output files in your file system:
- Initialize an SVG document using the SVGDocument() class.
- Create an instance of the PdfRenderingOptions class and specify the required rendering options.
- Create a new instance of the PdfDevice class.
- Convert SVG to PDF using the
render_to(
device
) method of theSVGDocument
class.
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 peg_quality and page_setup properties
16 pdf_rendering_options = PdfRenderingOptions()
17 pdf_rendering_options.jpeg_quality = 10
18 pdf_rendering_options.page_setup.sizing = SizingType.FIT_CONTENT
19
20 # Initialize an instance of the PdfDevice class
21 with PdfDevice(pdf_rendering_options, output_file) as device:
22 # Render SVG to PDF and send the document to the rendering device
23 document.render_to(device)
PdfRenderingOptions
is a class in Python API that provides options for rendering SVG content to PDF format. It allows customization of various aspects of the rendering process, such as background color, page setup, resolution, encryption, and JPEG quality. The PdfRenderingOptions
class is used in conjunction with the specific device PdfDevice
, which represents the target output format for the rendered SVG content.
Note: The key properties of the PdfRenderingOptions
class are the same as those of the PdfSaveOptions
class since
PdfSaveOptions inherits from
PdfRenderingOptions.
You can convert SVG to PDF with our free online SVG to PDF Converter that works with high quality, easy and fast. Just upload SVG, convert it, and get results in seconds! It’s fast, easy, and completely free!