Jump to content
We've recently updated our Privacy Statement, available here ×

How can I generate report bytes?


jcatubay

Recommended Posts

I'm using

JasperReport jasperReport = JasperCompileManager.compileReport(reportFile.getPath());

bytes = JasperRunManager.runReportToPdf(

jasperReport,

parameters);

return bytes;

for some reason it returns a null value. What are the conditions so I can use this version of the runReportToPdf. I'm using hibernate and spring

Thanks!

Link to comment
Share on other sites

  • Replies 7
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

How did you generate your byte array? Did you have a query in your application to get the data? That is what I'm doing right now, I create a JRDatasource in my application the fill it report with the data. I'm currently using:

 

bytes = JasperRunManager.runReportToPdf(

JasperReport jasperReport,

Map parameters,

JRDatasource ds

)

 

But I want to get away with queries in my code and just use the queries stored in my reports. I want to do something like this

 

bytes = JasperRunManager.runReportToPdf(

JasperReport jasperReport,

Map parameters);

 

But for some reason it returns a null value for my bytes.

Thanks!

Link to comment
Share on other sites

i have my data in a database and i give the jasperFillManager a DB-connection.

 

my code looks like this:

Code:

try {
Connection conn = getConnection();
jasperReport = JasperCompileManager.compileReport("reports/Gesamt_Monat.jrxml"«»);
jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), conn);

closeDB(conn);

byte[] outputFile = JasperExportManager.exportReportToPdf(jasperPrint);

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

and this byte[] ouputFile, i can use for streams.

 

But if you don't want to have queries in your code, did you try with JREmptyDataSource?

 

I really dont know, if this helps you in any way, but i don't have experience with hibernate/spring...

Link to comment
Share on other sites

I dont like to have queries in my code but I still want to be able to fill me report using the reports query. Would JREmptyDataSource give empty report. The program right now is using the parameters, my program generates a resultset by executing the queries in my program, then make that into a JRDataSource. After getting the JRDataSource, together with jrxml file, I pass them to a method that generates my byte[].This is the method:

public byte[] generatePDFReport(File reportFile, Map parameters, JRDataSource ds) {

byte[] bytes = null;

try {

JasperReport jasperReport = JasperCompileManager.compileReport(reportFile.getPath());

bytes = JasperRunManager.runReportToPdf(

jasperReport,

parameters,

ds);

} catch (JRException e) {

e.printStackTrace();

}

return bytes;

}

After I get the bytes, I pass it to another method:

public void sendToPDFResponse(byte[] bytes){

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

response.setContentType("application/pdf");

sendToResponse(response, bytes);

}

to publish it in the web.

What I want to do now is bypass the method where I get my JRDataSource using my programs query. I want to find a way to just use the query in my xml to get my data and generate byte[]. I saw a method in my JasperManager that have this interface:

bytes = JasperRunManager.runReportToPdf(

jasperReport,

parameters);

return bytes;

So it uses just the parameters and jasperreport but when I use it it generates just a null byte[].What are the precondition for me to be able to use this method and generate my data?

Thanks a lot for helping with this, I'm new to JasperReport but I think it's cool and I want to learn more about it....

Link to comment
Share on other sites

If you want to use the report query to retrieve data, you need to pass the data connection to the report in one of the following ways:

If the query is SQL and you are using JDBC to execute it, by calling
Code:
JasperRunManager.runReportToPdf(jasperReport, parameters, jdbcConnection);

In the general case, the connection parameter should be included in the parameters map, e.g.

Code:
[code]//for SQL queries
parameters.put(JRParameter.REPORT_CONNECTION, jdbcConnection);
//or for HQL queries
parameters.put(JRHibernateQueryExecuterFactory.PARAMETER_HIBERNATE_SESSION, hibernateSession);
//etc
[/ul]

 

HTH,

Lucian

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...