Part-III: Print Web Dynpro Java applications using JasperReports
We have already developed the code for generating PDF report; please refer to Part-II for more information. Here we will only discuss about integrating it into WebDynpro application. We will create WebDynpro application, which will take name as input and generate PDF report accordingly.
You need to copy HelloReport.java and HelloReport.jrxml, to respective folders as shown in the Figure 1, from the project created as part of Part-II of this blog series. If you haven’t read Part-II please read it before going further. Also if you don’t find folders as shown in the figure create them accordingly. Similarly you should also copy your JasperReport jar files to lib folder.
Your WebDynpro application should contain a view as show in the Figure 1 above. Create an input field and its text property should be bind to a context value attribute “name” of type string. Similarly create a button and bind its onAction event to GetReport action. If you go to Implementation tab, you will see a method created onActionGetReport, this method will be the center of our development. Rest of the form elements are just decoration. Code to be placed in your onActionGetReport method is:
public void onActionGetReport(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
{
//@@begin onActionGetReport(ServerEvent)
try{
HashMap data = new HashMap();
data.put(“NAME”, wdContext.currentContextElement().getName());
ArrayList dataList = new ArrayList();
dataList.add(data);
HelloReport report = new HelloReport(dataList);
byte[] pdfData= report.getReportData();
String reportFileName = “HelloJasperReport.pdf”;
IWDResource pdfResource = WDResourceFactory.createResource(pdfData, reportFileName, WDWebResourceType.PDF);
String urlToFile = pdfResource.getUrl(WDFileDownloadBehaviour.AUTO.ordinal());
IWDWindow pdfWindow = wdComponentAPI.getWindowManager().createNonModalExternalWindow(urlToFile, “View Report”);
pdfWindow.setWindowPosition(5, 5);
pdfWindow.setWindowSize(600, 600);
pdfWindow.show();
}catch(Exception e)
{
wdComponentAPI.getMessageManager().reportException(“Couldnot generate downloadble/print version: “+e.getMessage(), true);
}
//@@end
}
Now build your WebDynpro project and run it.
Suggestions:
You could deploy all the jars separately as a external library and use them in your subsequent projects. In this way you don’t have to copy jars to your lib folder and it will make the deployment archive smaller in size.
I found this very interesting blog ...
I followed these steps but I found an error when it comes to this syntax:
JasperReport report = JasperCompileManager.compileReport (HelloReport.class.getResourceAsStream ( "/ FirstExample.jrxml"));
the error is:
Could not generate downloadble / print version: com.sap.engine.lib.xml.parser.NestedSAXParserException: Generic Exception: -> com.sap.engine.lib.xml.util.NestedException: Validation of XML instance document failed. No external pattern is detected.
I put the same jar that you specified.
Have you any idea to solve this problem?
Thanks
Regards
Banana
You can download the source code from the link below:
http://www.jawed.info/1257342000118/WDJJRTest.rar
Thank you for your answer
On the server I put the
-Djava.awt.headless = true
Thanks 1000 for the manual!
Regards
Banana
I am glad that this blog helped you.
I've tried to get the Jasper Report running on my system. Anyway, the program struggles during runtime. It comes to SAXParserExceptions when trying to read the jrxml.
So, to be sure that it's not a version related problem. Which version of SAP Netweaver did you use?
My system is running on 2004s SP20. Maybe too old?
Thanks in advance
Regards, Matthias