用 Java 将 SVG 转换为 PDF

本文将介绍如何将 SVG 转换为 PDF 以及如何使用 PdfSaveOptions

用几行代码将 SVG 转换为 PDF

Converter 类的静态方法主要用于将 SVG 文件转换为各种格式的最简单方法。只需一行代码,您就可以在 Java 应用程序中将 SVG 转换为 PDF

在示例中,我们使用了 convertSVG(content, baseUri, options, savePath) 方法,该方法需要四个参数:包含要转换的 SVG 代码的字符串、输入 SVG 文件的基本文件夹、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");

用 Java 将 SVG 转换为 PDF

例如,如果您需要将 SVG 文档渲染为 PDF 文件格式,下面的示例演示了如何将其渲染为 PDF 文件格式:

  1. 使用 SVGDocument 类加载 SVG 文件。您可以从文件、SVG 代码、流或 URL 加载 SVG。在下面的示例中,我们将从头开始创建 SVG 内容。
  2. 创建一个新的 PdfSaveOptions 对象。使用空的 PdfSaveOptions() 构造函数转换默认保存选项。
  3. 使用 Converter 类的 сonvertSVG() 方法之一将 SVG 保存为 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");

保存选项 – PdfSaveOptions 类

Aspose.HTML for Java 允许使用默认或自定义保存选项将 SVG 转换为 PDF。通过 PdfSaveOptions,您可以自定义渲染过程。您可以指定 page sizemarginsfile permissionsmedia type等。

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.

您可以从 GitHub 下载完整的示例和数据文件。

使用 PdfSaveOptions 将 SVG 转换为 PDF

使用 Aspose.HTML for Java,您可以通过编程转换文件,并完全控制各种转换参数。要通过指定 PdfSaveOptions将 SVG 转换为 PDF,您需要遵循以下几个步骤:

  1. 使用 SVGDocument 类的 SVGDocument() 构造函数之一加载 SVG 文件。
  2. 创建一个新的 PdfSaveOptions对象,并指定所需的属性。PdfSaveOptions() 构造函数初始化了一个PdfSaveOptions类的实例,并将其传递给 converSVG() 方法。
  3. 调用 Converter 类的 сonvertSVG(sourcePath,options,savePath)方法。该方法接收文档 sourcePathoptions、输出文件路径 savePath 并执行转换操作。

下面的 Java 示例展示了如何使用 PdfSaveOptions,创建具有自定义页面大小和背景颜色的 PDF 文件:

 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");

要了解有关 PdfSaveOptions 的更多信息,请阅读 Fine-Tuning Converters 一文。

Aspose.HTML 提供免费的在线 SVG 到 PDF 转换器,可将 SVG 转换为高质量的 PDF,简单快捷。只需上传、转换您的文件,几秒钟就能得到结果!

文本 “SVG 到 PDF 转换器”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.