http://stackoverflow.com/questions/6875807/convert-svg-to-pdf

 

直接上代码:

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)throwsTranscoderException,FileNotFoundException

    {

        Transcoder transcoder =new PDFTranscoder();

        TranscoderInput transcoderInput =new TranscoderInput(new FileInputStream(new File("/tmp/test.svg")));

        TranscoderOutput transcoderOutput =newTranscoderOutput(newFileOutputStream(newFile("/tmp/test.pdf")));

        transcoder.transcode(transcoderInput, transcoderOutput);

    }

}

 

 

The compile-and-run commands are

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

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.

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

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

(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.)

Edit:

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

  • batik-svg-dom.jar

  • batik-util.jar

  • batik-xml.jar

  • xml-apis-ext.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