<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="creationCompleteHandler()" viewSourceURL="assets/pages/srcview/PrintDataExample/index.html">
<mx:Style source="assets/styles/yflexskin.css" />
<mx:Model id="contactsData" source="data/contacts.xml" />
<mx:Script>
<![CDATA[
import mx.events.CloseEvent;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.collections.ArrayCollection;
[Bindable]
private var contacts:ArrayCollection;
[Bindable]
private var printOptions:Array = [ {data: 'inline', label: 'Display in Browser'},
{data: 'attachment', label: 'Save/Open from Browser'},
{data: 'fromFlash', label: 'Save from Flash Player 10'}];
private var pdfData:ByteArray;
private function creationCompleteHandler():void{
contacts = new ArrayCollection(contactsData.contact);
}
private function doPrint():void{
cfService.printSampleData({ printTitle: 'Contact data for Your Company',
dataToPrint: contacts,
printLetterhead: includeHeaderOptions.selectedValue});
}
private function resultHandler(event:ResultEvent):void{
pdfData = event.result as ByteArray;
switch(printOptionsCb.selectedItem.data){
case "fromFlash":
var player:Array = getPlayerVersion();
if(player[0] < 10){
Alert.show("You must have Flash Player version 10 or higher. " + player[0]);
return;
}
Alert.show("Permit system access to save this file?", "Confirmation", Alert.OK |Alert.CANCEL, null, confirmationHandler);
break;
case "attachment":
generatePDF(pdfData, 'attachment');
break;
default:
generatePDF(pdfData, 'inline');
}
}
private function faultHandler(event:FaultEvent):void{
Alert.show("There was a problem connecting to the server", "ERROR");
}
private function confirmationHandler(event:CloseEvent):void{
if(event.detail == 4)
savePDF(pdfData);
}
private function savePDF(pdfBinary:ByteArray):void{
var fileRef:FileReference = new FileReference();
fileRef.save(pdfBinary, "yourPrintout.pdf");
}
private function generatePDF(pdfBinary:ByteArray, method:String):void{
var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
var urlString:String = "http://kalengibbons.com/assets/pages/pdfCreator.cfm";
if(method == "inline")
urlString += "?method=inline";
else
urlString += "?method=attachment&name=dataPrintSample.pdf";
var sendRequest:URLRequest = new URLRequest(urlString);
sendRequest.requestHeaders.push(header);
sendRequest.method = URLRequestMethod.POST;
sendRequest.data = pdfBinary;
navigateToURL(sendRequest, "_blank");
}
private function getPlayerVersion():Array{
var playerDetails:String = Capabilities.version;
var osDetails:Array = playerDetails.split(' ');
return osDetails[1].split(',');
}
]]>
</mx:Script>
<mx:RemoteObject id="cfService"
destination="ColdFusion"
endpoint="/flex2gateway/"
showBusyCursor="true"
source="cfc.printData"
result="resultHandler(event)"
fault="faultHandler(event)" />
<mx:Panel title="Printing Data to PDF" id="mainPanel" height="100%" width="100%" verticalGap="10">
<mx:HBox width="100%" verticalAlign="middle">
<mx:Label text="Print Options:" fontWeight="bold" />
<mx:ComboBox id="printOptionsCb" dataProvider="{printOptions}" labelField="label" />
<mx:Spacer width="50" />
<mx:Label text="Include Header:" fontWeight="bold" />
<mx:RadioButtonGroup id="includeHeaderOptions" />
<mx:RadioButton groupName="includeHeaderOptions" value="true" label="Yes" />
<mx:RadioButton groupName="includeHeaderOptions" value="false" label="No" selected="true" />
<mx:Spacer width="50" />
<mx:Button label="Print Data" click="doPrint()" />
</mx:HBox>
<mx:DataGrid width="100%" height="100%" dataProvider="{contacts}" />
</mx:Panel>
</mx:Application>