当前位置: 首页 > 知识库问答 >
问题:

如何在Apache FOP中从Java代码配置字体?

殷安顺
2023-03-14

该链接说从java代码配置字体并不“容易”。我如何实现这一点?我在从法语和日语等国际语言渲染某些HTMLs时遇到了问题。

WARNING: Font "Symbol,normal,700" not found. Substituting with "Symbol,normal,400".
May 08, 2015 4:45:39 PM org.apache.fop.events.LoggingEventListener processEvent
WARNING: Font "ZapfDingbats,normal,700" not found. Substituting with "ZapfDingbats,normal,400". 

生成的 PDF 因此而损坏。

更新:

我的HTML包含法语单词,如“Modifié Créée le Propriétaire”

File file = new File("C:\\Users\\me\\Desktop\\Test.html");


fopFactory = FopFactory.newInstance();
foUserAgent = fopFactory.newFOUserAgent();


String fileName = file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("\\")+1,file.getAbsolutePath().lastIndexOf("."));
        String workspacePath = file.getAbsolutePath().substring(0,file.getAbsolutePath().lastIndexOf("\\"));
        File xsltfile = new File("xhtml2fo.xsl");
        StreamSource source = null;
        source = new StreamSource(file);
        StreamSource transformSource = new StreamSource(xsltfile);
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();


        Transformer xslfoTransformer = null;
        TransformerFactory fac = TransformerFactory.newInstance();
        xslfoTransformer = fac.newTransformer(transformSource);
        xslfoTransformer.setErrorListener(this);

        Fop fop;
        fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, outStream);
        // Resulting SAX events (the generated FO)
        Result res = new SAXResult(fop.getDefaultHandler());
        xslfoTransformer.transform(source, res);

        output = new File(workspacePath + File.separator + fileName + ".pdf");
        OutputStream out = new java.io.FileOutputStream(output);
        out = new java.io.BufferedOutputStream(out);
        FileOutputStream str = new FileOutputStream(output);
        str.write(outStream.toByteArray());
        str.close();

我使用Antennahouse提供的XSLT将HTML标记转换为FO标记。

共有1个答案

汪弘光
2023-03-14

示例代码。

/** The Constant FOP_CONFIG. */
    private static final String FOP_CONFIG = "pdf.fop.cfg.xml";

fopFactory = FopFactory.newInstance();
                    // for image base URL
                    String serverPath = request.getSession().getServletContext().getRealPath("/");
                    //disable strict validatetion
                    fopFactory.setStrictValidation(false);
                    fopFactory.setBaseURL(serverPath);
                    // for fonts base URL
                    fopFactory.getFontManager().setFontBaseURL(serverPath);
                    // read custom font setting
                    StreamSource configSrc = new StreamSource(Thread.currentThread().getContextClassLoader().getResourceAsStream(PDFComponentFactory.FOP_CONFIG));
                    DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
                    Configuration cfg = cfgBuilder.build(configSrc.getInputStream());
                    fopFactory.setUserConfig(cfg);

希望能帮到你。

 类似资料:
  • 我有以下fop.conf,在Windows下工作正常: 但是因为应用程序还需要在没有提供Arial的操作系统上运行,所以我决定在Java应用程序的< code > src/main/resources/fonts/Arial . TTF 中添加一个字体。 所以我试着这样引用字体: 但它不使用字体。 调试时,我看到fopFactoryBuilder有一个名为< code>baseUri的字段,是否可

  • 我试图将以下Spring任务xml配置转换为纯粹基于代码/注释的版本: 根据Spring规范28.4.1(http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html),他们说,从XML开始: 编写代码配置就像启用@EnableScheduling和/或@EnableSync一样简

  • 我怎么能改变这个xml配置: 到目前为止的java代码配置我有这个我只有这部分的问题: 这就是我到目前为止在java代码中所拥有的: 这一行给我这个错误:

  • 我有许多s和它们每个的下面,一个具有

  • 问题内容: 我正在尝试将以下Spring任务xml配置转换为纯粹基于代码/注释的版本: 根据Spring规范28.4.1(http://docs.spring.io/spring/docs/current/spring-framework- reference/html/scheduling.html ),他们说要像这样从XML中获取: 代码配置就像启用@EnableScheduling和/或@E

  • 我在我的java应用程序中使用hive JDBC1.0来创建与hive服务器的连接并执行查询。我想从java代码中设置空闲配置单元连接超时。就像say一样,用户首先创建配置单元连接,如果配置单元连接在接下来的10分钟内保持空闲,那么该连接对象将过期。如果用户在10分钟后使用相同的连接对象执行查询,那么配置单元jdbc将抛出错误。你能告诉我通过java代码实现这一点的方法吗。 我知道配置单元中有一个