LC DS 2.5 : Generating PDF from within a Flex application
Mar/0727
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.
3:36 pm on March 23rd, 2007
Looks really nice. Question though – there now seems to be a lot of products under the ‘LiveCycle’ banner – are all of these part of a single purchasable suite or are they all separate items ? For example, if I purchase licences for LiveCycle Data Services, will that include LiveCycle Designer, so I can design printable documents from my LDS Flex App or do I need to purchase further licences in order to design my reports ?
Thanks,
Jamie.
3:44 pm on March 23rd, 2007
Livecycle Designer is a seperate product you would have to invest in. You can buy it online through the Adobe store … price is around 400$ …
On the server side Livecycle is a platform that consists out of a number of modules. Depending on the functionality you need, you would have to buy these modules …
10:42 am on March 24th, 2007
So the LiveCycle DS contains all the functionality to render a template to the PDF format? There’s no need for an Adobe LiveCycle Forms licence?
6:38 pm on March 25th, 2007
Hi Raf,
LC DS will only containt a small part of LC Forms ! LiveCycle DS will only be able to generate static PDF documents, no PDF forms ! PDF forms would still need to be generated through LC Forms …
Christoph
6:45 pm on May 18th, 2007
I’m trying out LiveCycle for pdf generation from my RIA designed with flex. I just need to capture a portion of the screen and generate a pdf on the fly. I’m trying the example code in the documentation though and I can’t access the ImageSnapShot class. Have I not configured something properly?
Thanks
3:58 pm on May 28th, 2007
Hello.
I’ve been playing around with the XFAHelper and templates, but two issues came up, maybe you can give some hints:
1. If the xml dataset that I import into the template does not fit, the pdf is generated even with errors (for instance, the template forms expect a int, but the corresponding mapping in the xml is a String). We want to make documents automatically generated and sent, therefore, would be very important to detect that something went wrong before sending. Would you suggest any strategy for facing this problem? Does LiveCycle includes some methods to open the pdf and count the number of errors?
2. I tested creating my template with the designer from an XSD. However, when merging list data, ony the first item would be displayed. Is this a common error? how may I get list to be displayed properly?
Thanks for all the information already given in the blog!
/daniel
6:00 pm on June 7th, 2007
Where is this ImageSnapshot AS object? There’s nothing in the Flex Builder docs on it.
9:18 am on June 12th, 2007
Hello
I work on flex and like you I want to take a snapshot of a chart and put it in a pdf file.
I see you’re sample and I would know if it’s possible to get more informations about how you do this or some sample code because I tried to search with my friend google and I found nothing clear.
Tkanks you in advance
(Sorry for my english ! )
9:20 am on June 12th, 2007
Oh I forgot How do you get the imagesnapshot library because i want to try but I can’t my flex builder says that mx.graphics.ImageSnapshot is unknow…
9:46 am on June 12th, 2007
Hey,
You would have to make sure your Flex Builder 2 is using the LC DS SDK. I think installing Flex 3 beta will be your best shot. Flex Builder 3 will allow you to select a certain SDK per project.
The example delivered with the LC DS product contains a chart … check out that one …
2:51 pm on June 12th, 2007
I’m sorry but I’m lost… With flex 3 you have libraries ? (snapshot, pdf…)
Do you need LC Designer or you can do it without that?
I downloaded LC DS ES, is that a good one ? Because I don’t understand how it works and I don’t have any example with charts etc like you say
9:30 pm on November 26th, 2007
Why is it so difficult/expensive to generate a PDF from flex… I can do it simple and free from any other web technology… but from the leading RIA from the company that developed PDF it is an expensive and difficlut task?
11:41 pm on December 2nd, 2007
PDF standard has been open and available for others to produce their own PDF generation tools. From within Flex you can also interact with these other frameworks through HTTP or SOAP for example.
With the solution I talk about in this item, I use an Adobe solution that is more productive and allows you to faster create PDF based on a template system. This is a paying solution, as this provides you a better and faster way to build PDF. But as I said, you can use competitive or open source solutions if productivitiy isn’t important for you.
12:56 am on December 15th, 2007
Hello,
I have been having some problems with generating pdf. I installed jboss/livecycle es
etc. I ran designer and made a form with some images which I had hoped to fill in with charts from mxml. I modfied main.mxml example and see my charts when I run it.
when I ry to generate the pdf, i get the blank pdf I created. I am having some trouble understanding how to make the connection from the mx:model in my mxml to the
pdf fields i have put in the template file. I tried with both xdp and pdf services.
they seem to be wokring ok since i get messages from my server side objects.
this would be a great capability to add to my application since I am using reports alot.
any advice/pointers/examples appreciated. a full up example with files that are already working are always appreciated since from there you can substitute until it breaks, etc. even if its just one chart, we woudl be able to open the pdf in livecycle
and see how it is structured. (i suspect thats my problem) anyway, thanks for
all the good info you provide here ! regards,
Brian
8:36 pm on December 15th, 2007
Hi again, I have gotten further, but stll stumbling on the xdp and pdf forms…
could you post/email the dynamic.pdf and xdp files you used for the example?
thanks,
Brian
dealyb@saic.com
9:15 am on February 22nd, 2008
As you say in the final part of your post…
“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. ”
This is exactly my need… I’d like to generate pdf reports in the server side, asynchronously, without user interaction (from database data), having this look and feel we can achieve from flex applications. Anyone knows which could be a good solution for it?
thanks in advance,
Marc
5:01 am on July 29th, 2008
Hi,
I have the same problem as Marc. I have a lot of customers who do not want to do anything, just set-up some options once and let the reports automatically get generated for them on a weekly basis.
What I need is an easy way to generate pdfs which include my flex graphs from the server-side without the user having to interfacing with the online flex application.
Please if anyone knows of a solution it would save me a ton of time.
cheers,
Adrian
10:45 am on August 8th, 2008
to create pdf file from ImageSnapshot byte array on backend(Java) i need just one class XFAHelper witch is static. I need only one jar, containts it… I don`t need to lcds.
but i can`t find in what exactly jar it is and can`t download it. Can I download it lcds to find this jar and add only it to classpath? How do you think?
9:14 pm on August 17th, 2008
Marc/Adrian are correct, this is a challange. In the days of Generator there where a serverside alternative for converting to .pdf (ans lot’s of other formats )- today the focus is on the client. While the client is good for .swf rendering – there is no reuse of code for server side rendering of flex applications to .pdf dumps. This is mots wanted!
Looking forward to any suggestions on how to overcome this issue.
Regards,
rikarddafsi
12:34 am on August 28th, 2008
Hi
We have a POJO that create a PDF as a Byte array. Can I get my Flex app to display the PDF without saving it as a file on the server an linking via a iframe? The only solution I can see at present is to write a web service that does this, but that adds another layer of complexity to teh app which at present has only 2 main objects – the flex cleint and the POJO it acceses via LCSD remoting.
Our flex app needs to display and step though multiple pdfs on screen at the same time.
Thanks for any help
James
7:40 am on August 28th, 2008
Hi James,
The problem is not downloading the byte array directly to the Flex client, but showing the PDF inside your Flex application. Showing PDF inside Flex is not possible. What you can do is convert on the server the PDF to images using LiveCycle ES, and show the images inside your Flex app.
Adobe AIR can show PDF’s inside the application, but I’m not sure if AIR is an alternative for you.
If you have a POJO, you can just use remoting to execute the method that will generate the PDF. As your POJO returns a ByteArray, this will be transformed to an actionscript ‘flash.utils.ByteArray’ object. But again … you won’t be able to do anything with this byte array in Flex.
7:43 am on August 28th, 2008
Rikard, Adrian, Marc,
Generating PDF with charts on the server, in batch mode, is not possible with Data Services. There you would use LC Output. You would also start from an XDP template, and insert the chart images. The difference would be that you would use a server component to generate the chart. There are a number of different java charting server components available.
7:46 am on August 28th, 2008
Victor,
1. I’m not sure only 1 jar would work, doubt it.
2. It will not be supported
3. And probably it is not allowed by the EULA, the licensing contract …
5:23 pm on September 19th, 2008
First off, great example! I was wondering if you could shed some light on a bit of an issue we are having. I used your sample within my flex app, but changed things up a bit on the back end. We are using LC ES 8.2 and i created a workflow with an xml input variable. i merged the xml to a form template then saved it to the file system. The only problem is that i get a black box for the image instead of the image itself. Any ideas as to why it isn’t displaying properly?
8:13 am on April 14th, 2009
Hi.
Do you have an example or documentation for generating PDF with charts on the server, in batch mode.
In the example of adobe http://www.adobe.com/devnet/flex/articles/portable_ria_02.html it is possible to add this functionality?
Thank you in advance.
By
8:26 am on April 20th, 2009
hi how can i import created dataset into pdf? i used XFA Helper,but data is not populated in pdf?
thanks in advance.