xhtmlrenderer xhtml到pdf字体问题,甚至不能与字体家族一起使用:Verdana;

时间:2018-12-11 11:39:17

标签: javascript java css spring-mvc flying-saucer

我正在使用flyingSaucer从HTML和CSS生成PDF。

除字体外,其余代码都可以正常工作。下面是我正在使用的代码。

public void convertHtmlToPdf(String html, String css, OutputStream out) {
    try {
        html = correctHtml(html);
        html = getFormedHTMLWithCSS(html, css);

        HtmlCleaner cleaner = new HtmlCleaner();
        TagNode rootTagNode = cleaner.clean(html);
        CleanerProperties cleanerProperties = cleaner.getProperties();
        XmlSerializer xmlSerializer = new PrettyXmlSerializer(cleanerProperties);
        String cleanedHtml = xmlSerializer.getAsString(rootTagNode);


        File fontFile = new File("/omegaengineeringservices/Verdana.ttf");
        FontFactory.register(fontFile.getAbsolutePath());

        ITextRenderer r = new ITextRenderer();
        FontResolver resolver = r.getFontResolver();

        r.getFontResolver().addFont(fontFile.getAbsolutePath(), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        r.setDocumentFromString(cleanedHtml);

        r.layout();
        r.createPDF(out);
        r.finishPDF();

    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage(), e);
    }
}

我也已经在CSS中添加了相同的字体。

.manual-body-center{
        width:100%;
        font-family: Verdana;
    }

结果,我得到一个错误,

ExceptionConverter: java.io.IOException: /omegaengineeringservices/Verdana.ttf not found as file or resource.
    at com.lowagie.text.pdf.RandomAccessFileOrArray.<init>(Unknown Source)
    at com.lowagie.text.pdf.TrueTypeFont.process(Unknown Source)
    at com.lowagie.text.pdf.TrueTypeFont.<init>(Unknown Source)
    at com.lowagie.text.pdf.BaseFont.getAllFontNames(Unknown Source)
    at com.lowagie.text.FontFactoryImp.register(Unknown Source)
    at com.lowagie.text.FontFactory.register(Unknown Source)
    at com.lowagie.text.FontFactory.register(Unknown Source)
    at com.omegaengineering.services.pdf.impl.Converter.convertHtmlToPdf(Converter.java:40)
    at com.omegaengineeringmanualsaddon.controllers.ManualsControllers.convertFromHtml(ManualsControllers.java:148)
    at com.omegaengineeringmanualsaddon.controllers.ManualsControllers.convertHtmlToPdf(ManualsControllers.java:131)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)

1 个答案:

答案 0 :(得分:0)

给定文件路径 /omegaengineeringservices/Verdana.ttf

的问题

您需要在此处提供有效的路径,有关更多详细信息,请参阅 flyingsaucer用户指南如何添加自定义字体或特定字体?部分,提供了带有详细信息的示例。

http://flyingsaucerproject.github.io/flyingsaucer/r8/guide/users-guide-R8.html#xil_32