Author: Maarten de Haan
How to send a raw string to the printer
Answer:
procedure PrintRawStr(const S: ANSIString);
1 uses WinSpool, Printers;
2 3 var4 sDefaultPrinter: string;
5 Handle: THandle;
6 dwN: DWORD;
7 diDocInfo1: TDocInfo1; // Uses WinSpool8 bP: BYTE;
9 10 begin11 // Get the default printer or the printer choosen in the Printer Setup Dialog12 // if you have one in the application13 if Printer.Printers.Count > 0 then14 begin15 sDefaultPrinter := Printer.Printers[Printer.PrinterIndex]; // Uses Printers16 //uses Printers, get default printer17 bP := Pos(' on ', sDefaultPrinter);
18 if bP > 0 then19 sDefaultPrinter := Copy(sDefaultPrinter, 1, bP - 1);
20 end21 else22 Exit; // No printers installed on this system...23 24 ifnot OpenPrinter(PChar(sDefaultPrinter), Handle, nil) then25 begin26 case GetLastError of27 87: ShowMessage('Printer name does not exists.');
28 else29 ShowMessage('Error ' + IntToStr(GetLastError)); // Uses Dialogs30 end;
31 Exit; // Cannot find the printer32 end;
33 34 with diDocInfo1 do35 begin36 pDocName := PChar('Print job raw'); // Will be seen in printer spooler37 pOutputFile := nil;
38 pDataType := 'RAW';
39 end;
40 41 StartDocPrinter(Handle, 1, @diDocInfo1);
42 StartPagePrinter(Handle);
43 WritePrinter(Handle, PChar(S), Length(S), dwN);
44 EndPagePrinter(Handle);
45 EndDocPrinter(Handle);
46 ClosePrinter(Handle);
47 end;