Convert SVG to PDF in Python – Aspose.HTML

In this article, you will find information on how to convert SVG to PDF using convert_svg() methods of the Converter class and how to apply PdfSaveOptions. With Aspose.HTML for Python via .NET, you can convert SVG to PDF format programmatically with full control over a wide range of conversion parameters. Also, you can try an Online SVG Converter to test the Python API functionality and convert SVG on the fly.

Online SVG Converter

You can convert SVG to other formats with Aspose.HTML API in real time. Please load SVG from your local file system or URL, select the output format and run the example. The save options are set by default. You will immediately receive the conversion result as a separate file.

                
            

Convert SVG to PDF

Let’s consider how to convert an SVG document into a PDF file format. The convert_svg() methods of the Converter class are primarily used as the easiest way to convert an SVG file into various formats. You should follow a few steps:

  1. Load an SVG document. To convert, you can load SVG from a file, URL, or code string. In the following example, we create an SVG file from code.
  2. Create a new PdfSaveOptions object. If you do not set specific options for saving the resulting PDF file, the default options will be used.
  3. Use the convert_svg(content, base_uri, options, output_path) method of the Converter class to save SVG as a PDF file.
 1import os
 2from aspose.html import *
 3from aspose.html.converters import *
 4from aspose.html.saving import *
 5
 6# Setup directories and define paths
 7output_dir = "output/"
 8if not os.path.exists(output_dir):
 9    os.makedirs(output_dir)
10save_path = os.path.join(output_dir, "circles.pdf")
11
12# SVG code
13svg_code = """
14<svg xmlns="http://www.w3.org/2000/svg">
15    <circle id="base" cx="100" cy="100" r="80" fill="teal" stroke="salmon" stroke-width="10" />
16    <g> 
17        <use href="#base" transform="translate(120, 10) scale(0.9)" />
18        <use href="#base" transform="translate(240, 20) scale(0.8)" />
19        <use href="#base" transform="translate(360, 30) scale(0.7)" />
20        <use href="#base" transform="translate(480, 40) scale(0.6)" />
21        <use href="#base" transform="translate(600, 50) scale(0.5)" />
22    </g>
23</svg>
24"""
25
26# Initialize PdfSaveOptions
27options = PdfSaveOptions()
28
29# Convert SVG to PDF
30Converter.convert_svg(svg_code, ".", options, save_path)

The figure shows the quality of SVG to PDF rendering using a fragment of the circles.pdf file as an example. We talk about a file fragment because the full file size will correspond to an A4 page since the save options are set to default.

Text “circles.pdf image”

Convert SVG to PDF using PdfSaveOptions

Aspose.HTML for Python via .NET provides the PdfSaveOptions class, which gives you more control over how documents are saved in PDF format. Some properties of this class inherit properties of base classes, such as PdfRenderingOptions or RenderingOptions. Here is a description of some properties available in PdfSaveOptions:

To convert SVG to PDF with PdfSaveOptions specifying, you should follow a few steps:

  1. Load an SVG file using one of the SVGDocument() constructors of the SVGDocument class.
  2. Create a new PdfSaveOptions object and specify save options.
  3. Use one of the convert_svg() methods to save SVG as a PDF file. In the example, the convert_svg() method takes the document, options, and output file path save_path and performs the conversion operation.

The following Python code snippet configures PDF save options to set page size and margins, apply CSS rules for printing, compress JPEG images at 80% quality, and enable tagged PDFs to improve accessibility:

 1import os
 2from aspose.html import *
 3from aspose.html.saving import *
 4from aspose.html.drawing import *
 5from aspose.html.converters import *
 6from aspose.html.dom.svg import *
 7
 8# Setup directories and define paths
 9output_dir = "output/"
10input_dir = "data/"
11if not os.path.exists(output_dir):
12    os.makedirs(output_dir)
13
14document_path = os.path.join(input_dir, "document.svg")
15save_path = os.path.join(output_dir, "svg-to-pdf.pdf")
16
17# Load an SVG document
18document = SVGDocument(document_path)
19
20# Initialize PdfSaveOptions
21options = PdfSaveOptions()
22
23# Customize save options for PDF
24options.page_setup.any_page = Page(Size(600, 500), Margin(20, 20, 10, 10))
25options.css.media_type.PRINT
26options.jpeg_quality = 80
27options.is_tagged_pdf = True
28
29# Convert SVG to PDF
30Converter.convert_svg(document, options, save_path)

In the above example, we use:

Check the quality of SVG to PDF conversion with our online SVG to PDF Converter. Upload, convert your files and get results in a few seconds. Try our forceful SVG to PDF Converter for free now!

Download our Aspose.HTML for .NET library allows you to successfully, quickly, and easily convert your HTML, MHTML, EPUB, SVG, and Markdown documents to the most popular formats.

Text “SVG to PDF Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.