java svg 转pdf_Convert SVG to PDF

直接上代码:

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.FileNotFoundException;

importjava.io.FileOutputStream;

importorg.apache.batik.transcoder.Transcoder;

importorg.apache.batik.transcoder.TranscoderException;

importorg.apache.batik.transcoder.TranscoderInput;

importorg.apache.batik.transcoder.TranscoderOutput;

importorg.apache.fop.svg.PDFTranscoder;

publicclassTest{

publicstaticvoidmain(String[]argv)throwsTranscoderException,FileNotFoundException

{

Transcodertranscoder=newPDFTranscoder();

TranscoderInputtranscoderInput=newTranscoderInput(newFileInputStream(newFile("/tmp/test.svg")));

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

transcoder.transcode(transcoderInput,transcoderOutput);

}

}

The compile-and-run commands arejavac-cp batik-rasterizer.jar-d buildTest.javajava-cp build:batik-rasterizer.jarTest

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.JPEGTranscoder, PNGTranscoder, 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 withbatik-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 withfop.jar

avalon-framework-4.2.0.jar

xmlgraphics-commons-1.5.jar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值