i need to print a string directly into the printer
i found this code by searching
uses WinSpool, Printers
type
TDoc_Info_1 = record
pDocName: pChar;
pOutputFile: pChar;
pDataType: pChar;
end;
procedure PrintSimpleText(sPrinter, sText: String);
var
sTitle: String;
hPrinter: THandle;
PrnDocInfo: TDoc_Info_1;
lst: TStringList;
i: Integer;
n: Cardinal;
sTextLine: String;
bFound: Boolean;
begin
lst := TStringList.Create;
try
lst.Text := sText; //with CRLF
//new doc
sTitle := 'Raw print';
ZeroMemory(@PrnDocInfo, SizeOf(TDoc_Info_1));
PrnDocInfo.pDocName := PChar(sTitle);
PrnDocInfo.pDataType := 'RAW';
//find printer (if is installed in windows)
bFound := False;
for i:=1 to Printer.Printers.Count do
begin
if Pos(sPrinter, Printer.Printers.Strings[i-1])>0 then
begin
bFound := True;
sPrinter := Printer.Printers.Strings[i-1];
Printer.PrinterIndex := i-1; //set printer
Break;
end;
end;
if bFound then
begin
// open the printer
if OpenPrinter(PChar(sPrinter), hPrinter, nil) then
begin
//start
StartDocPrinter(hPrinter, 1, @PrnDocInfo);
if StartPagePrinter(hPrinter) then
begin
//print by line
for i := 1 to lst.Count do
begin
sTextLine := lst.Strings[i-1];
if not WritePrinter(hPrinter, PChar(sTextLine), Length(sTextLine), n) then
Break;
end;
//end of page
EndPagePrinter(hPrinter);
//end
EndDocPrinter(hPrinter);
end;
ClosePrinter(hPrinter);
end;
end;
finally
lst.Free;
end;
end;
and this run like this :
procedure TForm1.Button1Click(Sender: TObject);
begin
PrintSimpleText('pdfFactory Pro', 'Tis is a'#13#10'text');
showmessage('aaaa');
end;
1)
but by clicking Button1
it just show a message!!!
is it required to send a custom header with the string for print?
or what is the problem here?
2) also if you think this is not a good way tell me a better solution! i need to submit a string like this to the printer
------------------------------------------------------
your card number is 1111 1111 1111 1111
your name is mr xxxx xxxxxxx
your nationality code is 9999999999
------------------------------------------------------
your password is : 555555
-----------------------------------------------------
at first i tried to save string into a text file and send it to pronter but printer printed the file name at the top of the file
then i tried to create a bitmap image and send it to the machine but the printer is a dot matrix and don't understand the image!!
UPDATE:
this code work perfectly on my pc i think printer is detected and working fine.
procedure TForm1.Button2Click(Sender: TObject);
begin
if OpenDialog1.Execute then
ShellExecute(Handle, 'print', PChar(OpenDialog1.FileName), nil, nil, SW_HIDE) ;
end;