How to Convert SVG to PDF using Java

In this quick tutorial, you will learn to convert SVG to PDF using Java with the help of just a couple of lines of code. It requires loading the SVG file and then saving it on the disk as a PDF. You can change SVG to PDF using Java along with the option to customize the output PDF in a variety of ways.

Steps to Export SVG to PDF using Java

  1. Configure your project to use Aspose.Imaging from the Maven repository
  2. Load the source SVG file using the Image class object
  3. Instantiate the PdfOptions class object to configure the output PDF
  4. Save the image as PDF using the specified PDF options

These steps guide to convert SVG file to PDF using Java by sharing configuration information and then simple to-do list to accomplish the task. First, you have to load the source SVG file and then instantiate the PdfOptions class object that contains options to set page size, PDF core options, and PDF document information to name a very few.

Code to Convert From SVG to PDF using Java

import com.aspose.imaging.Image;
import com.aspose.imaging.License;
import com.aspose.imaging.imageoptions.PdfOptions;
public class ConvertSVGToPDFUsingJava {
public static void main(String[] args) // Main function to convert SVG to PDF using java
{
// Initialize license to avoid watermark in the output PDF created from SVG
License licForSvgToPdf = new License();
licForSvgToPdf.setLicense("Aspose.Imaging.lic");
// Load the input SVG image to be converted to PDF
Image img = Image.load("Circle.svg");
// Instantiate the PdfOptions class object to customize the converted PDF from SVG
PdfOptions options = new PdfOptions();
// Save the output PDF file using the PdfOptions object
img.save("Compressed.pdf",options);
System.out.println("Done");
}
}

This code converts SVG to PDF using Java and provides options to set PDFCoreOptions using the PdfOptions class object. You can set bookmarks outline level, compression, expanded outline levels, heading outline levels, JPEG quality and PDF compliance properties using the PDFCoreOptions. Similarly, you can set the output PDF file author name, keywords, subject, and title using the PdfDocumentInfo property in the PdfOptions class object.

We have learned to convert SVG to PDF here however if you want to convert SVG to PNG, refer to the article on how to convert SVG to PNG using Java.

 English