I had an issue generating PDF from dynamic components from Flex. I used a VBox and added buttons as child dynamically.  I created pages each time a child is added and at the end generated the PDF. The PDF created blank pages.  I posted the issue in Kalen Gibbons’ Blog. Thanks to Kalen……the issue was solved. I would like to share the code with all of you.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" initialize="initializeHandler()">

<mx:Script>
<![CDATA[
import mx.events.CloseEvent;
import mx.controls.Alert;
import org.alivepdf.saving.Download;
import org.alivepdf.images.ResizeMode;
import org.alivepdf.saving.Method;
import org.alivepdf.images.ImageFormat;
import org.alivepdf.pages.Page;
import org.alivepdf.display.*;

import org.alivepdf.layout.*
import org.alivepdf.pdf.PDF;

private var mPDF:PDF;
private var numChildrenToAdd:Number = 10;

private function initializeHandler():void{
//initialize PDF
mPDF = new PDF(Orientation.PORTRAIT, Unit.MM, Size.A4 );
mPDF.setDisplayMode( Display.FULL_PAGE, Layout.SINGLE_PAGE );
}

private function onPDFCreate():void
{
/* Kick off a loop of adding children to the vbox.
When the child is added, the updateComplete handler will add
a page to the PDF and add another button to the vbox, which will
trigger the updateComplete handler and start the loop over again...
until all children have been added. */

var btn:Button = new Button();
btn.label = 'Click 1';
vbox.addChild(btn);
}

private function updateHandler():void{
//get the current number of children
var currentChildren:int = vbox.numChildren;
if(currentChildren == 0){ return; }

//add new page
var newPage:Page = new Page( Orientation.PORTRAIT, Unit.MM, Size.A4 );
mPDF.addPage( newPage );
mPDF.addImage(vbox, 0, 0, 0, 0, ImageFormat.PNG, 100, 1 );

//keep the loop going until all children have been addded
if(currentChildren < numChildrenToAdd){
var btn:Button = new Button();
btn.label = 'Click '+ (currentChildren+1);
vbox.addChild(btn);
}else if(currentChildren == numChildrenToAdd){
//all children are added so it's okay to print now
//mPDF.save(Method.REMOTE, "http://kalengibbons.com/assets/pages/pdfCreator.cfm", Download.INLINE);
Alert.show('Do u wanna pdf?', 'PDF Creation', Alert.OK|Alert.CANCEL, this, alertListener, null, Alert.OK);
}
}

private function alertListener(vEvent:CloseEvent):void
{
if(vEvent.detail == Alert.OK){
var file:FileReference = new FileReference();
file.save(mPDF.save(Method.LOCAL), "Module.pdf.pdf");
}
}

]]>
</mx:Script>

<mx:VBox id="vbox" borderStyle="solid" borderColor="0xff00ff"
width="100%" height="100%" updateComplete="updateHandler()" />
<mx:Button id="btn" label="CreatePDF" click="onPDFCreate()"/>

</mx:Application>
Share