使用Apache FOP Java库转换为PDF / a转换
作者:互联网
我正在尝试使用Apache FOP 2.1将.fo文件转换为PDF / a,文档中给出了exemple代码.我设法将helloworld.fo文件转换为PDF但是,当我尝试将其转换为PDF /文件时,我收到此错误:
org.apache.fop.pdf.PDFConformanceException:对于PDF / A-1a,必须嵌入所有字体,甚至是基本14字体!违规字体:/ 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>
但即便这样做,我仍然有同样的错误:org.apache.fop.pdf.PDFConformanceException:对于PDF / A-1a,所有字体,甚至是基本14字体,都必须嵌入!违规字体:/ Helvetica
我还尝试通过在< fo:root“>中添加font-family =”Helvetica“font-weight =”normal“font-style =”normal“来修改helloworld.fo,因为有些forum topic说这样做但是它没有改变任何东西.
请帮我解决嵌入字体错误.感谢您阅读本主题.
[编辑]:
这是完整的错误消息:
FOP ExampleFO2PDF
Preparing...
Input: XSL-FO (.\xml\fo\helloworld.fo)
Output: PDF (.\out\ResultFO2PDF.pdf)
Transforming...
août 07, 2017 11:26:49 AM org.apache.fop.events.LoggingEventListener processEvent
INFOS: Rendered page #1.
org.apache.fop.pdf.PDFConformanceException: For PDF/A-1b, all fonts, even the base 14 fonts, have to be embedded! Offending font: /Helvetica
at org.apache.fop.pdf.PDFFont.validate(PDFFont.java:170)
at org.apache.fop.pdf.PDFFont.output(PDFFont.java:179)
at org.apache.fop.pdf.PDFDocument.outputIndirectObject(PDFDocument.java:1053)
at org.apache.fop.pdf.PDFDocument.streamIndirectObject(PDFDocument.java:1018)
at org.apache.fop.pdf.PDFDocument.output(PDFDocument.java:994)
at org.apache.fop.pdf.PDFDocument.outputTrailer(PDFDocument.java:1097)
at org.apache.fop.render.pdf.PDFDocumentHandler.endDocument(PDFDocumentHandler.java:191)
at org.apache.fop.render.intermediate.util.IFDocumentHandlerProxy.endDocument(IFDocumentHandlerProxy.java:187)
at org.apache.fop.render.intermediate.IFRenderer.stopRenderer(IFRenderer.java:295)
at org.apache.fop.area.RenderPagesModel.endDocument(RenderPagesModel.java:265)
at org.apache.fop.area.AreaTreeHandler.endDocument(AreaTreeHandler.java:342)
at org.apache.fop.fo.FOTreeBuilder.endDocument(FOTreeBuilder.java:170)
at org.apache.xalan.transformer.TransformerIdentityImpl.endDocument(TransformerIdentityImpl.java:963)
at org.apache.xerces.parsers.AbstractSAXParser.endDocument(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl.endEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.endEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityScanner.load(Unknown Source)
at org.apache.xerces.impl.XMLEntityScanner.skipSpaces(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl$TrailingMiscDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:485)
at ConverterFO2PDFa.convertFO2PDF(ConverterFO2PDFa.java:104)
at ConverterFO2PDFa.main(ConverterFO2PDFa.java:154)
解决方法:
经过一些更改后,我能够使用相同的示例文件创建PDF / A输出:
>在输入FO文件中,我明确地将font-family =“Helvetica”(或您喜欢的字体系列)添加到影响所有文本的位置
如果没有这个,即使FOP在没有设置font-family时默认使用Helvetica,显然也无法找到它的配置.
>在配置文件中,我将Helvetica字体系列映射到现有字体,例如Arial
<font kerning="yes" embed-url="/Library/Fonts/arial.ttf">
<font-triplet name="Helvetica" style="normal" weight="normal"/>
</font>
>在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");
...
标签:apache-fop,java,fonts,pdfa 来源: https://codeday.me/bug/20191003/1847663.html