Este es un tip que a los que trabajamos con Flex, nos piden mucho: cómo imprimir datos desde un DataGrid, AdvancedDataGrid u OLAPDataGrid.
Flex nos provee clases específicas para imprimir los datos mostrados en esos componentes, estas clases son PrintDataGrid, PrintAdvancedDataGrid y PrintOLAPDataGrid. Con ellas podremos paginar los datos e imprimirlos de manera correcta.
Aquí les dejo un útil para que puedan imprimir agregándoles un encabezado y pie de pagina, un titulo, la cantidad de filas y la numeración de cada hoja. El componente cuenta con 2 clases, la clase principal q imprime y una clase de configuración
TabularPrint.as
Código :
package com.alozada.utils.print
{
import flash.errors.IllegalOperationError;
import mx.containers.HBox;
import mx.containers.VBox;
import mx.controls.AdvancedDataGrid;
import mx.controls.DataGrid;
import mx.controls.Label;
import mx.controls.OLAPDataGrid;
import mx.controls.Spacer;
import mx.core.Application;
import mx.core.UIComponent;
import mx.printing.FlexPrintJob;
import mx.printing.PrintAdvancedDataGrid;
import mx.printing.PrintDataGrid;
import mx.printing.PrintOLAPDataGrid;
import mx.resources.ResourceManager;
/**
*
* Author: Andrés Lozada Mosto
* Version: 1.0
* Fecha release: 25/03/2009
* Contacto: alfathenus@gmail.com
* Clase que imprime datos de forma tabulada
*
* @example
* <code>
* var options:TabularPrintOptions = new TabularPrintOptions();
* options.viewPageNumber = true;
* options.viewSumarize = true;
* TabularPrint.fromDataGrid(myDataGrid, options);
* </code>
*
* Lista de funciones
* <list>
* fromDataGrid: Imprime los datos de un Datagrid
* fromAdvancedDataGrid: Imprime los datos de un AdvancedDatagrid
* fromOLAPDatagrid: Imprime los datos de un OLAPDatagrid
* print: Imprime los datos de un Datagrid, AdvancedDataGrid u OLAPDataGrid
* </list>
*
*/
public class TabularPrint
{
public function TabularPrint()
{
throw new IllegalOperationError("TabularPrint class is static. Don't instace");
}
/**
*
* Imprime los datos de un Datagrid
*
* @param grid Refernecia al DataGrid
* @param options Opciones de impresion
*
* @return True si se pudo imprimir, False si no se pudo imprimir o se cancelo la impresion
*
*/
static public function fromDataGrid(grid:DataGrid, options:TabularPrintOptions):Boolean
{
return doPrint(grid, PrintDataGrid, options);
}
/**
*
* Imprime los datos de un AdvancedDatagrid
*
* @param grid Refernecia al AdvancedDataGrid
* @param options Opciones de impresion
*
* @return True si se pudo imprimir, False si no se pudo imprimir o se cancelo la impresion
*
*/
static public function fromAdvancedDataGrid(grid:AdvancedDataGrid, options:TabularPrintOptions):Boolean
{
return doPrint(grid, PrintAdvancedDataGrid, options);
}
/**
*
* Imprime los datos de un OLAPDatagrid
*
* @param grid Refernecia al OLApDataGrid
* @param options Opciones de impresion
*
* @return True si se pudo imprimir, False si no se pudo imprimir o se cancelo la impresion
*
*/
static public function fromOLAPDatagrid(grid:OLAPDataGrid, options:TabularPrintOptions):Boolean
{
return doPrint(grid, PrintOLAPDataGrid, options);
}
/**
*
* Imprime los datos de un Datagrid, AdvancedDataGrid u OLAPDataGrid
*
* @param grid Refernecia al componente
* @param options Opciones de impresion
*
* @return True si se pudo imprimir, False si no se pudo imprimir o se cancelo la impresion
*
*/
static public function print(grid:*, options:TabularPrintOptions = null):Boolean
{
if ( options == null )
options = new TabularPrintOptions();
if ( grid is DataGrid )
{
return fromDataGrid(grid, options);
}
else if ( grid is AdvancedDataGrid )
{
return fromAdvancedDataGrid(grid, options);
}
else if ( grid is OLAPDataGrid )
{
return fromOLAPDatagrid(grid, options);
}
return false;
}
//-----------------------------------------
// Private methods
//-----------------------------------------
static protected function createPage(grid:*, options:TabularPrintOptions):VBox
{
grid.id = "grid";
grid.name = "grid";
(grid as UIComponent).percentWidth = 100;
(grid as UIComponent).percentHeight = 100;
var vbox:VBox = new VBox();
vbox.percentWidth = 100;
//agrego header
if (options.headerViewInAllPages != null )
{
options.headerViewInAllPages.name = "header";
options.headerViewInAllPages.percentWidth = 100;
vbox.addChild(options.headerViewInAllPages);
}
//agrego header en primer hoja
if (options.headerViewInFirstPage != null )
{
options.headerViewInFirstPage.name = "headerFirst";
options.headerViewInFirstPage.percentWidth = 100;
vbox.addChild(options.headerViewInFirstPage);
}
//agrego data
vbox.addChild(grid);
vbox.setStyle("backgroundColor", 0xFFFFFF);
// agrego sumarizador
if (options.viewSumarize )
{
var hbox:HBox = new HBox();
hbox.percentWidth = 100;
hbox.name = "hbox";
hbox.id = "sumarize";
hbox.name = "sumarize";
var spacer:Spacer = new Spacer();
spacer.percentWidth = 100;
hbox.addChild(spacer);
var l:Label = new Label();
if (options.sumarizelabelKeyInternationalize != "" && options.sumarizeLabelDefault != "")
l.text = ResourceManager.getInstance().getString(options.propertiesFile, options.sumarizelabelKeyInternationalize)
if (l.text == "")
l.text = options.sumarizeLabelDefault;
l.text = l.text + grid.dataProvider.length;
hbox.addChild(l);
hbox.visible = false;
hbox.includeInLayout = false;
vbox.addChild(hbox);
}
//agrego footer
if (options.footerViewInAllPages != null )
{
options.footerViewInAllPages.name = "header";
options.footerViewInAllPages.percentWidth = 100;
vbox.addChild(options.footerViewInAllPages);
}
//agrego numero de pagina
if ( options.viewPageNumber )
{
var hbox2:HBox = new HBox();
hbox2.percentWidth = 100;
hbox2.name = "hbox";
var spacer2:Spacer = new Spacer();
spacer2.percentWidth = 100;
hbox2.addChild(spacer2);
var label:Label = new Label();
if (options.pageNumberLabelKeyInternationalize != "" && options.propertiesFile != "")
label.text = ResourceManager.getInstance().getString(options.propertiesFile, options.pageNumberLabelKeyInternationalize)
if (label.text == "")
label.text = options.pageNumberLabelDefault;
label.id = "page";
label.name = "page";
hbox2.addChild(label);
vbox.addChild(hbox2);
}
return vbox;
}
static protected function doPrint(grid:*, clase:Class, options:TabularPrintOptions):Boolean
{
if ( grid == null || grid.dataProvider == null ||
clase == null ||
grid.dataProvider.hasOwnProperty("length") == false || grid.dataProvider.length == 0)
{
return false;
}
if ( options == null )
options = new TabularPrintOptions();
var printView:* = new clase();
printView.dataProvider = grid.dataProvider;
var vbox:VBox = createPage(printView, options);
return doPrintAction(vbox, options);
}
static protected function doPrintAction(view:*, options:TabularPrintOptions):Boolean
{
var printView:* = (view as UIComponent).getChildByName("grid");
var page:Label;
var pageText:String;
var pageCount:int = 0;
if ( options.viewPageNumber )
{
page = ((view as UIComponent).getChildByName("hbox") as HBox).getChildByName("page") as Label;
pageText = page.text;
}
if ( printView == null )
return false;;
var printJob:FlexPrintJob = new FlexPrintJob();
if (printJob.start()) {
Application.application.addChild(view);
view.width=printJob.pageWidth;
view.height=printJob.pageHeight;
while(true)
{
pageCount++;
if ( page )
{
page.text = pageText + pageCount.toString();
}
if(printView.validNextPage)
{
printJob.addObject(view);
printView.nextPage();
}
else
{
if (options.viewSumarize )
{
var sum:HBox = ((view as UIComponent).getChildByName("sumarize") as HBox);
if (sum)
{
sum.visible = true;
sum.includeInLayout = true;
sum.validateNow();
}
}
printJob.addObject(view);
break;
}
if ( pageCount > 0 )
{
var headerFirst:* = (view as UIComponent).getChildByName("headerFirst");
if ( headerFirst != null )
{
headerFirst.visible = false;
headerFirst.includeInLayout = false;
}
}
}
Application.application.removeChild(view);
printJob.send();
return true;
}
return false;
}
}
}Código :
package com.alozada.utils.print
{
import mx.core.UIComponent;
public class TabularPrintOptions
{
/**
* Componente header q se agrega en la impresion solo en la primer pagina
*/
public var headerViewInFirstPage:UIComponent;
/**
* Componente header q se agrega en la impresion en todas las paginas
*/
public var footerViewInAllPages:UIComponent;
/**
* Componente footer q se agrega en la impresion en todas las paginas
*/
public var headerViewInAllPages:UIComponent;
/**
* Nombre del archivo properties de donde se sacan las claves de internacionalizacion
*/
public var propertiesFile:String;
/**
* Indica si se muestra o no la cantidad de filas impresas
*/
public var viewSumarize:Boolean = true;
/**
* Texto por default del indicador de cantidad de filas
*/
public var sumarizeLabelDefault:String = "Total rows: ";
/**
* Clave de internacionalizacion del indicador de cantidad de filas
*/
public var sumarizelabelKeyInternationalize:String;
/**
* Indica si se muestra o no el numero de paginas
*/
public var viewPageNumber:Boolean = true;
/**
* Texto por default de la cantidad de paginas
*/
public var pageNumberLabelDefault:String = "Page: ";
/**
* Clave de internacionalizacion de la cantidad de paginas
*/
public var pageNumberLabelKeyInternationalize:String;
public function TabularPrintOptions()
{
}
}
}Código :
TabularPrint.fromDataGrid(myDataGrid)
Código :
private function imprimir():void
{
TabularPrint.fromDataGrid(myDataGrid);
}Código :
package com.bla1.bla_mas_mas
Código :
com/bla1/bla_mas_mas
Código :
options:viewPageNumber:true
Código :
var optins:TabularPrintOptions = new TabularPrintOptions(); options.viewPageNumber = true; TabularPrint.fromDataGrid(dataGrid, options);