配置Batik以使用自定义字体。

7
我在配置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>

这是我的转码器代码:
 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));

                     
                     

示例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>

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

尝试将字体嵌入数据URL中。 - Robert Longson
尝试过了,但是PDF输出中仍然没有嵌入字体... - odoko
这个回答解决了你的问题吗?我如何在Batik SVG库中使用自定义字体? - Dave Jarvis
1个回答

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接