1

我正在尝试使用 AlivePdf 在我的 flex 应用程序上创建一个 pdf 表:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                xmlns:mh="mh.components.*"
                layout="absolute" width="500" height="500">



<mx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;

        import org.alivepdf.colors.RGBColor;
        import org.alivepdf.display.Display;
        import org.alivepdf.drawing.Caps;
        import org.alivepdf.fonts.FontFamily;
        import org.alivepdf.fonts.Style;
        import org.alivepdf.grid.Grid;
        import org.alivepdf.images.ImageFormat;
        import org.alivepdf.layout.Orientation;
        import org.alivepdf.layout.Resize;
        import org.alivepdf.layout.Size;
        import org.alivepdf.layout.Unit;
        import org.alivepdf.pages.Page;
        import org.alivepdf.pdf.PDF;
        import org.alivepdf.saving.Method;
        import org.alivepdf.visibility.Visibility;

        import org.alivepdf.grid.*;





        private var myPDF:PDF;


        //print chart in pdf format
        protected function savePDF(e:MouseEvent):void
        {
            var myPDF:PDF = new PDF ( Orientation.PORTRAIT, Unit.MM);
            myPDF.setDisplayMode(Display.FULL_PAGE);
            myPDF.addPage();



            myPDF.setXY( 10, 70);
            myPDF.textStyle ( new RGBColor ( 0x000000 ) );

            var dp:ArrayCollection = new ArrayCollection();
            dp.addItem( { firstName : "Bob Geldorf akjaskaj skajs as kajs kaj k dklfj sdkfjl sdkjf ksdj fkjs dkfj ksdj ", lastName : "Groove", city : "Paris" } );
            dp.addItem( { firstName : "Bob", lastName : "Wise", city : "Paris" } );
            dp.addItem( { firstName : "Bob", lastName : "Wise", city : "Paris" } );
            dp.addItem( { firstName : "Bob", lastName : "Wise", city : "Paris" } );


            var grid:Grid = new Grid ( dp.toArray(), 200, 100, new RGBColor (0x00DEFF));


            myPDF.addGrid( grid, 0, 0, true );
            myPDF.save( Method.REMOTE, "coldfusion/pdf.cfm", "inline", "test.pdf" );;
        }



    ]]>
</mx:Script>    

<mx:VBox width="100%" height="100%">


    <mx:HBox width="100%" backgroundColor="#FFFFFF">
        <mx:Spacer width="100%"/>

        <mx:Button horizontalCenter="0" label="Save to PDF" height="22"  click="savePDF(event)"  id="savePDFBtn" toolTip="SAVE TO PDF"/>

    </mx:HBox>
</mx:VBox>

奇怪的是脚本创建了一个带有双标题的表,我不知道为什么。您可以在此链接查看生成的 pdf:https ://docs.google.com/viewer?url=prestitiinpdap.biz/pdf/myPDF.pdf

4

2 回答 2

2

在使用网格创建 PDF 时,我遇到了相同的标题重复问题,如此处所述。您的问题已经过去了几年,但是自从我现在遇到它以来,这是我的解决方案...

为了确保使用最新的 AlivePDF 版本,我从他们的 SVN 下载了最新的源代码:http: //alivepdf.googlecode.com/svn/trunk/AlivePDFBeta/src

我相信这是作为 AlivePDF 0.1.5 RC 分发的,但是分发的 SWC 在某些方法签名上似乎存在差异。

在扫描 AlivePDF 源代码以查找与添加 Grid 标头相关的行为时,我注意到了类org.alivepdf.pdf.PDF,公共方法addGrid。该方法确实添加了 Grid 标题2 次,可能是错误的。这已通过注释第41784180行来解决,如以下源代码所示。

带有注释部分的完整方法包括:

