如何从Apache FOP中的Java代码配置字体? - java

link说从Java代码配置字体不是“容易”的。我该如何实现?
我在用法语和日语等国际语言渲染某些HTML时遇到问题。

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ééeleProprié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标签。

参考方案

示例代码。

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

希望对您有帮助。

java:继承 - java

有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…

Java:BigInteger,如何通过OutputStream编写它 - java

我想将BigInteger写入文件。做这个的最好方式是什么。当然,我想从输入流中读取(使用程序,而不是人工)。我必须使用ObjectOutputStream还是有更好的方法?目的是使用尽可能少的字节。谢谢马丁 参考方案 Java序列化(ObjectOutputStream / ObjectInputStream)是将对象序列化为八位字节序列的一种通用方法。但…

Java-如何将此字符串转换为日期? - java

我从服务器收到此消息,我不明白T和Z的含义,2012-08-24T09:59:59Z将此字符串转换为Date对象的正确SimpleDateFormat模式是什么? java大神给出的解决方案 这是ISO 8601标准。您可以使用SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM…

Java Double与BigDecimal - java

我正在查看一些使用双精度变量来存储(360-359.9998779296875)结果为0.0001220703125的代码。 double变量将其存储为-1.220703125E-4。当我使用BigDecimal时,其存储为0.0001220703125。为什么将它双重存储为-1.220703125E-4? 参考方案 我不会在这里提及精度问题,而只会提及数字…

Java:从类中查找项目名称 - java

仅通过类的实例,如何使用Java反射或类似方法查找项目名称?如果不是,项目名称(我真正想要的是)可以找到程序包名称吗? 参考方案 项目只是IDE使用的简单组织工具,因此项目名称不是类或JVM中包含的信息。要获取软件包,请使用Class#getPackage()。然后,可以调用Package#getName()将包作为您在代码的包声明中看到的String来获取…