使用 Primefaces dataExporter 更改生成 pdf 的样式

发布于 2024-11-17 23:43:34 字数 483 浏览 5 评论 0 原文

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

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

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

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

I'm using Primefaces dataExporter to generate pdf from a dataTable. The pdf generated has all the columns with the same width. I'm looking for a way to change the style of the table on the postProcessor/preProcessor functions. Can I use the setHtmlStyleClass method to change something before generating pdf? I tried to use it, but with no success. I think I didnt understand it correctly.

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

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

If I can use that method, where can I define reportClass ? Is it a css class for the page on the browser?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风筝在阴天搁浅。 2024-11-24 23:43:34

如果您查看 PDFExporter.java导出方法,无法操作PDF中的数据表。

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

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

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

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 的工作原理能够对您有所帮助。

If you look at whats going on in the PDFExporter.java export method, the data table in the PDF can not manipulated.

First a com.itextpdf.text.Document object is created.

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

Then the preProcessor method is called passing the Document, this is before the table is added to the PDF Document.

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

Then the com.itextpdf.text.pdf.PdfPTable is created. The exportPDFTable method doesn't do any special formatting.

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

Now the postProcess method is called and the Document is passed again. Here I would think you would be able to access and change the PdfPTable from the Document object but looking at the iText api it doesn't look like you can.

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

So if you want a styled PDF table your going to have to implement your own PDF export. Hopefully looking at how the PrimeFaces PDFExporter is done will help you with that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文