2 Replies Latest reply on May 11, 2011 10:24 AM by InfoCon_Brian

    Printing a Flex Component

    InfoCon_Brian Level 1

      I am trying to print a component with a datagrid(print version) and panels.

      However, all I get is the uppper left corner with some of the controls filled.

      How do I get multiple pages?

       

      I have tried the following code segments:

      package
      {
          import flash.printing.PrintJob;
          import flash.display.Sprite;

          public class BasicPrintExample extends Sprite
          {
              var myPrintJob:PrintJob = new PrintJob();
              var mySprite:Sprite = new Sprite();

              public function BasicPrintExample()
              {
                  myPrintJob.start();
                  myPrintJob.addPage(mySprite);
                  myPrintJob.send();
              }
          }
      }
      and the following data grid centric:

      // The function to print the output.
              public function doPrint():void {

                  var printJob:FlexPrintJob = new FlexPrintJob();
                  if (printJob.start()) {
                      // Create a FormPrintView control as a child of the current view.
                      var thePrintView:FormPrintView = new FormPrintView();
                     Application.application.addChild(thePrintView);

                      //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 data grid
                      // to be the data provider of the displayed data grid.
                      thePrintView.myDataGrid.dataProvider = myDataGrid.dataProvider;
                      // Create a single-page image.
                      thePrintView.showPage("single");
                      // If the print image's data grid can hold all the provider's rows,
                      // add the page to the print job.
                      if(!thePrintView.myDataGrid.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 print grid.
                              thePrintView.myDataGrid.nextPage();
                              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.myDataGrid.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.
                      Application.application.removeChild(thePrintView);
                  }
                  // Send the job to the printer.
                  printJob.send();
              }

       

       

        • 1. Re: Printing a Flex Component
          Shravan P

          check out this below code:

           

          <?xml version="1.0" encoding="utf-8"?>

          <mx:Application

          xmlns:mx=http://www.adobe.com/2006/mxml layout="absolute" initialize="init()">

           

           

          <mx:Script>

          <![CDATA[

           

          import mx.printing.PrintDataGrid;import mx.printing.FlexPrintJob;import mx.collections.ArrayCollection;

           

          [Bindable]

          public var dataSource:ArrayCollection = new ArrayCollection();private var totalRecords:Number = 50;

           

           

          private function init():void {

           

          for (var i:int = 1; i<=totalRecords; i++) {

           

          var dataObject:Object = new Object();

          dataObject.Name =

          "Name #" + i;"Phone #" + i;"Address #" + i;

          dataObject.Phone =

          dataObject.Address =

          dataSource.addItem(dataObject);

          }

          }

           

          private function doPrint():void {

           

          var printJob:FlexPrintJob = new FlexPrintJob();

           

          if (printJob.start()) {

           

          var myPrintData:PrintDataGrid = new PrintDataGrid();

          Application.application.addChild(myPrintData);

          myPrintData.dataProvider = myData.dataProvider;

          myPrintData.width = printJob.pageWidth;

          myPrintData.height = printJob.pageHeight;

           

          printJob.addObject(myPrintData);

           

          while (myPrintData.validNextPage) {

           

          myPrintData.nextPage();

          printJob.addObject(myPrintData);

           

          }

          Application.application.removeChild(myPrintData);

           

          printJob.send();

          }

          }

           

          ]]>

           

          </mx:Script>

           

          <mx:Panel title="PrintDataGrid" width="500" height="500" horizontalCenter="0" verticalCenter="0"center" verticalAlign="middle">

          horizontalAlign="

           

          <mx:DataGrid id="myData" dataProvider="{dataSource}" width="400" height="400" />

           

          <mx:Button label="Print" click="doPrint()"/>

           

          </mx:Panel>

           

          </mx:Application>

          • 2. Re: Printing a Flex Component
            InfoCon_Brian Level 1

            Maybe, I wasn't clear.

            I am trying to print a component that contains a data grid and a panel with

            client information, etc.  printing the component as it appears on

            the screen.

             

            Similar to what is done in coldfusion report generator.

            Which I don't want to use, because I would have to trigger a

            download, etc. (a pain for the user.)

             

            So the printed output looks like an invoice.