Thursday, August 25, 2011

Using AlivePDF to export PDF in Flex

I have recently been working with AlivePDF for exporting UIComponents into PDF and liked it very much. It is a simple tool, easy to use. A small code snippet

private function exportToPDF(uiComponent:IUIComponent):void

{
_fileReference.addEventListener(Event.COMPLETE, 
function callOnComplete(event:Event):void
{
if(onComplete != null) 
onComplete.call(null, event);
_fileReference.removeEventListener(Event.COMPLETE, callOnComplete);
})
_fileReference.addEventListener(Event.CANCEL, 
function callOnCancel(event:Event):void
{
if(onCancel != null) 
onCancel.call(null, event);
_fileReference.removeEventListener(Event.CANCEL, callOnCancel);
})

var pdfEncoder:PDF = new PDF(Orientation.PORTRAIT, Unit.MM, Size.A4);
pdfEncoder.setDisplayMode(Display.REAL);
pdfEncoder.addPage();
pdfEncoder.addImage(uiComponent as DisplayObject, 
                        new Resize("FitToPage", Position.LEFT));


var bytes:ByteArray = pdfEncoder.save(Method.LOCAL);
_fileReference.save(bytes, (fileName + ".pdf"));
}


I am assuming that we are using Flash 10. For player below that version, FileReference would not work. You'd need to do a server side implementation to download the PDF.

If anyone has any questions, please feel free to ask.


No comments :