Printing datagrid info in flash using as3

eager_to_learn
eager_to_learn used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011

Commented:
Top Expert 2011

Commented:
Top Expert 2011

Commented:
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
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

Author

Commented:
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

Top Expert 2011

Commented:
You can not use MXML code in FLA use option 3
Top Expert 2011
Commented:
Download and run FLA file from this attachment its working Great !

http://www.bgxcomponents.com/examples/printDG.zip

Author

Commented:
Exactly what I was looking for...Thanks

Author

Commented:
This is AS2...Do you have an AS3 version?
Top Expert 2011

Commented:
No, but you can modified code it will takes maybe one hour

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial