配置 Batik 以使用自定义字体

odo*_*oko 7 java svg truetype batik apache-fop

我在配置 Batik PDFTranscoder 以进行 Svg 到 Pdf 转换时遇到问题。我想将自定义 truetype 字体嵌入到 PDF 输出中,因此我使用 Batik 转码器。我在 fop 配置文件中提供字体配置,如下所述: https: //xmlgraphics.apache.org/fop/2.2/fonts.html

我尝试使用 org.apache.xmlgraphics.Fop 版本 2.1 和 2.2 进行转换。没有任何成功。

我的 pdf 输出带有“Times New Roman”字体,而不是带有嵌入字体“Arial”,因为它应该基于以下示例:

我的配置文件:

 <?xml version="1.0" encoding="UTF-8"?>
 <fop version="1.0">
  <renderers>
   <renderer mime="application/pdf">
     <fonts>
        <directory>C:/path/to/fontsfolder</directory>   
        <auto-detect/>
     </fonts>
   </renderer>
  </renderers>
 </fop>
Run Code Online (Sandbox Code Playgroud)

这是我的转码器代码:

 import org.apache.batik.transcoder.*;
 import org.apache.fop.svg.PDFTranscoder; 
 import org.w3c.dom.Document;


 PDFTranscoder transcoder = new PDFTranscoder();

 try {
   DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
   Configuration cfg = cfgBuilder.buildFromFile(new File("path-to-xml-config-file"));
   ContainerUtil.configure(transcoder, cfg);
 } catch (Exception e) {
   throw new TranscoderException(e);
 }
   transcoder.transcode(new TranscoderInput(doc),
                      new TranscoderOutput(os));

                     
                     
Run Code Online (Sandbox Code Playgroud)

示例 svg 文件:

<defs>
<style type="text/css">
     @font-face{font-family:'Arial';font-style: normal;font-weight:
     400;src:url('C:/path/to/fontsfolder/arial.ttf')
     format('truetype');}
</style>
</defs>
   
 
  <text x="55" y="245" style="font-family: 'Arial'; font-weight:normal;
font-style: normal" >Sample text</text>
Run Code Online (Sandbox Code Playgroud)

谁能告诉我转码器配置中缺少什么?在这种情况下,不能选择将 svg 字体嵌入到 svg 文件中。

vin*_*zee 1

更新配置文件并将字体部分移到渲染器部分之外,如下所示:

我的配置文件:

 <?xml version="1.0" encoding="UTF-8"?>
 <fop version="1.0">
     <fonts>
        <directory>C:/path/to/fontsfolder</directory>   
        <auto-detect/>
     </fonts>
 </fop>
Run Code Online (Sandbox Code Playgroud)

Apache 网站的批量字体配置完全错误: https ://xmlgraphics.apache.org/fop/2.2/fonts.html


此答案是由 OP odoko在 CC BY-SA 3.0 下作为对问题配置蜡染以利用自定义字体的编辑发布的。