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 )
HashMap data = new HashMap();
ArrayList dataList = new ArrayList();
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”);
wdComponentAPI.getMessageManager().reportException(“Couldnot generate downloadble/print version: “+e.getMessage(), true);
Now build your WebDynpro project and run it.
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.