POS打印机的Flex报告打印

时间:2014-02-14 08:50:10

标签: flex printing

我们正在尝试将餐馆账单打印到POS打印机。我们在FLEX中设计了一个完整的a4尺寸的帐单,并设法将其打印到POS打印机。我知道这不是最好的做法。有人可以帮助我们,让我们知道最佳做法是什么。

以下是我们使用的代码。非常感谢一点帮助。

<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:fx="http://ns.adobe.com/mxml/2009"
         xmlns:s="library://ns.adobe.com/flex/spark"
         xmlns:mx="library://ns.adobe.com/flex/mx"
         xmlns:prints="printing.*"
         xmlns:ProComp="ProComp.*"
         xmlns:WrbComp="WrbComp.*"
         width="580" height="338" creationComplete="onCreationComplete()" fontSize="4"
         horizontalAlign="left" horizontalScrollPolicy="off"
         implements="COM.pro.printing.IPrintableDocument" styleName="printPageStyle"
         verticalScrollPolicy="off">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
        <mx:NumberFormatter id="Rod2" precision="2" rounding="nearest" />
        <mx:NumberFormatter id="Rod4" precision="4" rounding="nearest" />
        <mx:NumberFormatter id="mxNumFormatter2"  useThousandsSeparator="true" thousandsSeparatorFrom="," precision="2" decimalSeparatorFrom="." />
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import COM.pro.printing.PrintPreviewControl;

            import Common.*;

            import WrbComp.BillCompKot;

            import flashx.textLayout.conversion.ConversionType;
            import flashx.textLayout.formats.Float;

            import mx.collections.ArrayCollection;
            import mx.collections.ArrayList;
            import mx.controls.AdvancedDataGrid;
            import mx.controls.Alert;
            import mx.core.Application;
            import mx.events.PropertyChangeEvent;
            import mx.messaging.SubscriptionInfo;
            import mx.rpc.events.HeaderEvent;
            import mx.rpc.mxml.Concurrency;
            import mx.utils.StringUtil;

            [Bindable] public var accounts:ArrayCollection;
            [Bindable] public var currentPageNumber:Number = 1;
            [Bindable] public var pageCount:Number = 1;
            public var _parentObj:Object;
            public var _HdrArrCol:ArrayCollection;
            public var _DtlsArrCol:ArrayCollection;
            static var gridheight:int=0;
            public var _DtlsArrOrd:ArrayCollection;
            public var _DtlsArrPrd:ArrayCollection;
            public var items:Number;
            public var qty:int;
            public var Title:String = "";
            public var TinNo:String = "";
            public var Address1:String="";
            public var Address2:String="";
            public var Address3:String="";
            public var PrintWidth:Number=0;
            public var TitleFontSize:Number=0;
            public var AddressFontsize:Number=0;
            public var OtherFontsize:Number=0;
            public var BillNoFontsize:Number=0;
            public var BillTitleFontsize:Number=0;

            var myXML:XMLDocument=new XMLDocument();
            var GrnXML:XMLDocument=new XMLDocument();

            public var Pnt:COM.pro.printing.PrintPreviewControl=new COM.pro.printing.PrintPreviewControl();

            private function fakeData():ArrayCollection
            {
                var COUNT:int = 70;
                var ar:Array = [];
                for (var i:int=0; i<COUNT; ++i)
                {
                    ar.push(
                        {
                            'account':i+1,
                            'control':COUNT-i-1,
                            'description':'Account ' + (i+1).toString()
                        }
                    );
                }
                return new ArrayCollection(ar);
            }            
            private function ReadRep_XML()
            {
                //                var file:File = new File(File.applicationDirectory.nativePath + "\\Config.xml");
                //                var fileStream:FileStream = new FileStream();
                //                fileStream.open(file,FileMode.READ)
                //                var Content:String=fileStream.readUTFBytes(fileStream.bytesAvailable)
                //                myXML.ignoreWhite=true;
                //                myXML.parseXML(Content);
            }
            private function onCreationComplete():void
            {            
                if(_HdrArrCol.length > 0)
                {                
                    accounts = fakeData();

                    lbl_title1.text=_HdrArrCol[0].Title6;
                    lbl_title2.text=_HdrArrCol[0].Title7;
                    //            lbl_Street.text=_HdrArrCol[0].Street;
                    //            lbl_Addres.text=_HdrArrCol[0].Address;
                    //            lbl_City.text=_HdrArrCol[0].City;
                    //    lbl_title3.text=_HdrArrCol[0].City+" - "+_HdrArrCol[0].Pincode+", Ph : "+_HdrArrCol[0].PhoneNumber;
                    if(_HdrArrCol[0].TinNumber!="")
                    {
                        //        lbl_title4.text="TIN No : "+_HdrArrCol[0].TinNumber;
                    }                    
                //    HT_CUSNAM.text = "Name : "+_HdrArrCol[0].GuestName;
                    //    Cus_Add.text = "Address : "+_HdrArrCol[0].City;
                    //        HT_DocNo.text = "Bill No:"+_HdrArrCol[0].BillNo;
                    //        HT_Billtype.text = "Bill Type : "+_HdrArrCol[0].Type;
                    //        Cus_Mob.text="Contact No : "+_HdrArrCol[0].MobileNo;
                    HT_Room.text="TableNo : "+_HdrArrCol[0].TableNo;
                    HT_DocDt.text ="Date         : "+_HdrArrCol[0].Date;
                    var timestr:String=_HdrArrCol[0].Time;
                    HT_Time.text ="Time      : "+timestr.substr(0,5);
                    //        HT_User.text ="User : "+_HdrArrCol[0].UserName;
                    //                    HT_Pax.text = "  Pax :"+_HdrArrCol[0].Pax;
                    //        lbl_SubTotal.text = _HdrArrCol[0].SubTotal;                        
                    //        lbl_ServiceTax.text=_HdrArrCol[0].ServiceTax;                        
                    //lbl_Rounding.text=_HdrArrCol[0].RoundingOff;
                    //    lbl_totalround.text=_HdrArrCol[0].TotalRounded;                        
                    //        lbl_Total.text=_HdrArrCol[0].Total;
                    lbl_KOT.text="K O T No : "+_HdrArrCol[0].KotNo;
                    lbl_Waiter.text = "Waiter      : "+_HdrArrCol[0].WaiterName;
                    //lbl_Vat.text=_HdrArrCol[0].VatAmount;
                    //lbl_DiscountAmt.text=_HdrArrCol[0].DiscountAmt;
                    //lbl_total.text=_HdrArrCol[0].Gross;
                    //lbl_PackageCost.text=_HdrArrCol[0].PackageCost;                        
                    //lbl_ECESS.text=_HdrArrCol[0].ECESS;    
                    //            lbl_Total1.text=_HdrArrCol[0].Total;

                }
                else
                {
                    Alert.show("No Data Available For Print");
                }                
                fnPrintGridSetup();
                layoutDocument();


            }            
            private function focusOut_Round2(CtrlNm:String)
            {
                this[CtrlNm].text=Rod2.format(this[CtrlNm].text)

            }
            private function fnPrintGridSetup():void
            {

                    //printADG.dataProvider = _DtlsArrCol;
                    var gridlen:int=_DtlsArrCol.length;

                    GridHeader.fnSetHeader("Item","Qty");
                    for(var linecnt:int=0;linecnt<gridlen; linecnt++){

                        var lineItem:BillCompKot= new BillCompKot();

                        lineItem.width = this.width-10;                  
                        lineItem.fnSetProps(_DtlsArrCol[linecnt].ProductName,
                            _DtlsArrCol[linecnt].Quantity);                      
                        lineItemGrp.addElement(lineItem);
                        items=linecnt+1;
                        qty= qty+int( _DtlsArrCol[linecnt].Quantity);        
                        lineItemGrp.height = lineItemGrp.height + 20;
                        var str:String=_DtlsArrCol[linecnt].ProductName;
                        /*if(str.length>15)
                        {
                            lineItemGrp.height = lineItemGrp.height+70;
                        }
                        else
                        {
                            lineItemGrp.height = lineItemGrp.height+30;
                        }*/
                    }

                    this.height = 310+ lineItemGrp.height;            
                HT_ItemsNo.text="Items     : "+String(gridlen);
            }

            private function layoutDocument():void
            {
                //printADG.percentHeight = 100;

                validateNow();

                calculatePageCount();
            }

            public function get desiredWidth():Number
            {
                return this.width;
            }

            public function get desiredHeight():Number
            {
                //var desiredheight:Number=Number(gridheight);
                var desiredheight:Number=this.height;
                return desiredheight;

            }

            public function set parentObj(obj:Object):void
            {
                _parentObj = obj;
            }
            public function set HdrArrCol(HAC:ArrayCollection):void
            {
                _HdrArrCol = HAC;
            }
            public function set DtlsArrCol(DAC:ArrayCollection):void
            {
                _DtlsArrCol = DAC;
                var gridlen:int=_DtlsArrCol.length;
                gridheight=400+(20*gridlen);

            }


            [Bindable(event="validPagesChanged")]
            public function get validNextPage():Boolean
            {
                // FIXME PLEASE : sometimes printADG.validNextPage returns false
                // even though it's not on the last page. Yet, if I disable the
                // check for printADG.validNextPage and just use my calculated
                // page numbers, everything displays and prints correctly, despite
                // going beyond what it considers its last page.
                //return printADG.validNextPage /* && currentPageNumber < pageCount*/;
                return false
            }

            public function displayObject():DisplayObject
            {
                return this;
            }                    

            public function nextPage():void
            {
                //                if ( pageCount != currentPageNumber + 1 )
                //                {
                //                    xfooter.visible = false;    
                //                }
                //                else
                //                {
                //                    xfooter.visible = true;    
                //                }

                //printADG.nextPage();
                currentPageNumber++;

                /*if(currentPageNumber == 2)
                {
                removeHeaderFromLayout();
                }*/

                validateNow();
                dispatchEvent(new Event("validPagesChanged"));

            }

            [Bindable(event="validPagesChanged")]
            public function get validPreviousPage():Boolean
            {
                /* see note for validNextPage */
                //return printADG.validPreviousPage /* && currentPageNumber > 1*/;
                return false
            }

            public function previousPage():void
            {
                //                if ( pageCount != currentPageNumber - 1 )
                //                {
                //                    xfooter.visible = false;    
                //                }
                //                else
                //                {
                //                    xfooter.visible = true;    
                //                }

                //printADG.previousPage();
                currentPageNumber--;

                if (currentPageNumber == 1)
                {
                    includeHeaderInLayout();
                }
                validateNow();
                dispatchEvent(new Event("validPagesChanged"));

            }

            public function calculatePageCount():void
            {
                var count:Number=1;
                /*
                while(printADG.validNextPage)
                {
                count++;
                if(count == 2)
                {
                removeHeaderFromLayout();
                }
                printADG.nextPage();
                validateNow();
                }*/
                pageCount = count;
                moveToFirstPage();
            }

            public function moveToFirstPage():void
            {
                currentPageNumber = 1;
                //printADG.moveToFirstPage();
                includeHeaderInLayout();
                validateNow();
                dispatchEvent(new Event("validPagesChanged"));
            }


            private function removeHeaderFromLayout():void
            {
                //header.includeInLayout = false;
                // force a re-calc
                //printADG.percentHeight = 99;
                //printADG.percentHeight = 100;
            }

            private function includeHeaderInLayout():void
            {
                //header.includeInLayout = true;
                // force a re-calc
                //printADG.percentHeight = 99;
                //printADG.percentHeight = 100;
            }


        ]]>
    </fx:Script>
    <s:Group x="2" y="2" width="98%" height="338">    
        <s:VGroup height="333" width="100%" gap="3" y="0" x="23">
            <s:VGroup width="98%" height="87" id="Header" horizontalAlign="center" verticalAlign="bottom" chromeColor="#060303" contentBackgroundColor="#FCFBFB" >                    

                <s:Label id="lbl_title1" x="42" y="3" width="474" height="55" fontSize="60"
                         fontWeight="bold" textAlign="center" fontFamily="Times New Roman"/>
                <s:Label id="lbl_title2" x="97" y="44" width="363" height="20" fontSize="25"
                         fontWeight="bold" textAlign="center" fontFamily="Times New Roman"/>                
            </s:VGroup>            
            <s:Line width="90%" height="0" >
                <s:stroke>
                    <s:SolidColorStroke color="black" weight="2" />
                </s:stroke>
            </s:Line>                        
            <s:VGroup width="98%" height="30" gap="1">
                <s:HGroup height="100%" width="100%" gap="60">
                    <s:Label id="lbl_KOT" width="250" height="100%" fontSize="30" text="K O T No : 145263" 
                             textAlign="left" verticalAlign="top" fontFamily="Times New Roman"/>
                    <s:Label id="HT_Room" width="180" verticalAlign="top" fontFamily="Times New Roman" 
                             height="100%" fontSize="30" text="Table No : 14"/>
                </s:HGroup>                
            </s:VGroup>
            <s:VGroup width="98%" height="30" gap="1">
                <s:HGroup height="100%" width="100%" gap="3">    
                    <s:Label id = "HT_DocDt" width="307" verticalAlign="top" fontFamily="Times New Roman"  
                             height="100%" textAlign="left" fontSize="30" text="Date         : 14/02/2014"/>
                    <s:Label id = "HT_Time" width="210" verticalAlign="top" fontFamily="Times New Roman"  
                             height="100%" textAlign="left" fontSize="30" text="Time       : 15:20"/>
                </s:HGroup>                
            </s:VGroup>     
            <s:VGroup width="98%" height="30" gap="1">
                <s:HGroup height="100%" width="100%" gap="1">    
                    <s:Label id="lbl_Waiter" width="315" height="100%" fontSize="30" fontFamily="Times New Roman"
                             textAlign="left" verticalAlign="top" text="Waiter      : fgdhfghfghfg"/>
                    <s:Label id = "HT_ItemsNo" width="178" verticalAlign="top" fontFamily="Times New Roman"  
                             height="100%" textAlign="left" fontSize="30" text="Items      : 99"/>
                </s:HGroup>                
            </s:VGroup>                
            <WrbComp:BillCompKot id="GridHeader" width="98%" height="30"/>        
            <s:Line width="90%" height="0" >
                <s:stroke>
                    <s:SolidColorStroke color="black" weight="2" />
                </s:stroke>
            </s:Line>            
            <s:VGroup width="98%"  id="lineItemGrp" gap="2">
            </s:VGroup>
            <s:Line width="90%" height="0" >
                <s:stroke>
                    <s:SolidColorStroke color="black" weight="2" />
                </s:stroke>
            </s:Line>    
            <s:HGroup width="98%" height="50" gap="0">
                <s:VGroup width="98%" height="100%" gap="0">                    
                    <s:HGroup height="44" width="100%" gap="80" verticalAlign="bottom" >
                        <s:Label text="" width="50%" height="25" fontFamily="Times New Roman" fontWeight="bold" verticalAlign="top" fontSize="25" x="130" y="0" textAlign="left"/> 
                        <s:Label width="146" height="25" fontSize="25" fontFamily="Times New Roman" fontWeight="bold" text="CASHIER"
                                 textAlign="right" verticalAlign="top"/>                
                    </s:HGroup>                                            
                </s:VGroup>
            </s:HGroup>            
            <s:Line width="90%" height="0" >
                <s:stroke>
                    <s:SolidColorStroke color="black" weight="2" />
                </s:stroke>
            </s:Line>
        </s:VGroup>
    </s:Group>
