### Convert SVG to PNG using convert_svg() with default options (Python) Source: https://docs.aspose.com/svg/python-net/convert-svg This Python example demonstrates converting an SVG file to a PNG image using the `convert_svg()` method from the Aspose.SVG library. It utilizes default conversion options provided by `ImageSaveOptions` and saves the output to 'image.png'. ```python from aspose.svg import SVGDocument from aspose.svg.converters import Converter from aspose.svg.saving import ImageSaveOptions options = ImageSaveOptions() with SVGDocument("image.svg") as document: # Convert SVG to PNG Converter.convert_svg(document, options, "image.png") ``` -------------------------------- ### Create and Save SVG Document with Aspose.SVG for Python via .NET Source: https://docs.aspose.com/svg/python-net/installation This Python code snippet demonstrates how to create a new SVG document, add a circle element with specified attributes, and save the document to a file using the Aspose.SVG for Python via .NET library. Ensure the 'aspose-svg-net' package is installed. ```python from aspose.svg import SVGDocument # Create a new SVG document document = SVGDocument() svg_element = document.document_element # Add an SVG element circle = document.create_element_ns("http://www.w3.org/2000/svg", "circle") circle.set_attribute("cx", "50") circle.set_attribute("cy", "50") circle.set_attribute("r", "40") circle.set_attribute("stroke", "black") circle.set_attribute("stroke-width", "3") circle.set_attribute("fill", "red") svg_element.append_child(circle) # Save the document document.save("circle.svg") ``` -------------------------------- ### Convert SVG to PNG with Custom Save Options in Python Source: https://docs.aspose.com/svg/python-net/convert-svg-to-png This Python example demonstrates converting an SVG file to a PNG image using Aspose.SVG. It utilizes the ImageSaveOptions class to customize the output, setting a specific background color, page size, margins, and resolution. The code first sets up input and output paths, then initializes ImageSaveOptions with desired properties, opens the SVG document, and finally converts it to PNG. ```python import os from aspose.svg import SVGDocument from aspose.svg.converters import Converter from aspose.svg.saving import ImageSaveOptions from aspose.svg.drawing import Resolution, Page, Size, Margin from aspose.pydrawing import Color # Initialize an SVG document from a file input_folder = "data/" output_folder = "output/" src_file = os.path.join(input_folder, "winter.svg") output_file = os.path.join(output_folder, "winter.png") if not os.path.exists(output_folder): os.makedirs(output_folder) options = ImageSaveOptions() options.background_color = Color.from_argb(231, 217, 230) options.page_setup.any_page = Page(Size(600, 550), Margin(10, 10, 10, 10)) options.horizontal_resolution = Resolution.from_dots_per_inch(96.0) options.vertical_resolution = Resolution.from_dots_per_inch(96.0) with SVGDocument(src_file) as document: # Convert SVG to PNG Converter.convert_svg(document, options, output_file) ``` -------------------------------- ### Convert SVG to Various Image Formats (Python) Source: https://docs.aspose.com/svg/python-net/convert-svg-to-png This snippet demonstrates how to convert an SVG document to different image formats (PNG, BMP, JPEG, GIF, TIFF, PDF, XPS) using the Aspose.SVG API. It dynamically imports necessary save options based on the desired output format and converts the SVG to a specified output file. Ensure the Aspose.SVG library is installed. ```python from aspose.svg import SVGDocument from aspose.svg.converters import Converter {{#if_output 'PNG' 'BMP' 'JPEG' 'GIF' 'TIFF'}} from aspose.svg.saving import ImageSaveOptions {{/if_output}} {{#if_output 'PDF'}} from aspose.svg.saving import PdfSaveOptions {{/if_output}} {{#if_output 'XPS'}} from aspose.svg.saving import XpsSaveOptions {{/if_output}} # Load an SVG document from a file with SVGDocument("{{input lower}}") as document: # Initialize saving options {{#if_output 'PDF'}} options = PdfSaveOptions() {{/if_output}} {{#if_output 'XPS'}} options = XpsSaveOptions() {{/if_output}} {{#if_output 'PNG'}} options = ImageSaveOptions() {{/if_output}} {{#if_output 'BMP' 'JPEG' 'GIF' 'TIFF'}} options = ImageSaveOptions() options.format.{{output upper}} {{/if_output}} # Convert the SVG document to {{output upper}} Converter.convert_svg(document, options, "result.{{output lower}}") ``` -------------------------------- ### Convert SVG to JPG using render_to() in Python Source: https://docs.aspose.com/svg/python-net/convert-svg-to-image This snippet demonstrates converting an SVG document to JPG format using Aspose.SVG for Python. It initializes an SVG document, sets image rendering options including format, resolution, and page setup, and then renders the document to a JPG file using an ImageDevice. ```python import os from aspose.svg import SVGDocument from aspose.svg.drawing import Resolution, Page, Size, Margin from aspose.svg.rendering.image import ImageRenderingOptions, ImageFormat, ImageDevice # Initialize an SVG document from a file input_folder = "data/" output_folder = "output/" src_file = os.path.join(input_folder, "image.svg") output_file = os.path.join(output_folder, "image.jpg") if not os.path.exists(output_folder): os.makedirs(output_folder) with SVGDocument(src_file) as document: # Initialize an instance of the ImageRenderingOptions class and set custom properties image_rendering_options = ImageRenderingOptions() image_rendering_options.format = ImageFormat.JPEG image_rendering_options.horizontal_resolution = Resolution.from_dots_per_inch(96.0) image_rendering_options.vertical_resolution = Resolution.from_dots_per_inch(96.0) image_rendering_options.page_setup.any_page = Page(Size(600, 600), Margin(10, 10, 10, 10)) # Initialize an instance of the ImageDevice class with ImageDevice(image_rendering_options, output_file) as device: # Render SVG to JPG and send the document to the rendering device document.render_to(device) ``` -------------------------------- ### Edit SVG Document with Python using Aspose.SVG Source: https://docs.aspose.com/svg/python-net/edit-svg-file This Python code snippet demonstrates how to load an SVG file, add a rectangular background, insert circle elements styled as stars, and change the fill color of a polyline element. It utilizes Aspose.SVG for Python, requiring the library to be installed. The function takes an input SVG file and saves the modified version. ```python from aspose.svg import SVGDocument # Create a new SVG document document = SVGDocument("сhristmas-tree.svg") svg_element = document.root_element # Add a element as a background rect = document.create_element_ns("http://www.w3.org/2000/svg", "rect") rect.set_attribute("x", "10") rect.set_attribute("y", "10") rect.set_attribute("width", "400") rect.set_attribute("height", "400") rect.set_attribute("fill", "#2a065b") svg_element.insert_before(rect, svg_element.first_child) # Add a circle element stylized as a star circle = document.create_element_ns("http://www.w3.org/2000/svg", "circle") circle.set_attribute("id", "star") circle.set_attribute("cx", "60") circle.set_attribute("cy", "50") circle.set_attribute("r", "5") circle.set_attribute("stroke", "white") circle.set_attribute("stroke-width", "4") circle.set_attribute("stroke-dasharray", "1 3") circle.set_attribute("fill", "none") svg_element.append_child(circle) # Create and add new stars use = document.create_element_ns("http://www.w3.org/2000/svg", "use") use.set_attribute("href", "#star") use.set_attribute("transform", "translate(40, 70)") svg_element.append_child(use) # You can add as many stars as you wish # Get polyline element to change tree color polyline_element = svg_element.query_selector("polyline") # Set a new "fill" attribute value for the polyline element polyline_element.set_attribute("fill", "#039da7") polyline_element.set_attribute("stroke", "white") polyline_element.set_attribute("stroke-width", "3") # Save the SVG document to a file document.save("сhristmas-tree-edited.svg") ``` -------------------------------- ### Get Root SVG Element using root_element Source: https://docs.aspose.com/svg/python-net/edit-svg-file Obtains the root '' element of an SVG document through the `root_element` property. This is an alternative method to access the primary SVG container. It requires the SVGDocument class. ```python # Create a new SVG document document = SVGDocument() # Get root element of the document svg_element = document.root_element ``` -------------------------------- ### Apply License from File - Aspose.SVG Python via .NET Source: https://docs.aspose.com/svg/python-net/licensing Demonstrates how to apply a license for Aspose.SVG for Python via .NET by specifying the path to the license file. The license can be placed in the same folder as the Aspose.SVG.dll or in a subfolder. ```python from aspose.svg import License # Initialize license object lic = License() # Set license from file lic.set_license("./license/Aspose.SVG.Python.via.NET.lic") print("License set successfully.") ``` -------------------------------- ### Get Root SVG Element using document_element Source: https://docs.aspose.com/svg/python-net/edit-svg-file Retrieves the root '' element of an SVG document using the `document_element` property. This provides direct access to the main SVG container. No external dependencies are required beyond the SVGDocument class. ```python # Create a new SVG document document = SVGDocument() # Get root element of the document svg_element = document.document_element ``` -------------------------------- ### Apply Metered License - Aspose.SVG Python via .NET Source: https://docs.aspose.com/svg/python-net/licensing Shows how to apply a metered license for Aspose.SVG for Python via .NET using public and private keys. This method is for usage-based billing and requires obtaining keys from Aspose. ```python from aspose.svg import Metered # Create an instance of the Metered class metered = Metered() # Set the public and private keys for metered licensing public_key = "your-public-key" private_key = "your-private-key" # Apply the metered license metered.set_metered_key(public_key, private_key) ``` -------------------------------- ### Convert SVG to PDF using render_to() method Source: https://docs.aspose.com/svg/python-net/convert-svg This code snippet demonstrates how to convert an SVG file to a PDF document using the Aspose.SVG library. It involves initializing an SVG document, configuring PDF rendering options (like JPEG quality), creating a PDF device, and then rendering the SVG to the specified output file. Ensure the 'data' and 'output' folders exist and the input SVG file is present. ```python import os from aspose.svg import SVGDocument from aspose.svg.rendering.pdf import PdfRenderingOptions, PdfDevice # Initialize an SVG document from a file input_folder = "data/" output_folder = "output/" src_file = os.path.join(input_folder, "document.svg") output_file = os.path.join(output_folder, "document.pdf") if not os.path.exists(output_folder): os.makedirs(output_folder) with SVGDocument(src_file) as document: # Initialize an instance of the PdfRenderingOptions class and set custom jpeg_quality properties pdf_rendering_options = PdfRenderingOptions() pdf_rendering_options.jpeg_quality = 10 # Initialize an instance of the PdfDevice class with PdfDevice(pdf_rendering_options, output_file) as device: # Render SVG to PDF and send the document to the rendering device document.render_to(device) ``` -------------------------------- ### PdfSaveOptions Class Overview Source: https://docs.aspose.com/svg/python-net/convert-svg-to-pdf The PdfSaveOptions class allows customization of PDF output when converting SVG files. It provides properties to control image quality, CSS processing, document metadata, background color, page setup, resolution, encryption, and form field behavior. ```APIDOC ## PdfSaveOptions Class ### Description This class specifies various options for saving SVG documents as PDF files, allowing customization of the PDF output. ### Properties - **jpeg_quality** (int) - Sets the quality level for JPEG images within the PDF. Default is 95. - **css** (CssOptions) - Configures the processing of CSS properties. - **document_info** (DocumentInfo) - Contains metadata for the output PDF document (title, author, subject, keywords). - **background_color** (Color) - Sets the background color for each page. Defaults to Transparent. - **page_setup** (PageSetup) - Configures the output page setup. - **horizontal_resolution** (float) - Sets the horizontal resolution in pixels per inch. Default is 300 dpi. - **vertical_resolution** (float) - Sets the vertical resolution in pixels per inch. Default is 300 dpi. - **encryption** (EncryptionDetails) - Gets or sets encryption details for the PDF. If not set, no encryption is performed. - **form_field_behaviour** (FormFieldBehaviour) - Specifies the behavior of form fields in the output PDF document. **Note**: Options implementing PdfSaveOptions inherit from PdfRenderingOptions. ### Usage Example ```python import os from aspose.svg import SVGDocument from aspose.svg.converters import Converter from aspose.svg.saving import PdfSaveOptions from aspose.svg.rendering import SizingType from aspose.svg.drawing import Resolution from aspose.pydrawing import Color # Initialize an SVG document from a file input_folder = "data/" output_folder = "output/" src_file = os.path.join(input_folder, "document.svg") output_file = os.path.join(output_folder, "document.pdf") if not os.path.exists(output_folder): os.makedirs(output_folder) with SVGDocument(src_file) as document: options = PdfSaveOptions() options.background_color = Color.from_argb(0, 255, 255, 255) # White background options.page_setup.sizing = SizingType.FIT_CONTENT options.horizontal_resolution = Resolution.from_dots_per_inch(96.0) options.vertical_resolution = Resolution.from_dots_per_inch(96.0) options.jpeg_quality = 80 # Convert SVG to PDF Converter.convert_svg(document, options, output_file) ``` ``` -------------------------------- ### Save SVG with Resources to Local Storage using Python Source: https://docs.aspose.com/svg/python-net/save-svg-file Demonstrates saving an SVG document along with its linked resources (like CSS, images) to local file system storage. It utilizes the `FileSystemResourceHandler` to manage resource saving. The code shows saving to a directory and also explicitly saving to a file with resources. ```python import os from aspose.svg import SVGDocument from aspose.svg.saving.resourcehandlers import FileSystemResourceHandler # Setup directories data_dir = "data/" output_dir = os.path.abspath(os.path.join(os.getcwd(), "../tests-out/save/")) if not os.path.exists(output_dir): os.makedirs(output_dir) # Prepare a path to the source SVG file input_path = os.path.join(data_dir, "with-resources.svg") # Load the SVG document from a file with SVGDocument(input_path) as doc: # Save SVG with resources doc.save(FileSystemResourceHandler(output_dir)) print(f"SVG document and resources saved to {output_dir}") # Setup directories data_dir = "data/" output_dir = os.path.abspath(os.path.join(os.getcwd(), "../tests-out/save/")) if not os.path.exists(output_dir): os.makedirs(output_dir) input_path = os.path.join(data_dir, "with-resources.svg") output_file = os.path.join(output_dir, "with-resources.svg") # Open the SVG file as stream with open(input_path, "rb") as file_stream: with SVGDocument(file_stream, ".") as doc: # Save the SVG file and resources explicitly doc.save(output_file, FileSystemResourceHandler(output_dir)) ``` -------------------------------- ### Convert SVG to PDF with Custom Options - Python Source: https://docs.aspose.com/svg/python-net/convert-svg-to-pdf This Python code demonstrates converting an SVG file to a PDF document using Aspose.SVG. It initializes an SVG document, configures PDF rendering options like JPEG quality and page sizing, and then renders the SVG to a specified output PDF file. ```python import os from aspose.svg import SVGDocument from aspose.svg.rendering import SizingType from aspose.svg.rendering.pdf import PdfRenderingOptions, PdfDevice # Initialize an SVG document from a file input_folder = "data/" output_folder = "output/" src_file = os.path.join(input_folder, "document.svg") output_file = os.path.join(output_folder, "document.pdf") if not os.path.exists(output_folder): os.makedirs(output_folder) with SVGDocument(src_file) as document: # Initialize an instance of the PdfRenderingOptions class and set custom peg_quality and page_setup properties pdf_rendering_options = PdfRenderingOptions() pdf_rendering_options.jpeg_quality = 10 pdf_rendering_options.page_setup.sizing = SizingType.FIT_CONTENT # Initialize an instance of the PdfDevice class with PdfDevice(pdf_rendering_options, output_file) as device: # Render SVG to PDF and send the document to the rendering device document.render_to(device) ``` -------------------------------- ### Create Empty SVG Document - Python Source: https://docs.aspose.com/svg/python-net/create-svg-file Demonstrates how to create an empty SVG document using the default SVGDocument() constructor and save it to a file. It also shows how to set attributes like width and height for the root SVG element. ```python from aspose.svg import SVGDocument # Create a new SVG document document = SVGDocument() # Save the empty SVG document document.save("empty-document.svg") ``` ```python import os from aspose.svg import SVGDocument # Set up the output directory output_folder = "output/" if not os.path.exists(output_folder): os.makedirs(output_folder) # Create a new empty SVG document document = SVGDocument() # Optionally, you can add attributes to the root element svg_element = document.document_element svg_element.set_attribute("width", "100%") svg_element.set_attribute("height", "100%") # Define the output file path output_file = os.path.join(output_folder, "create-empty.svg") # Save the SVG document to a file document.save(output_file) print(f"Empty SVG document saved to {output_file}") ``` -------------------------------- ### Edit SVG Element Attributes using CSS Selector in Python Source: https://docs.aspose.com/svg/python-net/navigate-svg Shows how to load an SVG document and modify an element's attributes using CSS selectors. The example uses `query_selector` to find a 'circle' element and then sets its 'stroke' attribute. This functionality is valuable for dynamic styling and manipulation of SVG content. ```python import os from aspose.svg import SVGDocument # Setup directories output_dir = "output/" data_dir = "data/" document_path = os.path.join(data_dir, "shapes.svg") # Load an SVG document from the file document = SVGDocument(document_path) # Get root svg element of the document svg_element = document.root_element # Get circle element to change color circle_element = svg_element.query_selector("circle") # Set a new "fill" attribute value for the circle element circle_element.set_attribute("stroke", "DarkCyan") # Save the SVG document to a file output_path = os.path.join(output_dir, "circle-color.svg") document.save(output_path) ``` -------------------------------- ### Python: Vectorize Photo with Custom Settings Source: https://docs.aspose.com/svg/python-net/vectorize-images This Python code snippet demonstrates how to vectorize a JPG photo into an SVG file using Aspose.SVG for Python via .NET. It configures vectorization options such as trace smoother, error threshold, max iterations, and color limits to achieve a desired artistic effect. The output SVG is saved to a specified directory. ```python import os from aspose.svg.imagevectorization import ImageVectorizer, ImageTraceSmoother, BezierPathBuilder # Setup directories input_folder = "data/" output_folder = "output/" src_file = "lioness.jpg" output_file = "lioness.svg" if not os.path.exists(output_folder): os.makedirs(output_folder) # Configuration for vectorization path_builder = BezierPathBuilder() path_builder.trace_smoother = ImageTraceSmoother(1) path_builder.error_threshold = 30.0 path_builder.max_iterations = 30 vectorizer = ImageVectorizer() vectorizer.configuration.path_builder = path_builder vectorizer.configuration.colors_limit = 25 vectorizer.configuration.line_width = 1.5 # Vectorize a photo with vectorizer.vectorize(os.path.join(input_folder, src_file)) as document: output_file = os.path.join(output_folder, output_file) document.save(output_file) ``` -------------------------------- ### Create New SVG Element using create_element_ns Source: https://docs.aspose.com/svg/python-net/edit-svg-file Creates a new SVG element, such as a '', within a specified namespace using the `create_element_ns` method. This method requires the namespace URI ('http://www.w3.org/2000/svg') and the qualified name of the element. It returns an instance of the Element class. ```python # Create a new SVG document document = SVGDocument() svg_element = document.document_element # Create a new SVG element circle = document.create_element_ns("http://www.w3.org/2000/svg", "circle") ``` -------------------------------- ### Convert SVG to PDF (Python) Source: https://docs.aspose.com/svg/python-net/convert-svg-to-pdf This code demonstrates how to convert an SVG document to a PDF file using Aspose.SVG for Python. It loads an SVG from a file, initializes PdfSaveOptions with default settings, and then uses the Converter.convert_svg method to perform the conversion. ```python from aspose.svg import SVGDocument from aspose.svg.converters import Converter from aspose.svg.saving import PdfSaveOptions # Load an SVG document from a file with SVGDocument("document.svg") as document: # Initialize saving options for PDF options = PdfSaveOptions() # Convert the SVG document to PDF Converter.convert_svg(document, options, "result.pdf") ``` -------------------------------- ### Load SVG from URL - Python Source: https://docs.aspose.com/svg/python-net/create-svg-file Illustrates how to create an SVG document by loading an SVG file directly from a URL. The Aspose.SVG library handles fetching the content from the specified web address. ```python from aspose.svg import SVGDocument # Load SVG from a URL document = SVGDocument("https://docs.aspose.com/svg/files/owl.svg") # Work with the SVG document here... # Save the document document.save("load-svg-from-url.svg") ``` -------------------------------- ### Load SVG from File - Python Source: https://docs.aspose.com/svg/python-net/create-svg-file Demonstrates how to load an existing SVG file into an SVGDocument object by providing the file path to the SVGDocument constructor. The loaded document can then be manipulated or saved. ```python from aspose.svg import SVGDocument # Create a new SVG document document = SVGDocument("document.svg") # Work with the SVG document here... # Save the document document.save("load-svg-from-file.svg") ``` -------------------------------- ### Create Image Stencil using Aspose.SVG in Python Source: https://docs.aspose.com/svg/python-net/image-stencil This code snippet demonstrates how to convert a PNG image into a stencil using the Aspose.SVG Python library. It involves configuring BezierPathBuilder for vectorization, setting up ImageVectorizer with stencil configurations, and saving the resulting SVG document. Ensure the 'data/' folder contains the input image and the 'output/' folder is available for saving the result. ```python import os from aspose.svg.drawing import Color from aspose.svg.imagevectorization import BezierPathBuilder, ImageTraceSmoother, ImageVectorizer, StencilConfiguration, StencilType # Setup directories input_folder = "data/" output_folder = "output/" if not os.path.exists(output_folder): os.makedirs(output_folder) # Configuration for image vectorization path_builder = BezierPathBuilder() path_builder.trace_smoother = ImageTraceSmoother(2) path_builder.error_threshold = 20.0 path_builder.max_iterations = 10 vectorizer = ImageVectorizer() vectorizer.configuration.path_builder = path_builder vectorizer.configuration.colors_limit = 5 vectorizer.configuration.line_width = 1.0 # Configuration for image stencil stencil_config = StencilConfiguration() stencil_config.type = StencilType.MONO_COLOR stencil_config.color = Color.from_rgb(0, 0, 255) vectorizer.configuration.stencil = stencil_config # Vectorize an image src_file = "image.png" with vectorizer.vectorize(os.path.join(input_folder, src_file)) as document: output_file = os.path.join(output_folder, "image-stencil.svg") document.save(output_file) ``` -------------------------------- ### Save SVG Document to a File using Python Source: https://docs.aspose.com/svg/python-net/save-svg-file Demonstrates how to save an SVG document to a specified file path using the `SVGDocument.save()` method. It handles directory creation and loading an existing SVG file. No external dependencies beyond the Aspose.SVG library are required. ```python import os from aspose.svg import SVGDocument # Prepare a path to the source and output SVG file data_dir = "data/" output_dir = "output/" input_path = os.path.join(data_dir, "with-resources.svg") output_path = os.path.join(output_dir, "modified_example.svg") if not os.path.exists(output_dir): os.makedirs(output_dir) # Load the existing SVG document from a file with SVGDocument(input_path) as document: # Work with the document here # Save the modified SVG document to a file document.save(output_path) ``` -------------------------------- ### Convert SVG to XPS (Python) Source: https://docs.aspose.com/svg/python-net/convert-svg-to-pdf This snippet illustrates how to convert an SVG document to an XPS file using Aspose.SVG for Python. It involves loading the SVG, setting up XpsSaveOptions, and utilizing the Converter.convert_svg method for the conversion process. ```python from aspose.svg import SVGDocument from aspose.svg.converters import Converter from aspose.svg.saving import XpsSaveOptions # Load an SVG document from a file with SVGDocument("input.svg") as document: # Initialize saving options for XPS options = XpsSaveOptions() # Convert the SVG document to XPS Converter.convert_svg(document, options, "result.xps") ``` -------------------------------- ### Convert SVG to Various Image and Document Formats (Python) Source: https://docs.aspose.com/svg/python-net/convert-svg This snippet demonstrates how to convert an SVG file to various formats such as PNG, BMP, JPEG, GIF, TIFF, PDF, and XPS using Aspose.SVG for Python. It dynamically imports necessary save options based on the desired output format and utilizes the Converter.convert_svg method. The input SVG can be loaded from a local file path. ```python from aspose.svg import SVGDocument from aspose.svg.converters import Converter from aspose.svg.saving import ImageSaveOptions, PdfSaveOptions, XpsSaveOptions # Load an SVG document from a file with SVGDocument("input.svg") as document: # Initialize saving options based on output format # Example for PDF options = PdfSaveOptions() # Example for PNG # options = ImageSaveOptions() # Example for XPS # options = XpsSaveOptions() # Convert the SVG document to a specified format (e.g., PDF) Converter.convert_svg(document, options, "result.pdf") ``` -------------------------------- ### Create SVG from Memory String - Python Source: https://docs.aspose.com/svg/python-net/create-svg-file Shows how to create an SVG document directly from a string containing SVG markup. This is useful for generating SVG content dynamically within your code without needing to save it to a file first. ```python from aspose.svg import SVGDocument documentContent = "" # Create a new SVG document document = SVGDocument(documentContent, ".") svg_element = document.document_element # Work with the document here... # Save the document document.save("create-svg-from-string.svg") ``` -------------------------------- ### Convert SVG to PNG using convert_svg() (Python) Source: https://docs.aspose.com/svg/python-net/convert-svg This Python code snippet shows a basic conversion of an SVG file to a PNG image using the Aspose.SVG library. It loads an SVG document from 'document.svg' and uses the Converter.convert_svg method with default ImageSaveOptions to save the output as 'result.png'. ```python from aspose.svg import SVGDocument from aspose.svg.converters import Converter from aspose.svg.saving import ImageSaveOptions # Load an SVG document from a file with SVGDocument("document.svg") as document: # Initialize saving options options = ImageSaveOptions() # Convert the SVG document to PNG Converter.convert_svg(document, options, "result.png") ``` -------------------------------- ### Convert SVG to PDF with Custom Save Options in Python Source: https://docs.aspose.com/svg/python-net/convert-svg-to-pdf This Python code snippet demonstrates how to convert an SVG file to a PDF document using Aspose.SVG for Python via .NET. It utilizes the PdfSaveOptions class to customize various aspects of the PDF output, including background color, page sizing, resolution, and JPEG quality. Ensure the 'data/' folder contains your input SVG file and the 'output/' folder is available for the generated PDF. ```python import os from aspose.svg import SVGDocument from aspose.svg.converters import Converter from aspose.svg.saving import PdfSaveOptions from aspose.svg.rendering import SizingType from aspose.svg.drawing import Resolution from aspose.pydrawing import Color # Initialize an SVG document from a file input_folder = "data/" output_folder = "output/" src_file = os.path.join(input_folder, "document.svg") output_file = os.path.join(output_folder, "document.pdf") if not os.path.exists(output_folder): os.makedirs(output_folder) with SVGDocument(src_file) as document: options = PdfSaveOptions() options.background_color = Color.from_argb(0, 255, 255, 255) options.page_setup.sizing = SizingType.FIT_CONTENT options.horizontal_resolution = Resolution.from_dots_per_inch(96.0) options.vertical_resolution = Resolution.from_dots_per_inch(96.0) options.jpeg_quality = 80 # Convert SVG to PDF Converter.convert_svg(document, options, output_file) ```