Convert SVG to PDF in Java – Aspose.HTML for Java

In this article, you will find information on how to convert SVG to PDF and how to use PdfSaveOptions.

SVG to PDF with a few lines 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!

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.

1// Prepare an SVG code.
2String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
3        "<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
4        "</svg>\n";
5
6// Call the ConvertSVG method to convert the SVG code to PDF.
7Converter.convertSVG(code, ".", new PdfSaveOptions(), "output.pdf");

Convert SVG to PDF in Java

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

  1. Load an SVG file using the 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.
 1// Prepare an SVG code and save it to the file.
 2String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
 3        "<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
 4        "</svg>\n";
 5try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.svg")) {
 6    fileWriter.write(code);
 7}
 8
 9// Initialize an SVG document from the svg file.
10SVGDocument document = new SVGDocument("document.svg");
11
12// Initialize PdfSaveOptions.
13PdfSaveOptions options = new PdfSaveOptions();
14
15// Convert SVG to PDF
16Converter.convertSVG(document, options, "output.pdf");

Save Options – PdfSaveOptions Class

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, 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.

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.
  3. Call the сonvertSVG(sourcePath, options, savePath) method of the Converter class. The method takes the document sourcePath, 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 and background color:

 1// Prepare an SVG code and save it to the file.
 2String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
 3        "<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
 4        "</svg>\n";
 5try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.svg")) {
 6    fileWriter.write(code);
 7}
 8
 9// Set A5 as a page-size and change the background color to green
10PdfSaveOptions options = new PdfSaveOptions();
11PageSetup pageSetup = new PageSetup();
12Page anyPage = new Page();
13anyPage.setSize(new Size(Length.fromInches(8.3f), Length.fromInches(5.8f)));
14pageSetup.setAnyPage(anyPage);
15options.setPageSetup(pageSetup);
16options.setBackgroundColor(Color.getGreen());
17
18// Convert SVG document to PDF
19Converter.convertSVG("document.svg", options, "output.pdf");

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 “SVG to PDF Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.