To answer your question well we need to clarify what product you are using, Jaspersoft is a company, not a product. For example, when you say "the preview inside Jaspersoft", we do not know if you mean the Jaspersoft® Studio designer or the JasperReports® Server report server.
Reports are usually designed in Jaspersoft® Studio or IReport®, and are saved in a file .jrxml
, that file is compiled .jasper
and the latter can be converted to a multitude of formats, including PDF.
The conversion of .jrxml to .jasper to PDF can be done from the same Studio, in the JasperReports® Server or from your own application using JasperReports® Library, this last scenario is the one that we have been trying to solve.
In any case I include my code, (by the way, you have not told us if it is a web or desktop application, this example is from a web application)
//Prefiero usar el .jrxml a la aplicacion que el .jasper por que es mas facil de versionar
String sourceFileName = rutaFisica + "MiReporte.jrxml";
File theFile = new File(sourceFileName);
JasperDesign jasperDesign = JRXmlLoader.load(theFile);//Se carga el archivo
//Si el reporte va a tener un query fijo, puedes omitir este paso
JRDesignQuery newQuery = new JRDesignQuery();
newQuery.setText("SELECT * FROM miTabla WHERE X = Y");
jasperDesign.setQuery(newQuery);
Map parameters = new HashMap();//Parametros que usa el jasperreports
//Este parametro sirve para meter una funcion que el reporte va a ejecutar para encontrar la ruta fisica de sus imagenes
parameters.put("REPORT_FILE_RESOLVER", new FileResolver() {
public File resolveFile(String fileName) {
return new File(getServletContext().getRealPath("") + "\mis_imagenes\"+fileName);
}
});
//Se compila el archivo a .jasper
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
//Aqui se llena el reporte (se ejecuta la consulta)
JasperPrint print = new JasperPrint();
print = JasperFillManager.fillReport(jasperReport, parameters, getConnection());
byte[] pdfBytes = JasperExportManager.exportReportToPdf(print);
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline;filename=" + nombreArchivo + ".pdf");
response.getOutputStream().write(pdfBytes);
response.flushBuffer();
Finally, seeing your final grade:
* Note: I do not want to feed the report from the application, because as I mentioned the table is filled by a query.
And looking at the editions of your question
I have not found a way to generate the report within JAVA; the codes that I found on the internet are from people who fill their reports from the application, but I'm already filling it directly from Jaspersoft
makes me believe (and correct me if I'm wrong) that you think that the .jasper file already includes the data with which the report is fed, and that it is not "feed" by the application, the .jasper file is so only the compiled version of the .jrxml file, if you open the latter with a text editor, you will see that the definition of your query is found ( SELECT * FROM ...
) but no your data, the report should be connected to the database every time the PDF is generated, that's why the getConnection()
in my example.
Or maybe none of us understood you, and the only thing you want is to link your JasperReports® Server to your web application.
Please answer, even if you already solved the problem. To get rid of the doubt.
Greetings.