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:
- 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.
- Create a new PdfSaveOptions object. If you do not set specific options for saving the resulting PDF file, the default options will be used.
- 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.
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
:
- page_setup – This property provides access to a PageSetup object used to configure the layout and settings of the output PDF pages to fit specific printing or display requirements.
- horizontal_resolution – This property controls the horizontal resolution for both internal images used during processing and any external images included in the HTML. By default, it is set to 300 dpi.
- vertical_resolution – Similar to horizontal_resolution, this property manages the vertical resolution for internal and external images during PDF generation. Like its horizontal counterpart, it defaults to 300 dpi.
- background_color – This property sets or retrieves the background color that fills each PDF document page. The default value is transparent, but this can be customized to suit branding or aesthetic preferences, ensuring consistency across all pages.
- jpeg_quality – This property determines the JPEG compression quality used for images embedded in a PDF document. The default quality is set to 95, providing a good balance between image fidelity and file size. Setting this property allows you to optimize file size or image quality based on your specific needs.
- css – This property uses a
CssOptions
object to configure the processing of CSS properties during HTML to PDF conversion. It allows precise control over how styles from the HTML are interpreted and applied in the resulting PDF. - encryption – This property provides detailed information about PDF document encryption, including password protection and permission settings. If it is not configured, no encryption is applied, but setting this property allows you to distribute and control access to sensitive PDF content securely.
- is_tagged_pdf – When set to true, a tagged layout is created within the PDF document, enhancing accessibility for users with disabilities. This ensures that content is properly structured and navigable using assistive technology and meets accessibility standards.
To convert SVG to PDF with PdfSaveOptions
specifying, you should follow a few steps:
- Load an SVG file using one of the SVGDocument() constructors of the SVGDocument class.
- Create a new PdfSaveOptions object and specify save options.
- 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 pathsave_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:
- the
page_setup
property to customize the layout of the PDF pages; - the
css.media_type
property to specify the media type for CSS during the rendering process. In this example, it is set toPRINT
, which means that the CSS rules defined for print media will be applied when generating the PDF; - the
jpeg_quality
property to set the quality of JPEG compression for images within the PDF. It is set to80
, which means that the JPEG images in the PDF will be compressed to 80% of their original quality; - the
is_tagged_pdf
property that enables or disables the creation of a tag structure in the PDF, which is important for accessibility. Setting this property toTrue
ensures that the generated PDF will be tagged, making it more accessible to users who rely on assistive technologies like screen readers.
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.