【问题标题】:Flying Saucer - html entities are not rendered飞碟 - 不呈现 html 实体
【发布时间】:2023-04-07 11:58:01
【问题描述】:

我正在使用飞碟库生成 pdf。但我对一些 html 实体有疑问。

我已经在寻找解决方案了,我在这个论坛和其他地方找到了很多提示,但仍然存在问题。

我已经尝试过这种方法:

http://sdtidbits.blogspot.com/2008/11/flying-saucer-xhtml-rendering-and-local.html

但没有成功

我的代码如下所示:

os = new FileOutputStream(pdf);

ITextRenderer renderer = new ITextRenderer();
ChainingReplacedElementFactory chainingReplacedElementFactory = new ChainingReplacedElementFactory();
chainingReplacedElementFactory.addReplacedElementFactory(new B64ImgReplacedElementFactory(renderer.getSharedContext()));

renderer.getSharedContext().setReplacedElementFactory(chainingReplacedElementFactory);
renderer.setDocument(url); 
renderer.layout();
renderer.createPDF(os);

其中 pdf 是要创建的新 pdf 的名称,url 是

        File f = new File(url);
        if (f.exists()) {
            url = f.toURI().toURL().toString();
        }

我的html文件是这样的

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org" />
<style type="text/css">
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, font, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, caption, tbody, tfoot, thead, tr, th
{
    color: #444;
    font-family: Arial;
    font-size: 14px;
    line-height: 25px;
    border: none;
}

table, td {border: solid 1px #CCC;}

img {page-break-inside: avoid;}
</style>

<title></title>
</head>
<body>
<h1>Test</h1>

<p>Html etites to test</p>
<p>&#8592;</p>

<p>&larr;</p>

<p>&uarr;</p>
<p>&#8593;</p>
<p>&#8595;</p>
<p></p>

</body>
</html>

除了这些实体之外,一切正常。没有任何东西只呈现箭头应有的空白点。 有没有人有解决方案?

【问题讨论】:

    标签: java itext html-entities flying-saucer xhtmlrenderer


    【解决方案1】:

    问题是 iText 默认使用的字体不支持您要打印的字符。

    解决方法是嵌入另一种可以显示该字符的字体,例如DejaVu

    在java文件中,向渲染器声明字体:

    ITextRenderer renderer = new ITextRenderer();
    renderer.getFontResolver().addFont("font/DEJAVUSANS.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    
    renderer.setDocument(url); 
    renderer.layout();
    renderer.createPDF(os);
    

    并更改 HTML 中的 font-family 声明:

    body
    {
    font-family: DejaVu Sans;
    }
    

    【讨论】:

    • 很高兴为您提供帮助。不要犹豫,接受答案(见here)。
    猜你喜欢
    • 1970-01-01
    • 2015-07-14
    • 2012-10-07
    • 2013-05-24
    • 1970-01-01
    • 2012-10-01
    • 2013-07-30
    • 1970-01-01
    • 2019-01-07
    相关资源
    最近更新 更多