Convert SVG to PDF in Java

In this article, you will find information about how to convert an SVG to PDF and how to use PdfSaveOptions and MemoryStreamProvider parameters.

SVG to PDF by a single line of code

The static methods of the Converter class are primarily used as the easiest way to convert an SVG file into various formats. You can convert SVG to PDF in your Java application literally with a single line of code!

1    // Invoke the convertSVG() method for SVG to PDF conversion          
2    com.aspose.html.converters.Converter.convertSVG(Path.combine(getDataDir(), "shapes.svg"), new PdfSaveOptions(), Path.combine(getOutputDir(), "convert-with-single-line.pdf"));

Convert SVG to PDF in Java

If your scenario is required rendering an SVG document, for instance, to PDF file format, the following example demonstrates how that is simple:

  1. Load an SVG file using SVGDocument class. You can load SVG from a file, SVG code, stream, or URL. In the following example we create SVG content from scratch.
  2. Create a new PdfSaveOptions object. Use the empty PdfSaveOptions() constructor to convert with the default save options.
  3. Use one of the сonvertSVG() methods of the Converter class to save SVG as a PDF file. In the example we use the convertHTML(content, baseUri, options, savePath) method that takes four parameters: string with SVG code to be converted, the base folder for the input SVG file, an instance of the PdfSaveOptions class, and the output file path where the converted file will be saved.

Please review the following Java code snippet, which shows the SVG to PDF conversion process with step-by-step instructions:

 1    // Prepare SVG code
 2    String code = StringExtensions.concat("<svg xmlns='http://www.w3.org/2000/svg'>",
 3                "<circle cx ='100' cy ='100' r ='50' fill='none' stroke='red' stroke-width='5' />",
 4                "</svg>");
 5
 6    // Prepare a path for converted file saving 
 7    String savePath = Path.combine(getOutputDir(), "circle.pdf");
 8       
 9    // Initialize PdfSaveOptions class
10    PdfSaveOptions options = new PdfSaveOptions();
11
12    // Convert SVG to PDF
13    com.aspose.html.converters.Converter.convertSVG(code, ".", options, savePath);

Save Options

Aspose.HTML for Java allows converting SVG to PDF using default or custom save options. PdfSaveOptions allows you to customize the rendering process. You can specify the page size, margins, file permissions, CSS media-type, etc.

MetodDescription
setJpegQuality(value)Specifies the quality of JPEG compression for images. The default value is 95.
getCss()Gets a CssOptions object which is used for configuration of CSS properties processing.
setBackgroundColor(value)Sets the color that will fill the background of every page. By default, this property is Transparent.
setPageSetup(value)This method sets a page setup object and uses it for configuration output page-set.
setHorizontalResolution(value)Sets horizontal resolution for internal images, in pixels per inch. By default this property is 300 dpi.
setVerticalResolution(value)Sets vertical resolution for output images in pixels per inch. The default value is 300 dpi.
setEncryptionThis method gets or sets encryption details. If it is not set, then no encryption will be performed.

Please note that you cannot set values against the Application and Producer fields, because Aspose Ltd. and Aspose.HTML for Java x.x.x will be displayed against these fields.

You can download the complete examples and data files from GitHub.

Convert SVG to PDF using PdfSaveOptions

With Aspose.HTML for Java, you can convert files programmatically with full control over a wide range of conversion parameters. 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 the required properties. The PdfSaveOptions() constructor initializes an instance of the PdfSaveOptions class that is passed to converSVG() method:
    • Use the setJpegQuality() method to specify JPEG compression quality.
    • Use the setBackgroundColor() method to set the color that fills the background.
    • Use the setHorizontalResolution() and setVerticalResolution() methods to set horizontal and vertical resolutions for output PDF files.
    • Use the setPageSetup() method to specify the page size and margins for the output document.
  3. Call the сonvertSVG(document, options, savePath) method of the Converter class. The method takes the document, options, output file path savePath and performs the conversion operation.

The following Java example shows how to use PdfSaveOptions and create a PDF file with custom page-size, margins, resolutions, and background color:

 1    // Prepare a path to a source SVG file
 2    String documentPath = Path.combine(getDataDir(), "aspose.svg");
 3
 4    // Prepare a path for converted file saving 
 5    String savePath = Path.combine(getOutputDir(), "aspose-options.pdf");
 6
 7    // Initialize an SVG document from the file
 8    SVGDocument document = new SVGDocument(documentPath);
 9    try {        }
10    finally { if (document != null) document.dispose(); }
11
12    // Initialize PdfSaveOptions. Set up the page-size, margins, resolutions, JpegQuality, and change the background color
13    PdfSaveOptions options = new PdfSaveOptions();
14    options.setHorizontalResolution(new Resolution(200, UnitType.AUTO));
15    options.setVerticalResolution(new Resolution(200, UnitType.AUTO));
16    com.aspose.html.drawing.Color.getGreen().CloneTo(options.getBackgroundColor());
17    options.setJpegQuality(100);
18    options.getPageSetup().setAnyPage(new Page(new com.aspose.html.drawing.Size(500, 500), new Margin(30, 10, 10, 10)));
19
20    // Convert SVG to PDF
21    com.aspose.html.converters.Converter.convertSVG(document, options, savePath);

To learn more about PdfSaveOptions please read Fine-Tuning Converters article.

Aspose.HTML offers a free online SVG to PDF Converter that converts SVG to PDF with high quality, easy and fast. Just upload, convert your files and get the result in a few seconds!

Text “Banner SVG to PDF Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.