使用Apache FOP Java库转换为PDF / a转换

时间:2017-08-01 10:45:18

标签: java fonts apache-fop pdfa

我尝试使用文档中提供的exemple代码将.fo文件转换为带有Apache FOP 2.1的PDF / a文件。我设法将helloworld.fo文件转换为PDF,但是当我尝试将其转换为PDF /文件时,我收到此错误:

org.apache.fop.pdf.PDFConformanceException: For PDF/A-1a, all fonts, even the base 14 fonts, have to be embedded! Offending font: /Helvetica

这就是为什么我尝试在fop.xconf文件中嵌入字体的原因:

  <fonts>

    <font kerning="yes" embed-url="C:/Users/thinkpad/workspace/FopConverter/Fonts/arial.ttf" name="Arial">
      <font-triplet name="Arial" style="normal" weight="700"/>
    </font>
    <font kerning="yes" embed-url="C:/Users/thinkpad/workspace/FopConverter/Fonts/arialbd.ttf" name="Arial Bold">
      <font-triplet name="Arial" style="normal" weight="bold"/>
    </font>
    <font kerning="yes" embed-url="C:/Users/thinkpad/workspace/FopConverter/Fonts/ariali.ttf" name="Arial Italique">
      <font-triplet name="Arial" style="italic" weight="700"/>
    </font>
    <font kerning="yes" embed-url="C:/Users/thinkpad/workspace/FopConverter/Fonts/arialbi.ttf" name="Helvetica Bold Italique">
      <font-triplet name="Arial" style="italic" weight="bold"/>
    </font>

    <directory>C:\Users\thinkpad\workspace\FopConverter\Fonts</directory>
    <directory>C:\Users\thinkpad\workspace\FopConverter\Fonts\ghostFonts</directory>

   <substitutions>
     <substitution>
       <from font-family="Helvetica" font-weight="700..900"/>
       <to font-family="Arial"/>
     </substitution>
   </substitutions>
   <auto-detect/>
  </fonts>

<fonts> <font kerning="yes" embed-url="C:/Users/thinkpad/workspace/FopConverter/Fonts/arial.ttf" name="Arial"> <font-triplet name="Arial" style="normal" weight="700"/> </font> <font kerning="yes" embed-url="C:/Users/thinkpad/workspace/FopConverter/Fonts/arialbd.ttf" name="Arial Bold"> <font-triplet name="Arial" style="normal" weight="bold"/> </font> <font kerning="yes" embed-url="C:/Users/thinkpad/workspace/FopConverter/Fonts/ariali.ttf" name="Arial Italique"> <font-triplet name="Arial" style="italic" weight="700"/> </font> <font kerning="yes" embed-url="C:/Users/thinkpad/workspace/FopConverter/Fonts/arialbi.ttf" name="Helvetica Bold Italique"> <font-triplet name="Arial" style="italic" weight="bold"/> </font> <directory>C:\Users\thinkpad\workspace\FopConverter\Fonts</directory> <directory>C:\Users\thinkpad\workspace\FopConverter\Fonts\ghostFonts</directory> <substitutions> <substitution> <from font-family="Helvetica" font-weight="700..900"/> <to font-family="Arial"/> </substitution> </substitutions> <auto-detect/> </fonts>

但即使这样做,我仍然有同样的错误:

我还尝试在org.apache.fop.pdf.PDFConformanceException: For PDF/A-1a, all fonts, even the base 14 fonts, have to be embedded! Offending font: /Helvetica中添加helloworld.fo来修改font-family="Helvetica" font-weight="normal" font-style="normal",因为有些forum topic表示会这样做,但它并没有改变任何内容。< / p>

请帮我解决嵌入字体错误。感谢您阅读此主题。

[编辑]: 以下是整个错误消息:

<fo:root">

1 个答案:

答案 0 :(得分:0)

我可以在几次更改后使用相同的示例文件创建PDF / A输出:

    输入FO文件
  1. ,我明确地将font-family="Helvetica"(或您喜欢的字体系列)添加到影响所有文字的位置

    如果没有这个,即使FOP在没有设置font-family时默认使用Helvetica,显然也无法找到它的配置。

  2. 配置文件中,我将Helvetica字体系列映射到现有字体,例如Arial

    <font kerning="yes" embed-url="/Library/Fonts/arial.ttf">
      <font-triplet name="Helvetica" style="normal" weight="normal"/>
    </font>
    
  3. Java代码中,我将FOP配置为使用我的配置文件,并将用户代理配置为启用辅助功能

    import org.apache.avalon.framework.configuration.Configuration;
    import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
    import org.apache.fop.apps.FopFactoryBuilder;
    
    ...
    DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
    Configuration cfg = cfgBuilder.buildFromFile(new File("/Users/lfurini/fop.xconf"));
    FopFactoryBuilder fopFactoryBuilder = new FopFactoryBuilder(new File(".").toURI()).setConfiguration(cfg);
    FopFactory fopFactory = fopFactoryBuilder.build();
    
    FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
    // configure foUserAgent as desired
    foUserAgent.setAccessibility(true);
    foUserAgent.getRendererOptions().put("pdf-a-mode", "PDF/A-1b");
    ...