(轉)小票打印

內容轉自:http://hotthing.blog.163.com/blog/static/2967030920090345613676/ 

 

向LPT口打印一個文本文件  
  procedure   TPosFrm.PrintFile(AFileToPrint:   string);  
  var  
    Port,   FileToPrint:   TextFile;  
    ALine:   string;  
  begin  
    AssignFile(FileToPrint,   AFileToPrint);  
    Reset(FileToprint);  
    AssignFile(Port,   'LPT1');  
    Rewrite(Port);  
    while   not   Eof(FileToPrint)   do  
    begin  
        Readln(FileToPrint,   ALine);  
        Writeln(Port,   ALine);  
    end;  
    CloseFile(Port);  
    CloseFile(FileToPrint);  
  end;  
   
   
        //   打印小單  
        //   輸出到文本,稍後再輸出到LPT1端口;  
        OutFile:=ExtractFilePath(Application.ExeName)+'output.txt';  
        AssignFile(F,   OutFile);  
        Rewrite(F);  
        CompanyTitle:=SysParms.CompanyTitle+'購物清單';  
        CompanyTitle:=StringOfChar('   ',   (32-Length(CompanyTitle))   div   2)+CompanyTitle;  
        Writeln(F,   CompanyTitle);  
        Writeln(F,'--------------------------------');  
  //         Writeln(F,'收銀機:'+EdPosID.Text+'           單號:'+EdSaleID.Text);  
  //         Writeln(F,'收銀員:'+EdOpID.Text+   '           日期:'+EdSaleDate.Text);  
        Write(F,'收銀員:',   Format('%-10s',   [STOpID.Caption]));  
        Writeln(F,   '單號:'+STSaleID.Caption);  
        Writeln(F,'日期:'+STSaleDate.Caption+   '     時間:'+FormatDateTime('hh:mm:ss',   Now));  
        Writeln(F,'--------------------------------');  
        //                   12345678901234567890123456789012  
        Writeln(F,'商品名稱                   數量   單價     金額');  
   
        ADSSaleList.DisableControls;  
        ADSSaleList.First;  
        while   not   ADSSaleList.Eof   do  
        begin  
            MerName:=ADSSaleList.FieldValues['MerName'];  
            len:=Length(MerName);  
            if   len   <   18   then  
                MerName:=MerName+StringOfChar('   ',   18-len);  
            if   (len   mod   2)   =   0   then  
                Write(F,   Copy(MerName,   1,   18))  
            else  
                Write(F,   (Copy(MerName,   1,   17)+'   '):18);  
            Write(F,   ADSSaleList.FieldValues['Qty']:2);  
            Write(F,   FormatFloat('0.0',   ADSSaleList.FieldValues['Price']):6);  
            Writeln(F,   FormatFloat('0.0',   ADSSaleList.FieldValues['Amount']):6);  
            ADSSaleList.Next;  
        end;  
        ADSSaleList.EnableControls;  
   
        Writeln(F,'--------------------------------');  
        Writeln(F,'合計:',STHj.Caption:8,   '         讓利:',   STRl.Caption:8);  
        Writeln(F,'實收:',STSs.Caption:8,   '         金卡:',  
                        FormatCurr('0.00',   CardPayment):8  
                        );  
        Writeln(F,'現金:',  
                        FormatCurr('0.00',   StrToCurr(EdCash.Text)-CardPayment):8,  
                        '         找補:',   STBack.Caption:8  
                        );  
        Writeln(F,'   ');   //空行,添加或減少空行即可控制出紙多少。  
        ...  
        PrintFile(Outfile);   //   輸出文件到打印口    
   
   
   
  輸出如下例:  
   
          XX化妝品世界購物清單  
  --------------------------------  
  收銀員:01                 單號:000000329  
  日期:2003-07-16     時間:17:53:28  
  --------------------------------  
  商品名稱                   數量   單價     金額  
  8g華美健強力消痘乳   4     16.0     64.0  
  小口紅                           1       5.0       5.0  
  180g隆力奇蛇膽不緊   3     20.8     62.4  
  速效脫毛露                   2       7.5     15.0  
  美容海藻面膜               3     18.0     54.0  
  蕾琪脣彩                       1     12.0     12.0    
  .....  
  --------------------------------  
  合計:   2058.40         讓利:         0.00  
  實收:   2058.40         金卡:         0.00  
  現金:   2100.00         找補:       41.60  
   
   
  因寬度只有58mm,故商品名切去部分。

 


如果沒有裝打印機的話,怎樣讓它跳出  
  //   向LPT口打印一個文本文件  
  procedure   TPosFrm.PrintFile(AFileToPrint:   string);  
  var  
    Port,   FileToPrint:   TextFile;  
    ALine:   string;  
  begin  
    AssignFile(FileToPrint,   AFileToPrint);  
    Reset(FileToprint);  
    AssignFile(Port,   'LPT1');  
    Rewrite(Port);  
    while   not   Eof(FileToPrint)   do  
    begin  
        Readln(FileToPrint,   ALine);  
        Writeln(Port,   ALine);  
    end;  
    CloseFile(Port);  
    CloseFile(FileToPrint);  
  end;  
   
  這段程序?不然程序無響印了.

 

