所需主要架包:

<!-- 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文件转成PDFpublic 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 blocke.printStackTrace();}}}//svg转为pngpublic 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 exceptione.printStackTrace();}finally{try {ins.close();outs.close();} catch (Exception ee) {// TODO: handle exceptionee.printStackTrace();}}}//svg转为pngpublic 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 exceptione.printStackTrace();}finally{try {ins.close();outs.close();} catch (Exception ee) {// TODO: handle exceptionee.printStackTrace();}}}//字符串转成pdfpublic 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 blocke.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);}}

Apache batik 转换svg文件为jpeg/png/pdf相关推荐

  1. python编辑svg文件_使用Python批量转换SVG文件为PNG或PDF文件

    使用Python批量转换SVG文件为PNG或PDF文件 使用Python批量转换SVG文件为PNG或PDF文件 使用模块 1 模块单独使用 2 模块用于代码 实例 1 命令行方式 2 python脚本 ...

  2. java使用batik转换svg文件

    svg是一种矢量图片格式,用来保存高保真的图片.我们可以用编辑器打开svg,我们可以看到svg文件其实就是一个xml文件,这种文件浏览器也可以识别.因此要查看svg用现成的浏览器就可以了.值得庆幸的是 ...

  3. 使用Batik开发SVG应用程序(一)

    使用Batik开发SVG应用程序 翻译时间 2007-2-6 修订记录 2007-2-23 语言修改 Thierry Kormann ILOG ILOG Les Taissounières HB2 1 ...

  4. java 解析 svg文件_java – 如何加载和解析SVG文档

    概观 使用Apache Batik加载和解析SVG文件.该解决方案在将SVG文件转换为MetaPost的初步阶段显示Java代码.这应该提供有关如何使用Java从SVG文件加载,解析和提取内容的一般概 ...

  5. java操作svg文件

    /*  *  功能:解析svg文件,并对svg文件进行操作  */ package svgapplication; import java.awt.BorderLayout; import java. ...

  6. 使用Batik开发SVG应用程序(二)

    使用Batik创建SVG应用程序 Batik工具集提供的JSVGCanvas模块是一个swing 组件,用于显示静态或动态SVG文档.通过JSVGCanvas模块,开发人员可以轻松显示SVG文档(通过 ...

  7. 如何把 AI 格式的文件转换成 BMP或者JPEG?

    简 介: 本文给出了将 AI 格式转换成JPEG的方法,经过测试可以看到,在 ZAMZAR 网站, Convertio网站提供的在线转换都获得的不错测结果.但是寻找 Python 转换 AI 文件的方 ...

  8. 将图片转换成svg文件,自定义icon小图标,svg速成

    将图片转换成svg文件,自定义icon小图标,svg速成 一.svg是什么? 二.操作步骤 1.进入网站 2.将svg复制 3.引用svg文件 总结 一.svg是什么? SVG是一种图像文件格式,它的 ...

  9. shp文件转换成svg文件

    依据展现平台硬体规格及运算能力的差异,Mobile SVG可分为SVG-B(Basic)及SVG-T(Tiny)两种不同的profiles.以相容性角度来看待这些不同的profiles,SVG-T 可 ...

最新文章

  1. Latex中的一些表格用法总结(二)——行列式的表格,表格的切分和合并
  2. Linq to SQL Like Operator(转)
  3. 补充一种简单的存储过程分页
  4. 白话Elasticsearch17-深度探秘搜索技术之match_phrase query 短语匹配搜索
  5. C语言设计新思维分享
  6. python获取cmd输出并生成字典_python - 字典输出生成如何进行改名
  7. C/C++中*和的用法详解
  8. Rambus推出面向下一代数据中心的PCIe 6.0控制器
  9. linux NAND驱动之四:6410上的NAND读写流程
  10. 免费题库CISP,NISP,SCSA,SCSP,CISA,CISSP,CISP-PTE
  11. [Java] 用java写的植物大战僵尸辅助
  12. c语言运行的快捷键是什么,c语言执行命令快捷键是什么??
  13. Java JDK8/JAVA8以及后版本收费后还能用吗
  14. android wifi智能硬件4g,智能硬件 篇五:把WiFi带在身上,告别宽带!华为随行WiFi2畅享版真实体验...
  15. 小程序实现canvas添加图文
  16. ieee 802.3学习笔记-MII
  17. 【转】这是一篇很完整的元器件选型指南
  18. Rtabmap 视觉建图与导航 Ubuntu 18.04 Kobuki
  19. 比较两组数据的差异用什么图更直观_你真的懂如何展示数据吗?
  20. 思科交换机接口划分到vlan

热门文章

  1. 「聊天宝」体验记录,生朋友聊天
  2. SQL零基础入门学习(十二)
  3. 从未感知海洋的神秘力量——海马
  4. MATLAB 元胞(cell)全解
  5. Packet Tracer - 在 VTY 线路上配置 IPv4 ACL
  6. 【数据结构与算法】 01 链表 (单链表、双向链表、循环链表、块状链表、头结点、链表反转与排序、约瑟夫环问题)
  7. 【微信小程序】自定义日志打印
  8. 老字号“斗汽”登顶热搜,国产汽水打完“情怀牌”还剩什么?
  9. Dicom文件支持中文字符
  10. 单机游戏体力恢复的思路