在使用XMLWorkerHelper解析html文本后,Pdf Writer不会打印所有html文本

时间:2015-11-24 17:48:30

标签: java html pdf itext

我有 html 文字。

<p>Use your card when shopping online at Our store and receive 20% off on your very first order.</p> <p>Our store delivers the latest styles at affordable prices for young trendsetters. For those who love putting a sexy spin on their everyday wardrobe, look no further--Our store is the ultimate style destination! Shop the latest pants, dresses and more. Shop now! We've got everything you're looking for.</p> <div> <p>To redeem this offer, click the Redeem button, sign up for our newsletter, and we will email you a discount code, entitling you to 70% off on your first order.</p> </div>

我正在使用 XMLWorkerHelper 解析它,如下所示。

StringReader strReader = new StringReader("<html><head><style> body {font-family:ArialUnicodeMS; font-size:9pt;} </style></head><body>" + htmlString+"</body></html>");
            try {
                PdfHtmlElementHandler eh = new PdfHtmlElementHandler();
                p = new Paragraph();
                XMLWorkerHelper.getInstance().parseXHtml(eh, strReader);
                List<Element> htmlList = eh.getHtmlList();
                for(int i=0; i<htmlList.size(); i++) {
                    p.addElement(htmlList.get(i));
                }
            }
           PdfPCell cell_2 = new PdfPCell();
           PdfPTable table = new PdfPTable(2);
           cell_2.addElement(p);
           table.addCell(cell_2);

现在,请仔细查看我的html文字。最后有一个 <div> 标记。在使用XMLWorkerHelper解析后,我在对象中添加元素,就像这样

 p.addElement(htmlList.get(i));

现在生成的pdf没有显示完整的文本。它通过修剪上面的<div>标记文本来打印文本。

在我们的商店在线购物时使用您的卡,并在第一笔订单上享受八折优惠。

我们的商店为年轻的潮流引领者提供价格合理的最新款式。对于那些喜欢在日常衣橱上打造性感的人来说,不要再犹豫了 - 我们的商店是终极风格的目的地!购买最新的裤子,连衣裙等。现在去购物!我们有你想要的一切。

如何解决这个问题。我希望打印全文。这是因为我要添加到段落对象中? 因为当我在代码下面使用时。

StringReader strReader = new StringReader("<html><head><style> body {font-family:ArialUnicodeMS; font-size:9pt;} </style></head><body>" + htmlString+"</body></html>");
            try {
                PdfHtmlElementHandler eh = new PdfHtmlElementHandler();
                pdfDiv = new PdfDiv();
                XMLWorkerHelper.getInstance().parseXHtml(eh, strReader);
                List<Element> htmlList = eh.getHtmlList();
                for(int i=0; i<htmlList.size(); i++) {
                    pdfDiv.addElement(htmlList.get(i));
                }
            }
           PdfPCell cell_2 = new PdfPCell();
           PdfPTable table = new PdfPTable(2);
           cell_2.addElement(pdfDiv);
           table.addCell(cell_2);

正在打印全文。我刚用pdfDiv替换了。但我又遇到了另一个问题。

如果文字长度很大且无法在一页上打印,则应将其打印在其他页面上。但在pdfDiv的情况下不会发生。

综述: -

  • p在两页上打印,但不是完整的html文本。
  • 使用pdfDiv完全打印html文本,但如果文本很大 ,它会在一个pdf页面上打印那么多文字

请帮忙。真的很紧急。我正在使用 Itext 库进行pdf生成。

1 个答案:

答案 0 :(得分:0)

通过将itext的版本从 5.3.2更改为5.5.7 来解决问题。