SVG to PDF Conversion with the Java API
Aspose.HTML for Java
converts SVG files, markup, URLs, and SVGDocument objects to PDF with
Converter.convertSVG()
. Create PdfSaveOptions, provide the output path, and let the renderer process SVG geometry, text, styles, and referenced resources.
Generate a PDF from an SVG File
The example loads document.svg, creates the default PDF save options, and writes output.pdf. Try-with-resources guarantees cleanup of the SVG document.
Convert SVG to PDF in Java
import com.aspose.html.converters.Converter;
import com.aspose.html.dom.svg.SVGDocument;
import com.aspose.html.saving.PdfSaveOptions;
// Load the SVG document
try (SVGDocument document = new SVGDocument("document.svg")) {
// Create the PDF save options
PdfSaveOptions options = new PdfSaveOptions();
// Convert SVG to PDF
Converter.convertSVG(document, options, "output.pdf");
}
Test SVG to PDF Conversion Online
Upload an SVG file or provide its URL to review the PDF result before adding the conversion workflow to a Java application.
SVG to PDF API Steps
- Add Aspose.HTML for Java to the project.
- Open the source with
SVGDocument. - Create a
PdfSaveOptionsinstance. - Pass the document, options, and destination path to
Converter.convertSVG().
Fit the PDF Page to SVG Content
Use PdfSaveOptions to configure page size and margins, background color, resolution for raster content, JPEG quality, CSS processing, and encryption. Extra space can appear when the PDF page dimensions do not match the SVG viewport. Set page layout according to the required print or distribution format.
See
Convert SVG to PDF in Java
and the
PdfSaveOptions API Reference
.
Install Aspose.HTML for Java
Add Aspose.HTML for Java from the official Aspose repository. Replace ASPOSE_HTML_VERSION with an exact version from the
Aspose.HTML for Java releases
page, then reload or rebuild the project so the dependency is resolved.
Add the repository and dependency to the Maven project:
Maven – pom.xml
<repositories>
<repository>
<id>AsposeJavaAPI</id>
<url>https://releases.aspose.com/java/repo/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-html</artifactId>
<version>ASPOSE_HTML_VERSION</version>
</dependency>
</dependencies>Add the repository and dependency to the Gradle project:
Gradle – build.gradle
repositories {
maven {
url = uri('https://releases.aspose.com/java/repo/')
}
}
dependencies {
implementation 'com.aspose:aspose-html:ASPOSE_HTML_VERSION'
}Use a JDK compatible with the selected release. For manual JAR installation, Kotlin DSL, updates, and dependency troubleshooting, see Install Aspose.HTML for Java .
SVG to PDF Conversion FAQ
1. How do I convert SVG to PDF in Java?
Load the SVG file into an SVGDocument, create PdfSaveOptions, and pass the document, options, and output path to Converter.convertSVG().
2. Why does the PDF have extra space around the SVG graphic?
The SVG viewport and PDF page dimensions can differ. Configure page size and margins through PdfSaveOptions so the fixed PDF page fits the intended SVG layout.
3. Can I protect a PDF created from SVG?
Yes. Configure the encryption settings in PdfSaveOptions before calling Converter.convertSVG() when the output requires passwords or restricted permissions.
4. Can I convert SVG to PDF online for free?
Yes. Use the free online SVG to PDF converter for a manual conversion. Use Aspose.HTML for Java for automated workflows.
Related Java Conversions
Choose another SVG output or a related PDF workflow.