我遇到了使用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文件進行任何更改。