<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="absolute"
    viewSourceURL="../assets/pages/srcview/AlivePDFExample/index.html">

    <!-- add Yahoo! Flex styles (http://developer.yahoo.com/flash/articles/yahoo-flex-skin.html)--> 
    <mx:Style source="assets/styles/yflexskin.css" />
    
    <!-- external data -->
    <mx:Model id="sampleData" source="data/testData.xml" />

    <mx:Script>
        <![CDATA[
            import org.alivepdf.saving.Method;
            import org.alivepdf.saving.Download;
            import org.alivepdf.images.ResizeMode;
            import org.alivepdf.display.Display;
            import org.alivepdf.layout.Layout;
            import org.alivepdf.layout.Size;
            import org.alivepdf.layout.Unit;
            import org.alivepdf.layout.Orientation;
            import org.alivepdf.pdf.PDF;
            import mx.core.UIComponent;
            
            [Bindable]
            private var printableItems:Array =     [    {item: 'mainPanel', label: 'The Entire Application'},
                                                    {item: 'printOptions', label: 'The Print Options'},
                                                    {item: 'mainDataGrid', label: 'The DataGrid'}];
                                                    
            [Bindable]
            private var resizeOptions:Array =    [    {resize: 'fitToPage', label: 'Fit To Page'},
                                                    {resize: 'resizePage', label: 'Resize Page'},
                                                    {resize: 'none', label: 'None'}];
            
            [Bindable]
            private var serverOptions:Array =     [    {script: 'pdfCreator.cfm', label: 'ColdFusion'},
                                                    {script: 'pdfCreator.php', label: 'PHP'}];
            
            [Bindable]
            private var outputOptions:Array =     [    {method: 'Inline', label: 'To Browser'},
                                                    {method: 'Download', label: 'For Download'}];
                                                
            [Bindable]
            private var displayOptions:Array =     [    {display: 'fullPage', label: 'Full Page'},
                                                    {display: 'fullWidth', label: 'Full Width'},
                                                    {display: 'real', label: 'Real'}];
                                                
            [Bindable]
            private var orientationOptions:Array = [{orientation: 'landscape', label: 'Landscape'},
                                                    {orientation: 'portrait', label: 'Portrait'}];
            
            private function doPrint():void{

                //which UIComponent to print
                var whatToPrint:UIComponent = this[whatToPrintCb.selectedItem.item];
                
                //what orientation to use (default Landscape)
                var orientation:String = Orientation.LANDSCAPE;
                if(orientationOptionsCb.selectedItem.orientation == 'portrait'){
                    orientation = Orientation.PORTRAIT;
                }
                
                //how to output the PDF (default Inline)
                var howToOutput:String = Download.INLINE;
                if(outputOptionsCb.selectedItem.method == "Download"){
                    howToOutput = Download.ATTACHMENT;
                }
                
                //how to scale the output (default FIT_TO_PAGE)
                var howToResize:String = ResizeMode.FIT_TO_PAGE;
                if(resizeOptionsCb.selectedItem.resize == "resizePage"){
                    howToResize = ResizeMode.RESIZE_PAGE;
                }else if(resizeOptionsCb.selectedItem.resize == "none"){
                    howToResize = ResizeMode.NONE;
                }
                
                //how to display the output (default full page)
                var displayMode:String = Display.FULL_PAGE;
                if(displayOptionsCb.selectedItem.display == "fullWidth"){
                    displayMode = Display.FULL_WIDTH;
                }else if(displayOptionsCb.selectedItem.display == "real"){
                    displayMode = Display.REAL;
                }
                
                //what server script to use
                var serverScript:String = "http://kalengibbons.com/assets/pages/" + serverOptionsCb.selectedItem.script;
                
                //now print to PDF with the settings established
                var printPDF:PDF = new PDF(orientation, Unit.MM, Size.A4);
                printPDF.setDisplayMode(displayMode, Layout.SINGLE_PAGE);
                printPDF.addPage();
                printPDF.addImage(whatToPrint, 0, 0, 0, 0, 'PNG', 100, 1, howToResize);
                printPDF.save(Method.REMOTE, serverScript, howToOutput, 'alivePDFExample.pdf');

            }
            
        ]]>
    </mx:Script>
    
    <mx:Panel title="AlivePDF sample" id="mainPanel" height="100%" width="100%">
        
        <!-- print options -->
        <mx:VBox id="printOptions" width="100%" paddingBottom="10">
            <mx:HBox width="100%" verticalAlign="bottom">
                <mx:VBox verticalGap="0">
                    <mx:Label text="What to print:" fontWeight="bold" />
                    <mx:ComboBox id="whatToPrintCb" dataProvider="{printableItems}" labelField="label" />
                    <mx:Spacer height="10" />
                    <mx:Label text="Resize options:" fontWeight="bold" />
                    <mx:ComboBox id="resizeOptionsCb" dataProvider="{resizeOptions}" labelField="label" width="100%" />
                </mx:VBox>
                <mx:VBox verticalGap="0">
                    <mx:Label text="Orientation options:" fontWeight="bold" />
                    <mx:ComboBox id="orientationOptionsCb" dataProvider="{orientationOptions}" labelField="label" />
                    <mx:Spacer height="10" />
                    <mx:Label text="Display options:" fontWeight="bold" />
                    <mx:ComboBox id="displayOptionsCb" dataProvider="{displayOptions}" labelField="label" width="100%" />
                </mx:VBox>
                <mx:VBox verticalGap="0">
                    <mx:Label text="Output options:" fontWeight="bold" />
                    <mx:ComboBox id="outputOptionsCb" dataProvider="{outputOptions}" labelField="label" />
                    <mx:Spacer height="10" />
                    <mx:Label text="Server options:" fontWeight="bold" />
                    <mx:ComboBox id="serverOptionsCb" dataProvider="{serverOptions}" labelField="label" width="100%" />
                </mx:VBox>
                <mx:Spacer width="25" />
                <mx:Button label="Print" click="doPrint()" />
            </mx:HBox>    
        </mx:VBox>
        
        <mx:DataGrid id="mainDataGrid" dataProvider="{sampleData.item}" width="100%" height="100%" />
        
    </mx:Panel>
    
</mx:Application>