public function addGrid ( grid:Grid, x:Number=0, y:Number=0, repeatHeader:Boolean=true ):void
        {   
            if ( textColor == null ) 
                throw new Error("Please call the setFont and textStyle method before adding a Grid.");

            currentGrid = grid;
            currentGrid.x = x;
            currentGrid.y = y;
            var i:int = 0;
            var j:int = 0;

            currentGrid.generateColumns(false);
            columns = currentGrid.columns;

            var row:Array;
            columnNames = new Array();
            var lngColumns:int = columns.length;    
            var item:*;

            for (i = 0; i< lngColumns; i++)
                columnNames.push ( new GridCell(columns[i].headerText, currentGrid.headerColor ) );

            var rect:Rectangle = getRect ( columnNames, currentGrid.headerHeight );
            if ( checkPageBreak(rect.height) )
                addPage();

            // Commented to avoid the duplicate header issue:
            //setXY (x +currentGrid.x, y+getY() );
            //addRow( columnNames,'', rect);
            //endFill();

            setXY ( x+getX(), y+getY() );
            addRow( columnNames, GridRowType.HEADER, rect );

            if (grid.cells == null)
                grid.generateCells();

            var buffer:Array = grid.cells;
            var lngRows:int = buffer.length;

            for (i = 0; i< lngRows; i++)
            {

                item = buffer[i];
                row = new Array();
                for (j = 0; j< lngColumns; j++)
                {
                    row.push (item[columns[j].dataField] != null ? item[columns[j].dataField] : "");
                    nb = Math.min(nb,nbLines(columns[j].width,row[j]));
                }

                row = buffer[i];


                rect = getRect ( row, currentGrid.rowHeight );
                setX ( x + getX());

                if ( checkPageBreak(rect.height) )
                {
                    addPage();
                    setXY ( x+getX(),nextPageY );
                    //setXY ( x+getX(),y+getY() ); hacked to allow user to set the next Page Y of Grid
                    if ( repeatHeader ) 
                    {
                        addRow (columnNames, GridRowType.HEADER, getRect(columnNames, currentGrid.headerHeight) ); // header
                        setX ( x + getX() );
                    }
                }

                if ( grid.useAlternativeRowColor && Boolean(isEven = i&1) )
                    addRow( row, GridRowType.ALTERNATIVE, rect );
                else addRow( row, GridRowType.NORMAL, rect );
            }
        } 
于 2013-04-25T15:04:31.560 回答
2

您使用哪个版本的库?我尝试使用当前版本 0.1.5RC 编译您的代码,但编译器找不到某些类。

更正类路径后,我发现“new Grid(...)”构造函数需要 7 个参数(在您的情况下只有 4 个)。

另一个问题是“Grid(...)”中的“宽度”。我不知道,开发人员想用它表达什么。我只有 60 才能获得正常视图。

毕竟我有以下PDF:

AlivePDF 网格

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" width="500" height="500">
<fx:Script>
    <![CDATA[
    import mx.collections.ArrayCollection;
    import org.alivepdf.colors.RGBColor;
    import org.alivepdf.data.Grid;
    import org.alivepdf.display.Display;
    import org.alivepdf.drawing.Caps;
    import org.alivepdf.fonts.FontFamily;
    import org.alivepdf.fonts.Style;
    import org.alivepdf.images.ImageFormat;
    import org.alivepdf.layout.Orientation;
    import org.alivepdf.layout.Resize;
    import org.alivepdf.layout.Size;
    import org.alivepdf.layout.Unit;
    import org.alivepdf.pages.Page;
    import org.alivepdf.pdf.PDF;
    import org.alivepdf.saving.Method;
    import org.alivepdf.visibility.Visibility;
    import org.alivepdf.layout.*;
    private var myPDF:PDF;

    protected function savePDF(e:MouseEvent):void
    {
        var myPDF:PDF = new PDF( Orientation.PORTRAIT, Unit.MM); 
        myPDF.setDisplayMode( Display.FULL_PAGE, Layout.SINGLE_PAGE );

        var newPage:Page = new Page ( Orientation.PORTRAIT, Unit.MM);
        myPDF.addPage(newPage);

        myPDF.textStyle ( new RGBColor ( 0x000000 ) );

        var dp:ArrayCollection = new ArrayCollection();
        dp.addItem( { firstName : "Bob Geldorf akjaskaj skajs as kajs kaj k dklfj sdkfjl sdkjf ksdj fkjs dkfj ksdj ", lastName : "Groove", city : "Paris" } );
        dp.addItem( { firstName : "Bob", lastName : "Wise", city : "Paris" } );
        dp.addItem( { firstName : "Bob", lastName : "Wise", city : "Paris" } );
        dp.addItem( { firstName : "Bob", lastName : "Wise", city : "Paris" } );

        //var grid:Grid = new Grid ( dp.toArray(), 200, 100, new RGBColor (0x00DEFF));

        var grid:Grid = new Grid (dp.toArray(), 60, 100, new RGBColor(0x00DEFF), new RGBColor (0xFFFFFF), false, new RGBColor (0x000000));
        myPDF.addGrid( grid, 0, 0, true );

        //myPDF.save( Method.REMOTE, "coldfusion/pdf.cfm", "inline", "test.pdf" );

        var f:FileReference = new FileReference();
        var b:ByteArray = myPDF.save(Method.LOCAL);
        f.save(b, "test.pdf");
    }

    ]]>
</fx:Script>    

<mx:VBox width="100%" height="100%">
    <mx:HBox width="100%" backgroundColor="#FFFFFF">
        <mx:Spacer width="100%"/>
        <mx:Button horizontalCenter="0" label="Save to PDF" height="22"  click="savePDF(event)"  id="savePDFBtn" toolTip="SAVE TO PDF"/>
    </mx:HBox>
</mx:VBox>
</s:Application>

试试看,也许它会帮助你。

于 2013-02-10T20:31:45.543 回答