Wednesday, July 9, 2008

Generate PDF from Flex Application by AlivePDF

Another story about Flex. Now this one is about using AlivePDF to generate PDF file from Flex Application. Its actually very funny that Flex and PDF are both Adobe's products but integrating them together is not that easy (at least there is no existing easy+free solution).

Lets look at what AlivePDF offers. This excellent open-source project is also available at Google Code. Note, generating PDF from AIR application is a little bit different (actually easier). Consult AlivePDF for more details.

In order to generate PDF rather than simply print it to a paper, firstly create a PDF function in MXML script block:
... ...
private function doPrintPDF():void {
var myPDF:PDF = new PDF(Orientation.PORTRAIT,
Unit.MM, Size.A4 );
myPDF.setDisplayMode(Display.FULL_PAGE,
Layout.SINGLE_PAGE );
myPDF.addPage();
myPDF.addImage(this.Template_box, 5, 5, 0, 0,
ImageFormat.JPG, 100, 1, ResizeMode.FIT_TO_PAGE,
"Normal", true);

myPDF.save(Method.REMOTE, "http://localhost/create.php",
Download.ATTACHMENT, "my_flash.pdf");
}
  1. at first create a PDF class which will initialize the required resources for generating a PDF. For example, page size here is A4, and page is portrait.
  2. next you define the page layout settings, very straightforward
  3. after all of these, you can call addPage() to add a page into your myPDF. Remember, you must "add a page" before adding any contents
  4. ok now i have a page already, but its empty. Next, call addImage() to add content into my page. The beauty of addImage() is you can add Flex Display Object into your page. For example, here i "print" my VBox and all its children. Be careful of the numbers: 5, 5, 0, 0. First 5 and 5 mean your "image" will be added 5 pixies after the top-left corner of paper (A4 here). Next 0 and 0 are very tricky. They means "shirk" my image if its too big (bigger than A4 here) or keep it normal when the size is equal or less that the paper (A4 again). Also you can define customized size of your image, for example, 5, 5, 200, 100 means print my image at 5 pixies and image size is 200, 100, regardless of its original size.
  5. save it. Remember you must provide the correct location of your create.php file (simply copy paste it to your_root/Sites if you are using Mac). As for the current time being that Flash player requires server script support for downloading files, you must provide this file when user want to save it to their local machine. On the other hand, use Method.Local for AIR application as it supports local file streams naturally.
Already, all we need is done. Next in your application, add a button as:

< label="Print PDF" click="doPrintPDF()">

That's it. Oh BTW, gonna say sorry to AlivePDF's author Thibault Imbert that i have created a bug report in Google Code but that i didn't realize that providing 0 and 0 in addImage() will "shirk" the image automatically. Well actually i found this from the source code, but anyway, its great that i dont have to write my own ratio calculation function. :)

No comments: