Link to home
Start Free TrialLog in
Avatar of eager_to_learn
eager_to_learn

asked on

Printing datagrid info in flash using as3

Team,

Problem:
I want to print my datagrid information using the client-side printer (user). I have a printer button but need actionscript code to implement the printing. What is the best way to have this work?

Details:
I want to use a flash movie clip to print...
I do not want to convert to html tables...
The information is being called using php and displayed in a datagrid...

Thanks Team,
ETL
Avatar of dgofman
dgofman
Flag of United States of America image

Avatar of eager_to_learn
eager_to_learn

ASKER

dgofman,

Fast response...Great Job.
I am attempting to work with the first option. The second option will require every client to have acrobat reader installed. I will post my results shortly.

Thanks
Here is the code that I pulled from the adobe website. I inserted this in a .fla just to test.

I get an error with the mxml headers...

<?xml version="1.0"?>
<!-- printing\MultiPagePrint.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    initialize="initData();">

...and nothing happens when I click the PrintIcon button.

What am I doing wrong?

PrintIcon.addEventListener(MouseEvent.MOUSE_DOWN, printNow);
function printNow(event:MouseEvent):void
{
	trace("button worked");


	<mx:Script>
    <![CDATA[
        import mx.printing.*;
        import mx.collections.ArrayCollection;
        // Import the MXML custom print view control.
        import myComponents.FormPrintView;

        // Declare variables and initialize simple variables.
        // The dgProvider ArrayCollection is the DataGrid data provider.
        // It must be bindable because you change its contents dynamically.
        [Bindable]
        public var dgProvider:ArrayCollection;
        public var footerHeight:Number = 20;
        public var prodIndex:Number;
        public var prodTotal:Number = 0;
        
        

        // The function to print the output.
        function doPrint():void {
            // Create a FlexPrintJob instance.
            var printJob:FlexPrintJob = new FlexPrintJob();
            
            // Start the print job.
            if (printJob.start()) {
                // Create a FormPrintView control 
                // as a child of the application.
                var thePrintView:FormPrintView = new FormPrintView();
                addChild(thePrintView);
				trace("print view added");
                
                // Set the print view properties.
                thePrintView.width=printJob.pageWidth;
                thePrintView.height=printJob.pageHeight;
                thePrintView.prodTotal = prodTotal;
                
                // Set the data provider of the FormPrintView 
                // component's DataGrid to be the data provider of 
                // the displayed DataGrid.
                thePrintView.ComparisonGrid.dataProvider = 
                    ComparisonGrid.dataProvider;
                
                // Create a single-page image.
                thePrintView.showPage("single");
                
                // If the print image's DataGrid can hold all the  
                // data provider's rows, add the page to the print job. 
                if(!thePrintView.ComparisonGrid.validNextPage)
                {
                    printJob.addObject(thePrintView);
                }
                // Otherwise, the job requires multiple pages.
                else
                {
                    // Create the first page and add it to the print job.
                    thePrintView.showPage("first");
                    printJob.addObject(thePrintView);
                    thePrintView.pageNumber++;
                    
                    // Loop through the following code 
                    // until all pages are queued.
                    while(true)
                    {
                        // Move the next page of data to the top of 
                        // the PrintDataGrid.
                        thePrintView.ComparisonGrid.nextPage();

                        // Try creating a last page.
                        thePrintView.showPage("last");  

                        // If the page holds the remaining data, or if 
                        // the last page was completely filled by the last  
                        // grid data, queue it for printing.
                        // Test if there is data for another 
                        // PrintDataGrid page.
                        if(!thePrintView.ComparisonGrid.validNextPage) 
                        {
                            // This is the last page; 
                            // queue it and exit the print loop.
                            printJob.addObject(thePrintView);
                            break;
                        }
                        else
                        // This is not the last page. Queue a middle page. 
                        {
                            thePrintView.showPage("middle");
                            printJob.addObject(thePrintView);
                            thePrintView.pageNumber++;
                        }
                    }
                }
                // All pages are queued; remove the FormPrintView 
                // control to free memory.
                removeChild(thePrintView);
            }
            // Send the job to the printer.
            printJob.send();
        }
    ]]>
    </mx:Script>

	
	
}

Open in new window

You can not use MXML code in FLA use option 3
ASKER CERTIFIED SOLUTION
Avatar of dgofman
dgofman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Exactly what I was looking for...Thanks
This is AS2...Do you have an AS3 version?
No, but you can modified code it will takes maybe one hour