Customizing gridcolumns/grids using alivePDF

4060
2
08-27-2010 06:51 AM
TracySchloss
Frequent Contributor
I have successfully generated a PDF containing my datagrid contents using alivePDF.  However, I have a few columns with large numbers (total populations of a county).  In the FLEX grid, I was able to use a labelFunction to call a numberFormatter.  This let me add the commas I wanted as a thousands separator.  (The commas that show in the FLEX grid are not showing in the alivePDF.)

I don't see that I have such a setting for the grids within alivePDF.  This leaves me a report with no commas to format my large numbers.  This is not really 'wrong', but it does make it harder to read the numbers.

Has anyone dug into the alivePDF grids enough to figure out how to properly render these numbers? I'm using alivePDF 0.1.5. I've attached the relevant section from my code.
Tags (2)
0 Kudos
2 Replies
MarkHoyland
Occasional Contributor II
Here's something that might help you.
It updates the values in the array sent to the pdf grid.
For testing purposes there is a field that get a $ sign added to it in a labelfunction. When exported to AlivePDF the dollar sign shows up.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
  creationComplete="initData()">
 
 <mx:Script>
  <![CDATA[
   import mx.controls.dataGridClasses.DataGridColumn;
   import org.alivepdf.fonts.CoreFont;
   import mx.collections.ArrayCollection;
   import org.alivepdf.colors.IColor;
   import org.alivepdf.colors.RGBColor;
   import org.alivepdf.data.Grid;
   import org.alivepdf.data.GridColumn;
   import org.alivepdf.saving.Method;
   import org.alivepdf.display.Display;
   import org.alivepdf.layout.Size;
   import org.alivepdf.layout.Unit;
   import org.alivepdf.layout.Orientation;
   import org.alivepdf.pdf.PDF;
   
   private var dgArray:Array = [
           {Artist:'Pavement', Album:'Slanted and Enchanted', Price:11.99},
           {Artist:'Pavement', Album:'Brighten the Corners', Price:11.99},
           {Artist:'test2', Album:'junk1', Price:11.99},
           {Artist:'test2', Album:'junk2', Price:22.99},
           {Artist:'test3', Album:'junk3', Price:11.99},
           {Artist:'test3', Album:'junk4', Price:11.99},
           {Artist:'test4', Album:'junk1 and Enchanted', Price:35.99},
           {Artist:'test4', Album:'Slanted and junk1', Price:6.99},
           {Artist:'test5', Album:'Slanted junk1', Price:89.99},
           {Artist:'test5', Album:'junk1 Enchanted', Price:33.12}];
           
   [Bindable] public var dgArrayColl:ArrayCollection;
    
      private function initData():void 
      {
       dgArrayColl=new ArrayCollection(dgArray);
      }

   private function myLabelFunc(data:Object, column:DataGridColumn):String 
   {
             return "$" + data.Price; 
         }
   
   private function sendToPDF():void
   {
    var PDFcolumns:Array = getPDFColumns(dg);
 
    var resultsArray:Array = dg.dataProvider.toArray();
    formatPDFGridData(resultsArray, dg);
    
    var pdf:PDF  = new PDF(Orientation.PORTRAIT,Unit.MM,Size.A4); 
    pdf.addPage();
    pdf.setAutoPageBreak(false,1);       
    pdf.setDisplayMode ( Display.FULL_PAGE );       
      
    pdf.setFont(new CoreFont(),10);
      pdf.textStyle(new RGBColor(0x000000));
      
      var rgbWhite:IColor = new RGBColor(0xFFFFFF);
      var rgbBlack:IColor = new RGBColor(0x000000);
      var rgbGray:IColor = new RGBColor(0xCCCCCC);
      
    var grid:Grid = new Grid(resultsArray,0,dg.height,rgbGray,rgbWhite,false,rgbBlack);
    grid.columns = PDFcolumns;
    pdf.addGrid(grid,25,10);
    
    pdf.save(Method.REMOTE, "http://localhost/createPDF/create.aspx", Download.ATTACHMENT, "report.pdf"); 
      
   } // end function sendToPrinter
   
   private function getPDFColumns(datagrid:DataGrid):Array
   {
    // This will create an array suitable for use as columns in AlivePDF
    // The columns are based on a datagrid's columns.
    // If the datagrid column is visible it will be included.
    
    //The width of each column is calculated assuming mm are the units used in the pdf.
    //1 pt = 0.35 mm   
    //1 mm = 2.83 pt 
    
    var PDFcolumns:Array = new Array;   
    var dataGridColumn:DataGridColumn;
    
    //loop through each datagridcolumn in the datagrid
    for( var i:Number = 0; i < datagrid.columns.length; i++ ) 
       {
        dataGridColumn = datagrid.columns;
        if (dataGridColumn.visible)
        {
         // add to the pdfcolumn array
         var gridColumn:GridColumn = new GridColumn(dataGridColumn.headerText,
                   dataGridColumn.dataField, 
                   dataGridColumn.width * 0.35);
         PDFcolumns.push(gridColumn);
        }
       }
       return PDFcolumns;
   }
   
   private function formatPDFGridData(arr:Array, datagrid:DataGrid):void
   {
    var dataGridColumn:DataGridColumn;
    // loop through the array 
    for (var i:int = 0; i<arr.length; i++)
    {
     //go through each datagridColumn in the datagrid
     for( var iCols:Number = 0; iCols < datagrid.columns.length; iCols++ ) 
        {
         dataGridColumn = datagrid.columns[iCols];
         // If there is a labelfunction on the column, 
         // update the data in the array using its return value
         if (dataGridColumn.labelFunction != null)
         {
          arr[dataGridColumn.dataField] = dataGridColumn.labelFunction(arr,dataGridColumn);
         }
        }
    }  
   }
  ]]>
 </mx:Script>
 
 <mx:DataGrid id="dg" dataProvider="{dgArrayColl}" width="450">
  <mx:columns>
   <mx:DataGridColumn dataField="Artist" width="100"/>
   <mx:DataGridColumn dataField="Album" />
   <mx:DataGridColumn dataField="Price" width="100" visible="true" labelFunction="myLabelFunc"/>
  </mx:columns>
 </mx:DataGrid>
 <mx:Button label="PDF" click="sendToPDF()"/>
</mx:Application>

0 Kudos
TracySchloss
Frequent Contributor
Perfect!  It did just what I needed.
0 Kudos