我使用Primefaces dataExporter从dataTable生成pdf。生成的pdf具有相同宽度的所有列。我正在寻找一种方法来更改postProcessor/preProcessor函数上的表的样式。在生成pdf之前,我可以使用setHtmlStyleClass方法更改某些内容吗?我试着用它,但没有成功。我想我没有正确理解。
public void preProcessPDF(Object document) throws IOException, BadElementException, DocumentException {
Document pdf = (Document) document;
pdf.setHtmlStyleClass("reportClass");
...
}
如果我可以使用该方法,我在哪里可以定义reportClass?它是浏览器上页面的css类吗?
如果你看看PDFExporter.java导出方法中发生了什么,PDF中的数据表就无法操作。
首先创建一个com.itextpdf.text.Document
对象。
Document document = new Document(PageSize.A4.rotate());
然后调用preProcessor方法传递文档,这是在将表添加到PDF文档之前。
if(preProcessor != null) {
preProcessor.invoke(facesContext.getELContext(), new Object[]{document});
}
然后创建CCD_ 2。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是如何完成的,这将对您有所帮助。