所需主要架包:
<!-- 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);
}
}