7

I have an issue configuring Batik PDFTranscoder for Svg to Pdf conversion. I want to embed custom truetype fonts to PDF output, therefore I utilize Batik transcoder. I supply font configuration in fop config file, as described here: https://xmlgraphics.apache.org/fop/2.2/fonts.html

I tried conversion with org.apache.xmlgraphics.Fop versions 2.1 and 2.2. Without any success.

My pdf output, comes with 'Times New Roman' font instead of coming with embedded font 'Arial', as it should, based on example below:

My config file:

 <?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>

Here's my transcoder code:

 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));

                     
                     

Sample svg file:

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

Can anyone tell me what I am missing here with Transcoder configuration? Embedding svg fonts into svg file is not an option in this case.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
odoko
  • 121
  • 1
  • 4

1 Answers1

1

Update the config file and move fonts section outside the renderers section, like this:

My config file:

 <?xml version="1.0" encoding="UTF-8"?>
 <fop version="1.0">
     <fonts>
        <directory>C:/path/to/fontsfolder</directory>   
        <auto-detect/>
     </fonts>
 </fop>

Bulk font configuration from Apache website is simply wrong: https://xmlgraphics.apache.org/fop/2.2/fonts.html


This answer was posted as an edit to the question Configure Batik to utilize custom fonts by the OP odoko under CC BY-SA 3.0.

vvvvv
  • 25,404
  • 19
  • 49
  • 81