将SVG转换为PDF [英] Convert SVG to PDF

查看:1502
本文介绍了将SVG转换为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式将SVG文件转换为PDF? (我需要在生成PDF之前在某些方面更改SVG,因此使用工具进行预转换是不够的。)

How would one go about converting a SVG file to a PDF programatically? (I need to alter the SVG in certain respects before generating the PDF so simply pre-converting it using a tool won't be sufficient.)

理想情况下使用Java而不是Perl或者PHP也没关系。

Ideally using Java but Perl or PHP would be fine too.

显然我基本上在考虑Apache FOP和Batik with Java。但无论我搜索多久,我都找不到如何做到的简单介绍。像 SVGConverter 这样的内容有类似定义能够转换部分或全部GraphicContext的类的接口,但我真的不知道这意味着什么。

Obviously I am basically considering Apache FOP and Batik with Java. However no matter how long I search I cannot find a simple introduction on how to do it. Things like SVGConverter have descriptions like "Defines the interface for classes that are able to convert part or all of a GraphicContext", but I don't really know what that means.

我有这种感觉必须有一个由FOP或Batik提供的API非常简单,但我现在无法找到它(或者它可能确实不存在。)

I have this feeling there must be an API to do this quite simply, provided by FOP or Batik, but I'm just not able to find it at the moment (or perhaps it really doesn't exist.)

就我所需的支持的SVG功能而言,该文件有一些路径填充了一些线性渐变。

In terms of the supported SVG features I need, the file has some paths which are filled with some linear gradients.

理想情况下,如果我可以将SVG作为理想的DOM文档传递;然后我会加载我的模板SVG文件,按照用户的指定进行更改,然后生成PDF。

Ideally if I could pass the SVG in as a DOM Document that would be ideal; then I would load my template SVG file, change it as specified by the user, and then generate the PDF.

推荐答案

感谢Adrian展示了如何使用Batik光栅化器API。但是,我需要一个更轻量级的解决方案---我不能写入临时文件,我想要更少的依赖项。因此,从他指出的方法开始,我找到了一种方法来访问较低级别的代码来进行转换,而不是别的。

Thanks to Adrian for showing how the Batik rasterizer API is supposed to be used. However, I needed a more lightweight solution--- I can't write to temporary files, and I want fewer dependencies. So, starting from the methods he pointed to, I found a way to access the lower-level code to do the conversion and nothing else.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import org.apache.batik.transcoder.Transcoder;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.fop.svg.PDFTranscoder;

public class Test {
    public static void main(String[] argv) throws TranscoderException, FileNotFoundException {
        Transcoder transcoder = new PDFTranscoder();
        TranscoderInput transcoderInput = new TranscoderInput(new FileInputStream(new File("/tmp/test.svg")));
        TranscoderOutput transcoderOutput = new TranscoderOutput(new FileOutputStream(new File("/tmp/test.pdf")));
        transcoder.transcode(transcoderInput, transcoderOutput);
    }
}

编译运行命令

javac -cp batik-rasterizer.jar -d build Test.java
java -cp build:batik-rasterizer.jar Test

重点是 TranscoderInput TranscoderOutput 可以使用任何 InputStream OutputStream ,而不仅仅是文件流。请注意,其中一个构造函数需要 org。 w3c.dom.Document ,这意味着您甚至不需要将SVG DOM序列化为SVG字符串,从而节省了额外的步骤。

The important point is that TranscoderInput and TranscoderOutput can work with any InputStream and OutputStream, not just file streams. Note that one of the constructors takes a org.w3c.dom.Document, which means that you don't even need to serialize an SVG DOM into an SVG string, saving an additional step.

与高级API不同,此版本也不会向stdout / stderr写入任何内容。

This version also doesn't write anything to stdout/stderr, unlike the high-level API.

对于JPEG,PNG或TIFF输出,用 org.apache.batik.transcoder.image.JPEGTranscoder 替换 org.apache.fop.svg.PDFTranscoder PNGTranscoder TIFFTranscoder (请注意,这些栅格格式位于不同的包中)。

For JPEG, PNG, or TIFF output, replace org.apache.fop.svg.PDFTranscoder with org.apache.batik.transcoder.image.JPEGTranscoder, PNGTranscoder, or TIFFTranscoder (note that these raster formats are in a different package).

(我不太确定Java如何找到 org.apache.batk.transcoder。* org.apache .fop.svg.PDFTranscoder 类,因为我在 batik-rasterizer.jar 中没有看到它们。)

(I'm not quite sure how Java finds the org.apache.batk.transcoder.* and org.apache.fop.svg.PDFTranscoder classes, since I don't see them in the batik-rasterizer.jar.)

修改:

虽然简单的命令行编译工作正常仅使用 batik-rasterizer.jar ,它正在进行某种类加载器魔术以找到所有必需的类。在更现实的情况下(使用Ant构建项目),您必须手动查找类。它们可以在 Batik项目中的 batik-1.7.zip 中找到来自 FOP项目的/ a>和 fop-1.1.zip 。从Batik,您需要使用 batik-transcoder.jar 进行编译并使用

Although the simple commandline-compilation works with the batik-rasterizer.jar only, it's doing some sort of classloader magic to find all the necessary classes. In a more realistic case (building a project with Ant), you have to find the classes by hand. They can be found in batik-1.7.zip from the Batik project and fop-1.1.zip from the FOP project. From Batik, you need to compile with batik-transcoder.jar and run with


    batik-transcoder.jar
  • batik-anim.jar

  • batik-awt-util.jar

  • batik-bridge。 jar

  • batik-css.jar

  • batik-dom.jar

  • batik-ext.jar

  • batik-gvt.jar

  • batik-parser.jar

  • batik-script.jar

  • 蜡染-svg-dom.jar

  • batik-util.jar

  • batik-xml.jar

  • xml-apis-ext.jar

  • batik-transcoder.jar
  • batik-anim.jar
  • batik-awt-util.jar
  • batik-bridge.jar
  • batik-css.jar
  • batik-dom.jar
  • batik-ext.jar
  • batik-gvt.jar
  • batik-parser.jar
  • batik-script.jar
  • batik-svg-dom.jar
  • batik-util.jar
  • batik-xml.jar
  • xml-apis-ext.jar

从FOP,您需要使用 fop.jar 进行编译,用

From FOP, you need to compile with fop.jar and run with


  • fop.jar

  • avalon-framework-4.2.0.jar

  • xmlgraphics-commons-1.5.jar

  • fop.jar
  • avalon-framework-4.2.0.jar
  • xmlgraphics-commons-1.5.jar

这篇关于将SVG转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