内容转自: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=打印 onclick=document.all.WebBrowser.ExecWB(6,1)>
<input type=button value=直接打印 onClick="document.all.WebBrowser.ExecWB(6,6);">
<input type=button value=页面设置 onclick=document.all.WebBrowser.ExecWB(8,1)>
<input type=button value=打印预览 onClick="document.all.WebBrowser.ExecWB(7,1);">
这个方法可以不弹出选择窗,但是要启用ActiveX
wintable控件...有免费的....