如果小票打印機是沒有帶硬字庫的怎麼打呢?如EPSON   210PD。  
     
    有兩種方式:一是利用驅動,這樣的話,樓上的方式就不行了,必須是利用windows打印。需要利用windows打印函數,或者報表工具來打印。  
                          二是採用圖形方式打印,利用windows自帶或者ucdos帶的字庫。把每個漢字轉換成圖形點陣字符串,然後用樓上的方式發送給打印機。  

 

錢箱是打印機驅動的  
  Var   F:TextFile;  
  PDStr:string;  
  begin  
      AssignFile(F,   'LPT1');  
      Rewrite(F);  
      //初始化  
      PDStr:=chr(27)+chr(64);  
      Write(F,   PDStr);  
      //開錢箱  
      PDStr:=   Chr(27)+'p'+Chr(0)+Chr(60)+Chr(255);  
      Write(F,   PDStr);  
      CloseFile(F);
  end


不同的錢箱可以彈出的指令會有所不同,一般在廠家的說明書上有提供的  
  如果指令是:27,112,0,100,100,你可以用下面的代碼打開錢箱.  
  Open   "LPT1"   For   Output   As   #1  
  Print   #1,chr(27)   &   chr(112)   &   chr(0)   &   chr(100)   &   chr(100)  
  close   #1

 

 

function   WritePrinter(   s:string):boolean;stdcall;  
  var  
   
        hPrn   :   THandle;  
        c:Cardinal;  
  begin  
   
      hPrn   :=   CreateFile('LPT1',  
                      GENERIC_WRITE   or   GENERIC_READ,  
                      0,  
                      nil,  
                      OPEN_EXISTING,  
                      FILE_ATTRIBUTE_NORMAL,  
                      0);  
      if   hPrn   =   INVALID_HANDLE_VALUE   then  
        begin  
          result:=false;  
          exit;  
        end;  
      c:=0;  
      WriteFile(hPrn,  
                          PChar(s)^,  
                          Length(s),  
                          c,  
                          nil);  
   
      CloseHandle(hPrn);  
      result:=true;  
  end;  
  function   PrintTkt(   name:string;   car:string;   fee:string;  
                                              sfy:string;   y1,y2,y3,y4,y5,y:single):boolean;stdcall;  
  var  
      s:string;  
      t,i:integer;  
      temp:Integer;  
  begin  
   
        temp:=GetPrinterStatus;  
        temp:=temp   and   $f0   ;  
        case   temp   of  
          $d0:   begin  
   
   
        t:=round(6*y/2.54);  
        s:=#27   +   #50   +#27   +   'C'   +   char(t)+#27   +   #68   +   #01   +   #12   +#27+#108   +   #0;  
        t:=round(6*y1/2.54);  
   
        for   i:=0   to   t-1   do  
          s:=s+   #10;  
        s:=s+#09+name;  
        t:=round(6*(y2-y1)/2.54);  
   
        for   i:=0   to   t-1   do  
          s:=s+   #10;  
          s:=s+#09+car;  
        t:=round(6*(y3-y2)/2.54);  
        for   i:=0   to   t-1   do  
          s:=s+   #10;  
          s:=s+#09+fee;  
        t:=round(6*(y4-y3)/2.54);  
   
        for   i:=0   to   t-1   do  
          s:=s+   #10;  
          s:=s+#09+sfy;  
        t:=round(6*(y5-y4)/2.54);  
        for   i:=0   to   t-1   do  
          s:=s+   #10;  
          s:=s+#09+FormatDateTime('yyyy-mm-dd',Now);  
          s:=s+   #10;  
          //s:=s+#27   +   'D'   +   #32;  
          s:=s+#09+#09+FormatDateTime('HH:MM:SS',Now);  
   
          s:=s+   #12;  
   
   
        if   WritePrinter(s)   then   result:=true  
        else  
          result:=false   ;  
        end;  
              $70:   Showmessage('打印機沒打開,請檢查打印機');  
              $60:   Showmessage('打印機缺紙,請檢查打印機');  
              $40:   Showmessage('打印機蓋子沒蓋好,請檢查打印機');  
        end;  
   
   
  end;
手頭上有一個項目需要web下打印小票,就是超市裏面打印的小票

用iframe存放 打印內容
iframe(id).focus();
iframe(id).print();
注意,本方法會彈出系統打印機選擇窗口

<object id="WebBrowser" width=0 height=0 classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2">
</object>

<input type=button value=打印 οnclick=document.all.WebBrowser.ExecWB(6,1)>
<input type=button value=直接打印 onClick="document.all.WebBrowser.ExecWB(6,6);">
<input type=button value=頁面設置 οnclick=document.all.WebBrowser.ExecWB(8,1)>
<input type=button value=打印預覽 onClick="document.all.WebBrowser.ExecWB(7,1);">

這個方法可以不彈出選擇窗,但是要啓用ActiveX


wintable控件...有免費的....

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章