飞碟-无法呈现html实体 [英] Flying Saucer - html entities are not rendered

查看:112
本文介绍了飞碟-无法呈现html实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用飞碟库lib生成pdf.但是我对某些html实体有疑问.

I'm generating pdf using flying-saucer lib. But I have problem with some html entities.

我已经在寻找解决方案了,我在这个论坛以及其他地方发现了很多技巧,但是仍然存在问题.

I've already was searching for solution I found many tips in this forum, and in other places but still there is the problem.

我已经尝试过这种方法:

I've tried this approach :

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是

where pdf is the name of new pdf to create and url is

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

我的html文件如下所示

my html file look like this

<!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>

在这些实体旁边,一切正常.没有什么东西会只用箭头在空白处呈现. 有人对此有解决方案吗?

Everything works fine beside those entities. There is nothing rendered only blank spots where should by arrows. Does anyone has solution for that ?

推荐答案

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

The issue is that the font used by iText by default doesn't support the caracters you want to print.

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

The solution is to embed another font which can display this character, for example DejaVu.

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

In the java file, declare the font to the renderer:

ITextRenderer renderer = new ITextRenderer();
renderer.getFontResolver().addFont("font/DEJAVUSANS.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

renderer.setDocument(url); 
renderer.layout();
renderer.createPDF(os);

并更改HTML中的字体系列声明:

And change the font-family declaration in the HTML:

body
{
font-family: DejaVu Sans;
}

这篇关于飞碟-无法呈现html实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