我遇到了使用Apache Batik转换PNG的问题,与具有不同字体系列的文本(如Arial)不同。SVG到PNG文本无法正常显示 - Arial字体
// Convert the SVG image to png and send back
PNGTranscoder transcoder = new PNGTranscoder();
//
TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(svgImage));
outStream = new ByteArrayOutputStream();
TranscoderOutput output = new TranscoderOutput(outStream);
// Transcode the given SVG
transcoder.transcode(input, output);
outStream.flush();
pngImage = outStream.toByteArray();
SVG文件,我要转换成PNG是:在美分OS 6运行Tomcat 7和Java 6
使用SVG转换成PNG的Java代码的环境中发生的问题:
<svg version="1.1" x="0" y="0" id="hjtqebzv1" width="610" height="240" xmlns="http://www.w3.org/2000/svg" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<linearGradient id="LFFFFFF0" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:#FFFFFF;stop-opacity:0.8"/>
<stop offset="100%" style="stop-color:#FAFAFA;stop-opacity:1"/>
</linearGradient>
</defs>
<g id="hjtqebzv-o1" transform="translate(5,5)">
<rect x="1" y="1" width="578" height="20" fill="url(#LFFFFFF0)" stroke="#5e5ca7" stroke-width="2" stroke-linejoin="round" stroke-linecap="round"/>
<text x="1" y="1" width="578" height="19" >
<tspan x="2" y="14" style="font-family:Arial;font-size:12px;fill:#000000;">This is a test text for testing text overlapping in the rectangle when convert the svg to PNG using SVG-Batik</tspan>
</text>
</g>
</svg>
当我打开Firefox浏览器的SVG文件,它会显示正确的如下图中给出:
但是,当我使用Apache Batik转换SVG时,转换后的图像看起来不同。 Apache Batik转换后的PNG为:
在Windows 7运行的tomcat 7和Java 7中,生成的图像与原始SVG完全相同。
作为Cent OS服务器,它提供了文本混乱的图像,我觉得Arial字体不适用于tomcat/java应用程序,需要手动加载它。如果是这样,我宁愿有一个建议,以通用的方式从底层操作系统位置(操作系统字体位置)加载它们,而不需要对SVG文件进行任何更改。