[Solved] Create a PDF file in memory


this solution, using itext, primefaces media.

 <p:dataTable widgetVar="tb1" id="tablaFact" var="item" selection="#{listadoFacturasMB.selectedFactura}" selectionMode="single"  paginator="true" 
   rows="20" rowKey="#{item.idFactura}" value="#{listadoFacturasMB.facturaUtilList}"> 

       <p:ajax event="rowSelect" update=":frm1:growl :frm1:dialog" oncomplete="PF('servDialog').show()" listener="#{listadoFacturasMB.createPDF}"/>                         

        <f:facet name="header">
            <h:outputText value="Listado de facturas (Cantidad: #{listadoFacturasMB.cantFact})"/>
        </f:facet>

        <p:column style="width:10%"  headerText="Nro" sortBy="noFactura" filterFunction="#{utilMB.filterByName}"  filterBy="noFactura" filterMatchMode="contains">                          
                        <h:outputText value="#{item.noFactura}"/>
        </p:column>                                                                                     
       </p:dataTable> 

             <p:dialog  resizable="false" closeOnEscape="true" appendTo="@(body)" modal="true" id="dialog" header="Detalles de la factura" widgetVar="servDialog" width="1000px" height="630px">  
                <p:media cache="false" value="#{listadoFacturasMB.fileDownload}" width="100%"  height="600px" player="pdf">
                    <f:param name="id" value="#{listadoFacturasMB.idFile}" />
                </p:media>                   
            </p:dialog> 

Backing bean:

private StreamedContent fileDownload;

  public void createPDF() {
    try {       
        Document pdf = new Document(PageSize.LETTER);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer;
        writer = PdfWriter.getInstance(pdf, baos);
        if (!pdf.isOpen()) {
            pdf.open();
        }

       //Adding content to pdf

        pdf.close();
        String fileName = "factura No " + this.selectedFactura.getNoFactura();

        InputStream stream = new ByteArrayInputStream(baos.toByteArray());
        fileDownload = new DefaultStreamedContent(stream, "application/pdf", fileName);

    } catch (Exception e) {
        JsfUtil.addErrorMessage(e, "Error: createPDF() " + e.getMessage());
    }
}

0

solved Create a PDF file in memory