【问题标题】:changing style on generating pdf with Primefaces dataExporter使用 Primefaces dataExporter 更改生成 pdf 的样式
【发布时间】:2011-07-01 14:52:44
【问题描述】:

我正在使用 Primefaces dataExporter 从数据表生成 pdf。生成的 pdf 具有相同宽度的所有列。我正在寻找一种方法来更改 postProcessor/preProcessor 函数上的表格样式。我可以在生成 pdf 之前使用 setHtmlStyleClass 方法更改某些内容吗?我尝试使用它,但没有成功。我想我没有正确理解它。

public void preProcessPDF(Object document) throws IOException, BadElementException, DocumentException {

    Document pdf = (Document) document;
    pdf.setHtmlStyleClass("reportClass");
    ...
}

如果我可以使用该方法,我可以在哪里定义 reportClass ?是浏览器上页面的css类吗?

【问题讨论】:

    标签: primefaces primefaces-dataexporter


    【解决方案1】:

    如果你看看PDFExporter.java导出方法中发生了什么,PDF中的数据表是无法操作的。

    首先创建一个com.itextpdf.text.Document 对象。

    Document document = new Document(PageSize.A4.rotate());
    

    然后调用 preProcessor 方法传递 Document,这是在将表格添加到 PDF Document 之前。

    if(preProcessor != null) {
        preProcessor.invoke(facesContext.getELContext(), new Object[]{document});
    }
    

    然后创建com.itextpdf.text.pdf.PdfPTable。 exportPDFTable 方法不做任何特殊的格式化。

    PdfPTable pdfTable = exportPDFTable(table, excludeColumns);
    document.add(pdfTable);
    

    现在调用 postProcess 方法并再次传递 Document。在这里,我认为您将能够从 Document 对象访问和更改 PdfPTable,但查看 iText api 看起来您无法做到。

    if(postProcessor != null) {
        postProcessor.invoke(facesContext.getELContext(), new Object[]{document});
    }
    

    因此,如果您想要一个样式化的 PDF 表格,您将不得不实现自己的 PDF 导出。希望看看 PrimeFaces PDFExporter 是如何完成的对您有所帮助。

    【讨论】:

    • 感谢马克提供详细信息。我会考虑实现我自己的 PDF 导出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多