uses Printers, WinSpool; function WriteRawDataToPrinter(PrinterName: String; Str: String): Boolean; var PrinterHandle: THandle; DocInfo: TDocInfo1; i: Integer; B: Byte; Escritos: DWORD; begin Result:= FALSE; if OpenPrinter(PChar(PrinterName), PrinterHandle, nil) then try FillChar(DocInfo,Sizeof(DocInfo),#0); with DocInfo do begin pDocName:= PChar('Printer Test'); pOutputFile:= nil; pDataType:= 'RAW'; end; if StartDocPrinter(PrinterHandle, 1, @DocInfo) <> 0 then try if StartPagePrinter(PrinterHandle) then try while Length(Str) > 0 do begin if Copy(Str, 1, 1) = '\' then begin if Uppercase(Copy(Str, 2, 1)) = 'X' then Str[2]:= '$'; if not TryStrToInt(Copy(Str, 2, 3),i) then Exit; B:= Byte(i); Delete(Str, 1, 3); end else B:= Byte(Str[1]); Delete(Str,1,1); WritePrinter(PrinterHandle, @B, 1, Escritos); end; Result:= TRUE; finally EndPagePrinter(PrinterHandle); end; finally EndDocPrinter(PrinterHandle); end; finally ClosePrinter(PrinterHandle); end; end;
"PrinterName" es el nombre de la impresora tal como aparece en la carpeta de impresoras de windows, esto permite que nos olvidemos de si la impresora es serie, paralelo o usb, funcionara en todos los casos. Si la impresora se conecta por el puerto serie o paralelo y no quieres instalar los drivers, instala una impresora "Genérica / solo texto" indicándole el puerto donde esta. Por otro lado el parámetro Str es una cadena de texto donde los caracteres especiales están precedidos por el carácter "\" seguido de 3 cifras decimales o de "x" y dos cifras hexadecimales.
Por ejemplo, para que la impresora emita un pitido (las secuencias pueden variar de una impresora a otra):
\x1B@\x0A\x0D\x1B\x07\x0A\x0D
Para que corte el papel:
\x1B@\x0A\x0D\x1Bi\x0A\x0D
Para abrir el cajon de monedas
\x1B@\x0A\x0D\027\112\000\100\250\x0A\x0D
Para que escriba "Hola mundo"

\x1B@\x0A\x0DHola mundo\x0A\x0D