Livecycle Data Services 2.5 (previously known as Flex Data Services) has got a new feature that will allow you to generate PDF files. Cool stuff certainly if look at the way we integrate a part of the Livecycle solution.
Adobe Livecycle allows you already a while to generate PDF based on a template and data (xml). The template is being build using Livecycle Designer. LC Designer offers you a WYSIWYG interface to build templates. The data(xml) you will be merging into the template is being displayed as a tree structure. By using drag and drop, you can link a data element with a visual control in the template. This solution is based on the XFA specification.
This functionality will now be available for Flex developers through LC Data Services. Your Flex application will have to generate a XML file, so LC DS can merge the XML data with your XFA template. (the template is being build with LC Designer). To build the XML within your flex application use the mx:XML tag. This makes life easy as you can just reference variables to fill up the XML.
<mx:XML id=”xmlModel”>
<CompanyReport>
<TitleText>{panel.title}</TitleText>
<OverviewText>{overviewText.text}</OverviewText>
<BalanceSheetImage>{balanceSheetImage}</BalanceSheetImage>
<EarningsImage>{earningsImage}</EarningsImage> </CompanyReport>
</mx:XML>
Nice thing is that you can put images into the XML and map them in the XFA template with an image control. And to make it complete, the ImageSnapshot actionscript object, allows you to create an image/screenshot from flex controls in your Flex application. Check out this code example …
var snapshot:ImageSnapshot = ImageSnapshot.captureImage(balanceSheet);
balanceSheetImage = ImageSnapshot.encodeImageAsBase64(snapshot);
You just have to reference the balanceSheetImage variable into the XML model. ImageSnapshot can take a snapshot from any UIComponent. Think especially on chart components that can be shown in PDF.
The actual creation of the PDF will happen on the server, by calling a Remote Object. You’ll have to create a POJO that will use the “flex.acrobat.pdf.XFAHelper” java object (part of LC DS). Here is an example on how this might look like …
example : calling the remote object in Flex
<mx:RemoteObject id=”service” destination=”PDFService” fault=”faultHandler(event)” result=”resultHandler(event)” />
// call the remote POJO method (as defined below), giving the XML model as an attribute …
service.generatePDF(xmlModel);
example : java source code of object that calls XFAHelper
// define a string with a link to the XFA template
String source = FlexContext.getServletContext().getRealPath(”/pdfgen/company.pdf”);
XFAHelper helper = new XFAHelper();
helper.open(source);
// Import XFA dataset : dataset is the XML constructed in your Flex application. dataset is a org.w3c.dom.Document
helper.importDataset(dataset);
// Save new PDF as a byte array in the current session
byte[] bytes = helper.saveToByteArray();
Download the Livecycle DS beta from http://labs.adobe.com and try it out for yourself.
Don’t forget that this solution is generating the chart on the client. So this is not a solution for generating charts on the server and merge them with a XFA template.
Here you can find an example of a PDF generated through LC DS.