从html字符串中提取并使用生成pdf

时间:2018-10-12 06:59:55

标签: itext portlet pdftables

我正在尝试从字符串中提取 table 标记(html),并将其作为pdf表输出,并下载到本地。

作为包含以下内容的字符串: html的内容是动态的,所以我不能按单元格或逐行进行映射。

例如。

private String message = "<html><body><p class=\"MsoNormal\"><b><span style=\"color: rgb(68, 84, 106);\">Dear Agent,<br><br>Please be informed that because no TRMF or reason for delay were received by the due date mentioned below, we consider the Transaction to be Paid in Error. We are going to act accordingly which means charging the Paying Account in case we are not able to defend legal dispute without TRMF.</span></b><span style=\"font-size: 10pt; line-height: 14.2667px;\"><o:p></o:p></span></p><p class=\"MsoNormal\"><span style=\"font-size: 10pt; line-height: 14.2667px;\">&nbsp;</span></p><div><span style=\"font-size: 10pt; line-height: 14.2667px;\"><br></span></div><table class=\"MsoNormalTable\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"0\" style=\"width: 472.9pt; margin-left: 5.9pt;border-collapse: collapse;\"><tr><td>Neeraj</td><td>Chand</td></tr><tr><td>Sowmya</td><td>Javvadi</td></tr></table></body></html>";

我将收到这样的字符串,它将保留html内容。我必须生成与此类内容相对应的pdf文件。输入字符串可能具有或可能没有任何表内容。

我在下面尝试过,但是它不起作用,并且我收到“表宽度不能为0”的错误消息。

public StreamedContent getFile() throws IOException, DocumentException {
        final PortletResponse portletResponse = (PortletResponse) FacesContext.getCurrentInstance().getExternalContext()
                .getResponse();
        final HttpServletResponse res = PortalUtil.getHttpServletResponse(portletResponse);
        res.setContentType("application/pdf");
        res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        // res.setHeader("Content-Disposition", "attachment; filename=\".pdf\"");
        res.setHeader("Content-Disposition", "attachment; filename=" + subject + ".pdf");
        res.setHeader("Refresh", "1");
        res.flushBuffer();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStream out = res.getOutputStream();
        Document document = new Document(PageSize.LETTER);
        PdfWriter.getInstance(document, baos);
        document.open();
        document.addCreationDate();
        /* without parsing html, it works and generates pdf
        Table table = new Table(2, 2);
        document.add(new Paragraph("converted to PdfPTable:"));
        table.setConvert2pdfptable(true);
        document.add(table);
         */

        //below doesn't work
        HTMLWorker htmlWorker = new HTMLWorker(document);
        String str = this.getMessage();
        htmlWorker.parse(new StringReader(str));
        PdfPTable table= new PdfPTable(2); // not sure what to give here as nummber of columns is dynamic
        table.setTotalWidth(document.getPageSize().getWidth() - 80);
        document.add(table);
        document.close();
        baos.writeTo(out);
        out.flush();
        out.close();
        return null;
    }

是否可以通过提供的任何html字符串生成pdf?或者,如果我可以使用其他工具,请告诉我。

0 个答案:

没有答案