渲染图像XmlWorkerHelper(itextpdf) [英] rendering images XmlWorkerHelper(itextpdf)

查看:569
本文介绍了渲染图像XmlWorkerHelper(itextpdf)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我.

我在用pdf渲染图像时遇到问题 即时通讯使用itextpdf 5.5.6和itext 5.5.6

I have problem with rendering image in pdf im usung itextpdf 5.5.6 and itext 5.5.6

我的代码是:

    .
    .
    .


    URL url = new URL("http://some.html");

    URLConnection uc = url.openConnection();
    InputStreamReader inputStreamReader = new InputStreamReader(uc.getInputStream());
     XMLWorkerHelper worker = XMLWorkerHelper.getInstance();

     worker.parseXHtml(pdrwriter, doc, inputStreamReader);

     doc.close();
        //close the writer
     pdrwriter.close();

我的html有:

<table><tr><td><img src="http://mysite/logo.jpg" /></td</tr></table>

,我得到了错误: 找到无效的嵌套标签td,预期关闭标签img.

and i get error: Invalid nested tag td found, expected closing tag img.

我尝试过

<table><tr><td><img src="http://mysite/logo.jpg"></img</td</tr></table> 

sam错误..

您知道如何处理吗?谢谢!!!

do You know how to handel it? Thx for help!!!

推荐答案

如果您要解析的HTML文件存储在与工作目录不同的目录中,则iText将无法创建Image对象.我们必须提供ImageProvider接口的实现,该实现告诉iText如果遇到img标签该怎么办.此界面具有以下方法.

If the HTML file you're parsing is stored in a directory that is different from the working directory, iText won't be able to create Image objects. We have to supply an implementation of the ImageProvider interface that tells iText what to do if an img tag is encountered. This interface has the following method.

Image retrieve(final String src);
String getImageRootPath();
void store(String src, Image img);
void reset();

您可以编写自己的类来实现这四个方法,或者可以对AbstractImageProvider进行子类化.最好是后者.

You can write your own class implementing these four methods, or you can subclass AbstractImageProvider. It is preferred to do the latter.

XML Worker将使用AbstractImageProvider类的store()方法来缓存Map中遇到的所有Image对象.当对具有相同src的图像调用retrieve()方法时,这些对象将被重用.如果您不缓存图像,则PDF将会will肿.相同的图像位和字节将多次写入PDF. reset()方法清除缓存;克隆ImageProvider时使用它.最后,未实现getImageRootPath()方法.您必须自己实现它,如以下代码片段

XML Worker will use the store() method of the AbstractImageProvider class to cache all the Image objects that are encountered in a Map. These objects will be reused when the retrieve() method is called for an image with the same src. If you don't cache images, your PDF will be bloated. The same image bits and bytes will be written to the PDF more than once. The reset() method clears the cache; it is used when an ImageProvider is cloned. Finally, the getImageRootPath() method isn't implemented. You have to implement it yourself, as is done in the following snippet

htmlContext.setImageProvider(new AbstractImageProvider() {

public String getImageRootPath() {

    return "src/main/resources/html/";

}
});

这篇关于渲染图像XmlWorkerHelper(itextpdf)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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