因为打印往往需要动态设置数值,并且存入后台数据库,最好的方式是使用官方的打印api,最最好的方式是使用html和官方打印flexapi结合,as+js+html+css+flex可以完美预览,数据修改和打印页面设置。富客户端嵌入了webkit内核,因此as和js通信完全没问题。需要分页可以仔细研究下flexpaper
下面的组件多多少少有些局限性,或者没法在air中预览,或者不能动态修改被打印数据,或者需要在服务器端进行格式转换,或者要装插件,问题多多:
1 基于JasperReports的打印http://Flex.org/software/component/xviewer
2 FlexReport: http://Flexreport.riaforge.org
3 AlivePDF: http://code.google.com/p/alivepdf
4 flexpaper http://flexpaper.devaldi.com/docs.html
最简单的例子如下,alivepdf仅是用来保存pdf格式的,如果需要其他格式excel,word,似乎只能在服务器端做了:
<?xml version="1.0" encoding="utf-8"?> <s:WindowedApplication 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="1200" height="700" creationComplete="init()"> <fx:Declarations> <!-- Place non-visual elements (e.g., services, value objects) here --> </fx:Declarations> <fx:Script> <![CDATA[ import mx.collections.ArrayCollection; import mx.printing.FlexPrintJob; import flash.sampler.NewObjectSample; import flash.utils.ByteArray; import mx.collections.ArrayCollection; import mx.controls.HTML; import mx.events.FlexEvent; import org.alivepdf.annotations.Annotation; import org.alivepdf.annotations.AnnotationType; import org.alivepdf.annotations.TextAnnotation; import org.alivepdf.colors.CMYKColor; import org.alivepdf.colors.RGBColor; import org.alivepdf.colors.SpotColor; import org.alivepdf.data.Grid; import org.alivepdf.data.GridColumn; import org.alivepdf.events.CharacterEvent; import org.alivepdf.fonts.CodePage; import org.alivepdf.fonts.CoreFont; import org.alivepdf.fonts.EmbeddedFont; import org.alivepdf.fonts.FontFamily; import org.alivepdf.fonts.IFont; import org.alivepdf.images.ColorSpace; import org.alivepdf.layout.Align; import org.alivepdf.layout.Mode; import org.alivepdf.layout.Orientation; import org.alivepdf.layout.Position; import org.alivepdf.layout.Resize; import org.alivepdf.layout.Size; import org.alivepdf.layout.Unit; import org.alivepdf.links.HTTPLink; import org.alivepdf.links.Highlight; import org.alivepdf.links.ILink; import org.alivepdf.links.InternalLink; import org.alivepdf.pages.Page; import org.alivepdf.pdf.PDF; import org.alivepdf.saving.Method; import org.alivepdf.display.Display; import org.alivepdf.layout.Layout; [Bindable] public var dataSource:ArrayCollection = new ArrayCollection(); private var totalRecords:Number = 15; private function init():void { pageMenu.selectedIndex=1; var page:Object=pageMenu.selectedItem as Object; containerBox.width=page.width; containerBox.height=page.height; } private function doPrint():void { var printJob:FlexPrintJob = new FlexPrintJob(); printJob.printAsBitmap=false; //提高文字清晰度 if (printJob.start()) { printJob.addObject(containerBox); printJob.send(); } } public var pages:ArrayCollection = new ArrayCollection( [ {index:3,label:"A3",width:Size.A3.dimensions[0],height:Size.A3.dimensions[1]}, {index:4,label:"A4",width:Size.A4.dimensions[0],height:Size.A4.dimensions[1]}, {index:5,label:"A5",width:Size.A5.dimensions[0],height:Size.A5.dimensions[1]} ]); private function changePageHandler(event:Event):void { var page:Object=DropDownList(event.target).selectedItem as Object; pageDescription.text=page.width+"*"+page.height; containerBox.width=page.width; containerBox.height=page.height; } protected function directClickHandler(event:MouseEvent):void { if(direct.label=="方向:横"){ direct.label="方向:竖"; var page:Object=pageMenu.selectedItem as Object; containerBox.width=page.height; containerBox.height=page.width; }else{ direct.label="方向:横"; var page:Object=pageMenu.selectedItem as Object; pageDescription.text=page.data; containerBox.width=page.width; containerBox.height=page.height; } } [Embed( source="../assets/Capture it.ttf", mimeType="application/octet-stream" )] private var fontStream:Class; private var file:File; private var b:ByteArray = new ByteArray(); private function createPDF():void { var t:ByteArray = new fontStream(); var ty:Array=Size.A4.dimensions; trace(Size.A4.dimensions.toString()); trace(Size.A4.inchesSize.toString()); trace(Size.A4.mmSize.toString()); var uf:IFont = new CoreFont ( FontFamily.ARIAL ); var p:PDF = new PDF( Orientation.PORTRAIT, Unit.MM, Size.A4 ); p.setDisplayMode( Display.FULL_PAGE, Layout.SINGLE_PAGE ); p.addPage(); p.textStyle( new RGBColor(0x990000), 1 ); p.setFont(uf, 9); p.addImage( this.printData ); var f:FileStream = new FileStream(); file = File.desktopDirectory.resolvePath("exported.pdf"); f.open( file, FileMode.WRITE); var bytes:ByteArray = p.save( Method.LOCAL ); f.writeBytes(bytes); f.close(); } ]]> </fx:Script> <mx:VBox id="topMenu" width="100%" height="100%" horizontalScrollPolicy="auto" verticalScrollPolicy="auto"> <mx:ApplicationControlBar id="dockedBar" dock="true" paddingTop="0" paddingBottom="0"> <mx:HBox paddingBottom="5" verticalAlign="middle" horizontalAlign="left" paddingTop="5"> <mx:Label text="纸型" /> <s:DropDownList id="pageMenu" labelField="label" dataProvider="{pages}" textAlign="center" width="114" color="0x000000" change="changePageHandler(event);" selectedIndex="0" height="39"/> <mx:Label textAlign="center" id="pageDescription" text="" /> <mx:LinkButton id="direct" click="directClickHandler(event)" label="方向:横" /> <mx:Label text="左边距" /> <mx:HSlider id="lBar" width="60" minimum="0" maximum="100" value="1" snapInterval="1" liveDragging="true" /> <mx:Label text="右边距" /> <mx:HSlider id="rBar" width="60" minimum="0" maximum="100" value="1" snapInterval="1" liveDragging="true" /> <mx:Label text="上边距" /> <mx:HSlider id="tBar" width="60" minimum="0" maximum="100" value="1" snapInterval="1" liveDragging="true" /> <mx:Label text="下边距" /> <mx:HSlider id="bBar" width="60" minimum="0" maximum="100" value="1" snapInterval="1" liveDragging="true" /> <mx:Spacer width="50"/> <mx:Button label="另存为PDF" click="createPDF()" /> <mx:Button label="Print" click="doPrint()"/> </mx:HBox> </mx:ApplicationControlBar> <mx:Panel layout="absolute" horizontalScrollPolicy="off" title="打印预览区域" paddingBottom="35" paddingLeft="30" paddingRight="30" paddingTop="60" horizontalAlign="center" width="100%" height="100%" > <mx:Box y="30" horizontalScrollPolicy="off" id="containerBox" removedEffect="Resize" borderStyle="solid" horizontalAlign="center" verticalAlign="middle" horizontalCenter="0" paddingLeft="{lBar.value}" paddingRight="{rBar.value}" paddingTop="{tBar.value}" paddingBottom="{bBar.value}" > <mx:Box borderStyle="solid" borderColor="0xEEEEEE" id="printData" borderAlpha="10" borderVisible="false" width="100%" height="100%"> <mx:HTML width="100%" height="100%" location="template/CustomerInfo.htm"> </mx:HTML> </mx:Box> </mx:Box> </mx:Panel> </mx:VBox> </s:WindowedApplication>