所需主要架包:
<!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/batik-transcoder -->
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-transcoder</artifactId>
<version>1.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/batik-codec 转PNG需要 -->
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-codec</artifactId>
<version>1.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/fop 转pdf需要,注意版本 -->
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
<version>1.0</version>
</dependency>
此处用的batik 1.7版本,更高版本出现一些问题,坑了:
1、如所需某些类没有了,估计是高版本没有整合完;
2、某些高版本出现转换pdf时,出现死循环的情况,有点无语。
直接上完整代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
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.batik.transcoder.image.JPEGTranscoder;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.fop.svg.PDFTranscoder;
import java.io.*;
public class SvgUtil {
//svg文件转成PDF
public static void convert2Pdf(File svg, File pdf) {
OutputStream outs =null;
InputStream ins = null;
try {
outs =new BufferedOutputStream(new FileOutputStream(pdf));
ins= new FileInputStream(svg);
Transcoder transcoder = new PDFTranscoder();
TranscoderInput input = new TranscoderInput(ins);
TranscoderOutput output = new TranscoderOutput(outs);
transcoder.transcode(input, output);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
ins.close();
outs.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//svg转为png
public static void convert2Jpeg(File svg, File jpeg){
InputStream ins = null ;
OutputStream outs = null;
try {
ins= new FileInputStream(svg);
outs = new BufferedOutputStream(new FileOutputStream(jpeg));
Transcoder transcoder = new JPEGTranscoder();
//为防止ERROR: The JPEG quality has not been specified. Use the default one: no compression 错误,需如下配置
transcoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, 0.99f);
TranscoderInput input = new TranscoderInput(ins);
TranscoderOutput output = new TranscoderOutput(outs);
transcoder.transcode(input, output);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
try {
ins.close();
outs.close();
} catch (Exception ee) {
// TODO: handle exception
ee.printStackTrace();
}
}
}
//svg转为png
public static void convert2Png(File svg, File png){
InputStream ins = null ;
OutputStream outs = null;
try {
ins= new FileInputStream(svg);
outs = new BufferedOutputStream(new FileOutputStream(png));
Transcoder transcoder = new PNGTranscoder();
//为防止图片转换时出现图片大小和SVG设置的大小不一致,需要在转换时设置图片大小 2450和150是SVG中width和height
// transcoder.addTranscodingHint(PNGTranscoder.KEY_WIDTH, new Float(2450.0));
// transcoder.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, new Float(150.0));
TranscoderInput input = new TranscoderInput(ins);
TranscoderOutput output = new TranscoderOutput(outs);
transcoder.transcode(input, output);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
try {
ins.close();
outs.close();
} catch (Exception ee) {
// TODO: handle exception
ee.printStackTrace();
}
}
}
//字符串转成pdf
public static void convertStr2Pdf(String svg, File pdf){
OutputStream outs =null;
InputStream ins = null;
try {
outs =new BufferedOutputStream(new FileOutputStream(pdf));
ins= new ByteArrayInputStream(svg.getBytes());
Transcoder transcoder = new PDFTranscoder();
TranscoderInput input = new TranscoderInput(ins);
TranscoderOutput output = new TranscoderOutput(outs);
transcoder.transcode(input, output);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
ins.close();
outs.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
File file=new File("g://score_0.svg");
File pdf=new File("g://score_0.pdf");
File png =new File("g://score_0.png");
File jpeg =new File("g://score.jpg");
SvgUtil.convert2Pdf(file, pdf);
// SvgUtil.convert2Png(file, png);
// SvgUtil.convert2Jpeg(file,jpeg);
}
}