0

我正在尝试从字符串中提取表格标签(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?或者,如果有任何其他工具可以用于此,请告诉我。


当一个元素被动态创建时,任何预定义的事件都不会附加到它上面。因此需要在父元素上注册事件监听器,如下所示。

$("body").on("click", '.date', function(event) {
    alert( $(event.currentTarget).attr('value'));
});

第二个参数on是要触发事件的目标(动态创建的元素的类)。

4

0 回答 0