</mx:VBox>

1 个答案:

答案 0 :(得分:0)

使用system.drawing.printing dll并将所有内容转换为XML。完成。长期研究后,问题得以解决感谢Shameem和Arun在我们的团队中努力解决这个问题。

我们使用的示例代码。

XmlDocument dc = new XmlDocument();
            dc.LoadXml(Dtls);
            int len = dc.SelectNodes("//GridXml").Count;
            PrintDialog pd = new PrintDialog();
            pdoc = new PrintDocument();
            PrinterSettings ps = new PrinterSettings();
            Font font = new Font("Courier New", 15);
            PaperSize psize = new PaperSize("Custom", 300, 300);
            pd.Document = pdoc;
            pd.Document.DefaultPageSettings.PaperSize = psize;
            // 230 + (len * 15) KOT
            // 410+(len*15) - Bill
            pdoc.DefaultPageSettings.PaperSize.Height = 230 + (len * 15) + 410 + (len * 15);
            pdoc.DefaultPageSettings.PaperSize.Width = 300;
            pdoc.PrintPage += new PrintPageEventHandler(pdoc_PrintPage);
            //pdoc.Print();
            PrintDialog printdlg = new PrintDialog();
            PrintPreviewDialog printPrvDlg = new PrintPreviewDialog();
            printPrvDlg.Document = pdoc;
            printPrvDlg.ShowDialog(); // this shows the preview and then show the Printer Dlg below
            printdlg.Document = pdoc;
            if (printdlg.ShowDialog() == DialogResult.OK)
            {
                pdoc.Print();
            }
        }