How to use a generated Report as attachment

From Wiki

Jump to: navigation, search


In WebRatio it's possible to generate PDF, Excel or RTF reports. This article discusses a particular usage of the WebRatio generated reports. The goal is to create a report in order to use it as attachment for a newsletter (or to store it in the database). Suppose you want to send an email to all the customers with the list of the month's offers as attachment. This list is a PDF report in which there are products details and images. Since this list may vary from a month to another, the report can not be a predefined one, but it has to be calculated every month with the current offers.

Here are the steps in order to make a working example:

  1. Download the "Acme Sample Project" using the menu Help -> WebRatio Samples.
  2. Model the "Offer" report page. You can find information about how to model a report in the WebRatio User Guide in the section Tasks -> Modeling Reports With WebRatio. The model should be something like the figure on the right.
  3. In order to launch explicitely the report generation, model a Script Unit that, in the operation chain of the newsletter sending, calls the report page, generates the PDF report and converts it into a blob that can be used by the Email Unit. The newsletter sending operation chain can be something like the following figure.

This is the Groovy Script for the Script Unit:

//The reportUrl is the complete url of the report page and must 
//contain all the parameters required by the page in order to be
//correctly computed
//inputs=reportUrl|filename

/* The reportUrl is the absolute url to the report page (ex. http://localhost:8080/Acme/pageX.do) 
   The filename is the name of the PDF file to store (ex. OfferList.pdf)
*/

import com.webratio.rtx.RTXBLOBData;
import com.webratio.rtx.blob.BLOBData;
import com.webratio.rtx.RTXBLOBService;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

HttpClient client = new HttpClient();
client.getParams().setCookiePolicy(CookiePolicy.DEFAULT);

GetMethod authget = new GetMethod(reportUrl);
client.executeMethod(authget);

byte[] PDF_File = authget.getResponseBody();
String uploadDir = rtx.getUploadDirectory();

File PDF = new File(uploadDir + File.separator+ filename);
PDF.createNewFile();
FileUtils.writeByteArrayToFile(PDF , PDF_File);
RTXBLOBService fileService = rtx.getBLOBService();
RTXBLOBData blobFile = new BLOBData(fileService.getRelativePath(PDF), rtx);
return blobFile

N.B. The operation chain used in this article to model the newsletter sending is just an example. It's not the best way to do it. In fact, in this example the operation chain is synchronous with the user navigation and so the user has to wait until the operation is finished. A better way to realize this feature is to use a Job. Please refer to the article "Getting started with Jobs" to get more information about Jobs and to see a more efficient model.



Related articles:
Category Difficulty Refers
Context Parameters Web Model Beginner Get Unit
Set Unit
Reset Unit
Create a "Hello world" application Web Model Beginner Site View
WebML
WebRatio
Create a "Simple data centric" web application Web Model Beginner Site View
WebML
WebRatio
Custom Locations and Master pages Web Model Beginner Master Page
Custom Location
Getting started with Groovy Technical Reference Beginner Script Unit
Template
… further results












Personal tools