2013-06-19 63 views
1

我想用來生成pdf數據導出器,使用方法預處理器插入一些內容。通過給字體大小頁面吸收文本格式。然後製作一個分頁符將圖表放在一個新頁面上,問題是生成其他大小的第二頁面,並找到更改導出表格文本字體大小的方法。在Primefaces中更改默認格式dataExporter

<h:commandLink> 
     <p:graphicImage value="/images/pdf.png"/> 
      <p:dataExporter type="pdf" target="dataTableAddDetalles" fileName="pdf" preProcessor="#{serviciosMB.preProcessPDF}"/> 
</h:commandLink> 

支持豆

public void preProcessPDF(Object document) throws Exception { 
    try { 
     Document pdf = (Document) document; 
     pdf.open(); 
     pdf.setPageSize(PageSize.LETTER); 

     ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext(); 
     String logo = servletContext.getRealPath("") + File.separator + "images" + File.separator + "header.gif"; 

     // pdf.add(Image.getInstance(logo)); 
     pdf.add(new Paragraph("EMNI", FontFactory.getFont(FontFactory.HELVETICA, 22, Font.BOLD, new Color(0, 0, 0)))); 
     SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yyyy"); 

     pdf.add(new Phrase("Fecha: " + formato.format(new Date()))); 
     pdf.newPage(); 
    } catch (Exception e) { 
     //JsfUtil.addErrorMessage(e, e.getMessage()); 
    } 
} 

回答

4

的primefaces文檔(如4.0)沒有提到任何編寫自定義數據導出能力,只有預先&後處理器,其中在的情況下PDF阻止你做大量的修改數據等

但是你可以做的是在您的項目稱爲創建一個包

org.primefaces.component.export 

並從primefaces源複製ExporterFactory.java。 然後您可以用您自己的實施替換原來的PDFExporter調用。

出口商的實施相當簡單。它使用iText庫(雖然是一個過時的版本),您可以輕鬆地將其擴展到您的需求。

這種方法的一個顯而易見的問題是,在將來(如果)您正在更新您的primefaces庫時,您可能必須格外小心。

6

你不能這樣做,你想用什麼dataexporter,則需要更改您的代碼:

<h:commandLink actionListener="#{serviciosMB.createPDF}"> 
    <p:graphicImage value="/images/pdf.png" /> 
</h:commandLink> 

而且你的託管bean:

public void createPDF() { 
    try { //catch better your exceptions, this is just an example 
     FacesContext context = FacesContext.getCurrentInstance(); 
     Document document = new Document(); 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     PdfWriter.getInstance(document, baos); 

     if (!document.isOpen()) { 
      document.open(); 
     } 

     PdfPTable pdfTable = exportPDFTable(); 
     document.add(pdfTable); 

     //Keep modifying your pdf file (add pages and more) 

     document.close(); 
     String fileName = "PDFFile"; 

     writePDFToResponse(context.getExternalContext(), baos, fileName); 

     context.responseComplete(); 

    } catch (Exception e) { 
     //e.printStackTrace();   
    } 
} 

exportPDFable方法:

private PdfPTable exportPDFTable() { 
    int numberOfColumns = 1; 
    itemOfList item = null; 
    PdfPTable pdfTable = new PdfPTable(numberOfColumns); 
    pdfTable.setWidthPercentage(100); 
    BaseFont helvetica = null; 
    try { 
     helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED); 
    } catch (Exception e) { 
     //font exception 
    } 
    Font font = new Font(helvetica, 8, Font.NORMAL); 
    pdfTable.addCell(new Paragraph("columnName", font));   

    for (int i = 0; i < lstPdfTable.size(); i++) { //lstPdfTable is the list from your datatable. A List of "itemOfList" type 
     item = new itemOfList(); 
     item = lstPdfTable.get(i); 
     //pdfTable.addCell(new Paragraph('any_string_field', font)); 
     pdfTable.addCell(new Paragraph(item.getStringField(), font));   
    } 
    return pdfTable; 
} 

和writePDFToResponse方法是:

private void writePDFToResponse(ExternalContext externalContext, ByteArrayOutputStream baos, String fileName) { 
    try { 
     externalContext.responseReset(); 
     externalContext.setResponseContentType("application/pdf"); 
     externalContext.setResponseHeader("Expires", "0"); 
     externalContext.setResponseHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); 
     externalContext.setResponseHeader("Pragma", "public"); 
     externalContext.setResponseHeader("Content-disposition", "attachment;filename=" + fileName + ".pdf"); 
     externalContext.setResponseContentLength(baos.size()); 
     OutputStream out = externalContext.getResponseOutputStream(); 
     baos.writeTo(out); 
     externalContext.responseFlushBuffer(); 
    } catch (Exception e) { 
     //e.printStackTrace(); 
    } 
} 
+0

謝謝,我試着從primefaces擴展出口,出口是好的,你可以做一個自定義PDF,但也是有限的,因爲你不能自定義表和更多的東西。我正在使用itext製作pdf,每件事情都可以......比你... – meyquel

+0

是的,出口商是一個很好的幫助,但正如你所說,它是有限的。 – danRod

+0

非常感謝。我喜歡PrimeFaces,但在PDF數據導出器中的支持並不好。這使得它很容易實現。 – Namphibian