在网上找了一大圈方法。试过imagemagick等其他方式进行转换 都无法满足需要。转换后的矢量文件有时候是无法进行编辑的,就一张图片。无法满足我的要求,这个期间查看了好多帖子,有的要就是不全,要不就是jar不给提供,只给下载,还需要积分,这个可以理解 ,但是偏偏花了大量积分下载下来的jar包尽然无法使用,前钱后后花了100多积分,这可是我的好不容易积攒的。可真是害死人了,太坑了。无意间看到了一个帖子 上面提供了这种方式,但是也没有提供jar包和pom,但是官网是提供出来了,时间有点久忘记了那个贴子的路径了,实在不好意思,无法贴出来。最终 在官方上找到了jar包,同时在maven中央仓库找到了pom文件,为了使大家不在被坑,因而贴出来,希望能少走点弯路。
如果您不要通过代码转换,可以试试网上的提供的免费连接:
https://cn.office-converter.com/SVG-to-EPS
https://convertio.co/zh/svg-eps/
如果您不差钱,我差钱(🤭),可以使用上面两个连接提供的第三方接口,每个月大概是$14.99美元,大概是人民币:106.1847人民币元
所支持的相关格式:
具体请查看相关源码:org.apache.batik.transcoder.Transcoder
pom:
<!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/batik-all -->
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-all</artifactId>
<version>1.12</version>
<type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/fop -->
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
<version>2.4</version>
</dependency>
工具类:
import org.apache.batik.transcoder.*;
import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.batik.transcoder.image.TIFFTranscoder;
import org.apache.fop.render.ps.EPSTranscoder;
import org.apache.fop.render.ps.PSTranscoder;
import org.apache.fop.svg.AbstractFOPTranscoder;
import org.apache.fop.svg.PDFTranscoder;
import java.io.*;
/**
* * 利用Apache Batik实现 SVG转PDF/PNG/JPG
*/
public class SVGConverterUtils {
public static void main(String[] args) throws Exception {
String svgpath = "E:\\svgfile\\c.svg";
File svgFile = new File(svgpath);
String name = svgFile.getName();
name = name.substring(0, name.lastIndexOf("."));
SVGConverter converter = new SVGConverter();
// converter.svg2PDF(new File(svgpath), new File("E:/" + name + "_SVG文件转PDF.pdf"));
// converter.svg2PNG(new File(svgpath), new File("E:/" + name + "_SVG文件转PNG.png"));
// converter.svg2JPEG(new File(svgpath), new File("E:/" + name + "_SVG文件转JPG.jpg"));
String svgCode = converter.svg2String(new File(svgpath));
// converter.svg2PDF(svgCode, "D:/" + name + "_SVG代码转PDF.pdf");
// converter.svg2PNG(svgCode, "D:/" + name + "_SVG代码转PNG.png");
// converter.svg2JPEG(svgCode, "D:/" + name + "_SVG代码转JPG.jpg");
// converter.svg2PDF(svgCode, new FileOutputStream(new File("E:/svgfile/" + name + "_SVG代码转输出流.pdf")));
// converter.svg2PNG(svgCode, new FileOutputStream(new File("E:/svgfile/" + name + "_SVG代码转输出流.png")));
// converter.svg2JPEG(svgCode, new FileOutputStream(new File("E:/svgfile/" + name + "_SVG代码转输出流.jpg")));
// converter.svg2EPS(svgCode, new FileOutputStream(new File("E:/svgfile/" + name + "_SVG代码转输出流.eps")));
// converter.svg2PS(svgCode, new FileOutputStream(new File("E:/svgfile/" + name + "_SVG代码转输出流.ps")));
converter.svgtoeps(svgCode, new FileOutputStream(new File("E:/svgfile/" + name + "_SVG代码转输出流.eps")));
}
public void svgtoeps(String svgCode, OutputStream os){
EPSTranscoder epsTranscoder = new EPSTranscoder();
try {
svgCode = svgCode.replaceAll(":rect", "rect");
TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(svgCode.getBytes()));
TranscoderOutput output = new TranscoderOutput(os);
epsTranscoder.transcode(input, output);
os.flush();
os.close();
} catch (Exception e) {
}
}
/**
* SVG转PNG
*
* @param svgCode SVG代码
* @param outpath 输出路径
* @throws TranscoderException
* @throws IOException
*/
public void svg2PNG(String svgCode, String outpath) throws TranscoderException, IOException {
Transcoder transcoder = new PNGTranscoder();
svgConverte(svgCode, outpath, transcoder);
}
/**
* SVG转PNG
*
* @param svgCode SVG代码
* @param out 输出流
* @throws TranscoderException
* @throws IOException
*/
public void svg2PNG(String svgCode, OutputStream out) throws TranscoderException, IOException {
Transcoder transcoder = new PNGTranscoder();
svgConverte(svgCode, out, transcoder);
}
/**
* SVG转PNG
*
* @param svgFile SVG文件
* @param outFile 输出文件
* @throws TranscoderException
* @throws IOException
*/
public void svg2PNG(File svgFile, File outFile) throws TranscoderException, IOException {
Transcoder transcoder = new PNGTranscoder();
svgConverte(svgFile, outFile, transcoder);
}
/**
* SVG转JPG
*
* @param svgCode SVG代码
* @param outpath 输出路径
* @throws TranscoderException
* @throws IOException
*/
public void svg2JPEG(String svgCode, String outpath) throws TranscoderException, IOException {
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);
svgConverte(svgCode, outpath, transcoder);
}
/**
* SVG转JPG
*
* @param svgCode SVG代码
* @param out 输出流
* @throws TranscoderException
* @throws IOException
*/
public void svg2JPEG(String svgCode, OutputStream out) throws TranscoderException, IOException {
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);
svgConverte(svgCode, out, transcoder);
}
/**
* SVG转JPG
*
* @param svgFile SVG文件
* @param outFile 输出文件
* @throws TranscoderException
* @throws IOException
*/
public void svg2JPEG(File svgFile, File outFile) throws TranscoderException, IOException {
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);
svgConverte(svgFile, outFile, transcoder);
}
/**
* SVG转PDF
*
* @param svgCode SVG代码
* @param outpath 输出路径
* @throws TranscoderException
* @throws IOException
*/
public void svg2PDF(String svgCode, String outpath) throws TranscoderException, IOException {
Transcoder transcoder = new PDFTranscoder();
svgConverte(svgCode, outpath, transcoder);
}
/**
* SVG转PS
*
* @param svgCode
* @param outpath
* @throws TranscoderException
* @throws IOException
*/
public void svg2PS(String svgCode, String outpath) throws TranscoderException, IOException {
Transcoder transcoder = new PSTranscoder();
svgConverte(svgCode, outpath, transcoder);
}
/**
* SVG转PS
*
* @param svgCode SVG代码
* @param out 输出流
* @throws TranscoderException
* @throws IOException
*/
public void svg2PS(String svgCode, OutputStream out) throws TranscoderException, IOException {
Transcoder transcoder = new PSTranscoder();
svgConverte(svgCode, out, transcoder);
}
/**
* SVG转EPS
*
* @param svgCode SVG代码
* @param out 输出流
* @throws TranscoderException
* @throws IOException
*/
public void svg2EPS(String svgCode, OutputStream out) throws TranscoderException, IOException {
Transcoder transcoder = new EPSTranscoder();
svgConverte(svgCode, out, transcoder);
}
/**
* SVG转EPS
*
* @param svgCode
* @param outpath
* @throws TranscoderException
* @throws IOException
*/
public void svg2EPS(String svgCode, String outpath) throws TranscoderException, IOException {
Transcoder transcoder = new EPSTranscoder();
svgConverte(svgCode, outpath, transcoder);
}
/**
* SVG转PDF
*
* @param svgCode SVG代码
* @param out 输出流
* @throws TranscoderException
* @throws IOException
*/
public void svg2PDF(String svgCode, OutputStream out) throws TranscoderException, IOException {
Transcoder transcoder = new PDFTranscoder();
svgConverte(svgCode, out, transcoder);
}
/**
* SVG转PDF
*
* @param svgFile SVG文件
* @param outFile 输出文件
* @throws TranscoderException
* @throws IOException
*/
public void svg2PDF(File svgFile, File outFile) throws TranscoderException, IOException {
Transcoder transcoder = new PDFTranscoder();
svgConverte(svgFile, outFile, transcoder);
}
private void svgConverte(String svgCode, String outpath, Transcoder transcoder) throws IOException, TranscoderException {
svgConverte(svgCode, getOutputStream(outpath), transcoder);
}
private void svgConverte(File svg, File outFile, Transcoder transcoder) throws IOException, TranscoderException {
svgConverte(svg2String(getInputStream(svg)), getOutputStream(outFile), transcoder);
}
private void svgConverte(String svgCode, OutputStream out, Transcoder transcoder) throws IOException, TranscoderException {
svgCode = svgCode.replaceAll(":rect", "rect");
TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(svgCode.getBytes()));
TranscoderOutput output = new TranscoderOutput(out);
svgConverte(input, output, transcoder);
}
private void svgConverte(TranscoderInput input, TranscoderOutput output, Transcoder transcoder) throws IOException, TranscoderException {
transcoder.transcode(input, output);
}
public InputStream getInputStream(File file) throws IOException {
return new FileInputStream(file);
}
public InputStream getInputStream(String filepath) throws IOException {
File file = new File(filepath);
if (file.exists())
return getInputStream(file);
else
return null;
}
public OutputStream getOutputStream(File outFile) throws IOException {
return new FileOutputStream(outFile);
}
public OutputStream getOutputStream(String outpath) throws IOException {
File file = new File(outpath);
if (!file.exists())
file.createNewFile();
return getOutputStream(file);
}
/**
* 默认使用编码UTF-8 SVG文件输入流转String
*
* @param svgFile
* @return SVG代码
* @throws IOException
*/
public String svg2String(File svgFile) throws IOException {
InputStream in = getInputStream(svgFile);
return svg2String(in, "UTF-8");
}
/**
* SVG文件输入流转String
*
* @param svgFile
* @return SVG代码
* @throws IOException
*/
public String svg2String(File svgFile, String charset) throws IOException {
InputStream in = getInputStream(svgFile);
return svg2String(in, charset);
}
/**
* 默认使用编码UTF-8SVG输入流转String
*
* @param in
* @return SVG代码
*/
public String svg2String(InputStream in) {
return svg2String(in, "UTF-8");
}
/**
* 指定字符集SVG输入流转String
*
* @param in 输入流
* @param charset 字符编码
* @return SVG代码
*/
public String svg2String(InputStream in, String charset) {
StringBuffer svgBuffer = new StringBuffer();
BufferedReader bfr = null;
try {
InputStreamReader inputStreamReader = new InputStreamReader(in, charset);
bfr = new BufferedReader(inputStreamReader);
String line = "";
while ((line = bfr.readLine()) != null) {
svgBuffer.append(line);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bfr != null)
bfr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return svgBuffer.toString();
}
}
注意:目前遇到的问题是svg转成eps文件可以成功,但是转出来的eps颜色会丢,查询官网目前还没有解决,如果解决了 后续会修改掉。
参考:
https://xmlgraphics.apache.org/fop/
https://xmlgraphics.apache.org/batik/
https://issues.apache.org/jira/projects/FOP/issues/FOP-1487?filter=allopenissues