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. :(((
– João Neto
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.
– Wakim
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.
– João Neto
Then you must use the
JasperExportManager.exportToPdfFile
passing the file name/path. Or use theJasperExportManager.exportToPdfStream
passing aFileOutputStream
. If you want to record directly in the bank, without generating file, I suggest using theByteArrayOutputStream
with theexportToPdfStream
and use thetoString
.– Wakim
As soon as I get to college again I’ll test and give feedback. Jaja I’m going back there.
– João Neto
But I only use this method, and I keep everything else? What about my Jrbeancollectiondatasource?
– João Neto
Yes, the
JRBeanCollectionDataSource
is used to pass aDataSource
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?– Wakim
Generating on demand is an alternative too. How would I generate on demand?
– João Neto
When the user requests, through a button. You would execute the question code.
– Wakim
Exactly the way it is? Because I tried it and it didn’t work =(
– João Neto
Are you using ajax? This code does not work with ajax.
– Wakim
I’ll edit the code showing the code I call it.
– João Neto
To make a
p:commandButton
do Submit, use attribute:ajax="false"
.– Wakim
It’s returning this error now: javax.servlet.Servletexception: Method not found: [email protected]() But I have the insert methodEdital.
– João Neto
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?– Wakim
Let’s go continue this discussion in chat.
– João Neto