cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

Running Jasper Report from Java code

RK_10805837
6-Contributor

Running Jasper Report from Java code

Version: Windchill 12.0

 

Use Case: We have a custom action where when the user clicks on it, a jasper report is downloaded as pdf in the background


Description:

Hello all,

 

I am trying to run a Jasper Report from java code. I have few questions regarding that-

 

1. I have a jrxml with multiple data sources( infoengine tasks), how do I combine all of them into one Jasper Print object using the below API? 

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters);

 

2. When exporting the jasper report as pdf, how can I give the path so that the report gets automatically downloaded in the user's local system? Currently I am running the report on the server so the pdf is saved in the server location itself, so how can I change that? Below, instead of creating the LfDocumentReport.pdf in the particular location, it creates a pdf in appl/ptc/Windchill_12.0/Windchill location with the whole name given as path

JasperExportManager.exportReportToPdfFile(jasperPrint, "C:\\Users\\55806@itcinfotech.com\\Downloads\\LfDocumentReport.pdf");

 

I have attached the code in this post. Any help or suggestions would be helpful

 

Thankyou,

Ramsha Kamal

5 REPLIES 5

Hi @RK_10805837 

If you want to download the file locally to a client you need to work with a HTTP request and return a response with the downloading file.

PetrH

Hi @HelesicPetr ,

 

Thanks for your reply. I am downloading the pdf in the backend so when I try to pass HTTP request, it returns null and throws NullPointerException. Can you please elaborate on how to do that? Also, can you please give some hint for the first part of the question? How to combine multiple data sources to create Jasper Print object?

Hi @RK_10805837 

It is common method.

//  HTTPResponse response, byte[] byteArray,String downloadfileName
response.setHeader("Content-Disposition", "attachment; filename=\"" + downloadfileName + "\"");
OutputStream os = response.getOutputStream();
os.write(byteArray);
os.flush();

PetrH

Did you try using HttpResponse class to send the pdf to the user?  I have used this many times to send pdf files to the user that initiates a custom action.

olivierlp
Community Manager
(To:RK_10805837)

Hi @RK_10805837,
I wanted to see if you got the help you needed.
If so, please mark the appropriate reply as the Accepted Solution. It will help other members who may have the same question.
Please note that industry experts also review the replies and may eventually accept one of them as solution on your behalf.
Of course, if you have more to share on your issue, please pursue the conversation. 

Thanks,

Olivier
Top Tags