How to create PDF from . Jasper

Asked

Viewed 468 times

0

I created a report on iReports and managed . Jasper, but I have no idea how to generate the PDF, I’ve tried several tutorials and nothing works, to create the pdf. What I need is to generate the PDF and take the path of the file created to set within a field in the database. I was trying to use this method, does not return any error, but also does not create the file.

   public void imprimirRelatorio() throws IOException, ParseException {

        HashMap parametros = new HashMap();
        parametros.put("ID_EDITAL", 28);

        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();

        response.setContentType("application/pdf");

        response.setHeader("Content-disposition",
                "inline; filename=\"arquivo.pdf\"");
        try {
            JRBeanCollectionDataSource dsColecao = new JRBeanCollectionDataSource(lista, false);
            JasperPrint impressoraJasper = JasperFillManager.fillReport("d:/Edital.jasper", parametros, dsColecao);
            ServletOutputStream streamDeSaida = response.getOutputStream();
            JasperExportManager.exportReportToPdfStream(impressoraJasper,
                    streamDeSaida);
        } catch (JRException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        facesContext.responseComplete();    
    }
}

Where I make the method call:

                    <p:commandButton value="Submit"
                        actionListener="#{editalBean.inserirEdital}" update="growl"
                        process="@this" />

When calling editalBean’s inserEdital method it calls the printRelatory method().

The list I pass on the datasource in this case is a list of Notices. Could someone help me? Give a direction? I’m totally lost.

  • For God’s sake, I really need this, I give all my points if possible to those who help me. :(((

  • Do you want to write the Jasper output pdf straight to disk? Or do you want to write to the bank in a blob type column? Or want to write the pdf in the answer to this request? The code in the question makes the third.

  • I found this code on the Internet, I do not know much that you do to be honest :((. But, I need to write the pdf on disk (inside the web-content folder for example), and then take the path of that pdf to save in the database in a field of type String.

  • Then you must use the JasperExportManager.exportToPdfFile passing the file name/path. Or use the JasperExportManager.exportToPdfStream passing a FileOutputStream. If you want to record directly in the bank, without generating file, I suggest using the ByteArrayOutputStream with the exportToPdfStream and use the toString.

  • As soon as I get to college again I’ll test and give feedback. Jaja I’m going back there.

  • But I only use this method, and I keep everything else? What about my Jrbeancollectiondatasource?

  • Yes, the JRBeanCollectionDataSource is used to pass a DataSource that your report expects, the only change is the method you will call in the export. Is it really necessary to save the pdf in the bank (this generates big impacts)? Why doesn’t it generate on demand?

  • Generating on demand is an alternative too. How would I generate on demand?

  • When the user requests, through a button. You would execute the question code.

  • Exactly the way it is? Because I tried it and it didn’t work =(

  • Are you using ajax? This code does not work with ajax.

  • I’ll edit the code showing the code I call it.

  • To make a p:commandButton do Submit, use attribute: ajax="false".

  • It’s returning this error now: javax.servlet.Servletexception: Method not found: [email protected]() But I have the insert methodEdital.

  • Was it no longer returning error before and had no message in the (server) console? The inserirEdital this declared as: public void inserirEdital()? You have no argument?

Show 11 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.