Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Aspose.HTML for Java converts SVG code, SVG files, URLs, and SVGDocument objects to PDF. For a basic conversion, pass the SVG source,
PdfSaveOptions, and output path to a suitable
Converter.convertSVG() overload.
Converting SVG to PDF is useful when you need a portable, fixed-layout document based on SVG graphics, text, and styling. This article covers inline SVG conversion, file-based conversion, and PDF output settings.
The
Converter.convertSVG() overload used below accepts SVG markup, a base URI, PDF save options, and an output path. The base URI is used to resolve relative resources referenced by the SVG. In this example, "." points to the current directory.
The code converts an inline SVG circle to output.pdf with default PdfSaveOptions.
1// Convert SVG to PDF in a few lines using Java
2
3// Prepare SVG code
4String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
5 "<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
6 "</svg>\n";
7
8// Call the convertSVG() method to convert SVG to PDF
9Converter.convertSVG(code, ".", new PdfSaveOptions(), "output.pdf");Follow these steps to convert an SVG document to PDF:
PdfSaveOptions object. Use the default constructor when no custom output settings are required.Converter.convertSVG() with the document, save options, and output path.The following example writes SVG markup to document.svg, loads it as an SVGDocument, and converts it to output.pdf.
1// Convert SVG to PDF using Java
2
3// Prepare SVG code and save it to a file
4String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
5 "<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
6 "</svg>\n";
7try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.svg")) {
8 fileWriter.write(code);
9}
10
11// Initialize an SVG document from the svg file
12SVGDocument document = new SVGDocument("document.svg");
13
14// Initialize PdfSaveOptions
15PdfSaveOptions options = new PdfSaveOptions();
16
17// Convert SVG to PDF
18Converter.convertSVG(document, options, "output.pdf");The PdfSaveOptions class controls PDF conversion settings. Common methods include:
| Method | Purpose |
|---|---|
setPageSetup(value) | Sets the page size and margins used for PDF output. |
setBackgroundColor(value) | Sets the color used to fill the output page background. |
setHorizontalResolution(value) | Sets the horizontal resolution for rasterized content in pixels per inch. |
setVerticalResolution(value) | Sets the vertical resolution for rasterized content in pixels per inch. |
setJpegQuality(value) | Sets JPEG compression quality for images embedded in the PDF. |
setEncryption(value) | Applies PDF encryption settings. |
To convert SVG to PDF with custom page settings:
PdfSaveOptions and configure the required page or rendering settings.Converter.convertSVG() with the source, configured options, and output path.The next example converts document.svg to output.pdf. It configures a green page background and a page size of 8.3 × 5.8 inches, approximately A5 in landscape orientation.
1// Convert SVG to PDF in Java with custom page settings
2
3// Prepare SVG code and save it to a file
4String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
5 "<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
6 "</svg>\n";
7try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.svg")) {
8 fileWriter.write(code);
9}
10
11// Set A5 as a page-size and change the background color to green
12PdfSaveOptions options = new PdfSaveOptions();
13PageSetup pageSetup = new PageSetup();
14Page anyPage = new Page();
15anyPage.setSize(new Size(Length.fromInches(8.3f), Length.fromInches(5.8f)));
16pageSetup.setAnyPage(anyPage);
17options.setPageSetup(pageSetup);
18options.setBackgroundColor(Color.getGreen());
19
20// Convert SVG to PDF
21Converter.convertSVG("document.svg", options, "output.pdf");Download complete Java examples and data files from GitHub.
You can use the free online SVG to PDF Converter for occasional manual conversions. Aspose.HTML for Java is a commercial API; you can evaluate it before licensing it for production use.
The PDF page dimensions and the SVG viewport do not always match. Configure the page size and margins through PdfSaveOptions and its page setup so the PDF page fits the required layout.
Use the free online SVG to PDF Converter for quick manual conversion without installing software. Use Aspose.HTML for Java when SVG to PDF conversion must run programmatically.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.