2

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;
6
  • 1
    Read the documentation for your printer and do what it says. You can't really just spew text at a printer and hope that it can read your mind. Commented Aug 18, 2014 at 20:29
  • 1
    The code you present was written for AnsiString. In XE6 standard string type is unicode. Change pChar to pAnsiChar and String to AnsiString. Commented Aug 18, 2014 at 20:30
  • 1
    @DavidHeffernan pdfFactory Pro a virtual universal printer to print string to a pdf file!! aslo my post have two question :) thank you Commented Aug 18, 2014 at 20:45
  • 1
    Your program doesn't notify the user if the OpenPrinter or WritePrinter fails. You should add notifications and see if either of those fails. Commented Aug 18, 2014 at 20:47
  • 1
    @LURD your changed dont helped. Commented Aug 18, 2014 at 20:49

4 Answers 4

5

Example how to write some text directly to the default printer like to the text file.

uses ..., Printers;

...

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
  lst: TextFile;
begin
  AssignPrn(lst);
  Rewrite(lst);
  try
    Writeln(lst, '      your card number is 1111 1111 1111 1111');
    Writeln(lst, '           your name is mr xxxx xxxxxxx');
    Writeln(lst, '        your nationality code is 9999999999');
    Writeln(lst, '------------------------------------------------------');
  finally
    CloseFile(lst);
  end;
end;

Especially useful with matrix printers.

Sign up to request clarification or add additional context in comments.

Comments

4

this can do your job too

  Printer.BeginDoc;
  Printer.Canvas.TextOut(0,0,'Place any text here');
  Printer.EndDoc;

also with canvas you can edit styling too

   Printer.Canvas.Font.size:=18;
   Printer.Canvas.Font.style := [fsbold];

Comments

4

If the printer is available over the network:

procedure Print(const AText: string);
var
  F: TStreamWriter;
begin
  F := TStreamWriter.Create('\\printserver\printername', False, TEncoding.Default);
  try
    F.Write(AText);
  finally
    F.Free;
  end;
end;

(Tested with Delphi 2009)

Comments

1

When need send commands directly to printer can use the class "Printer". To writing one string use the code:

procedure TForm1.btTextoNormalClick(Sender: TObject);
begin

  ComandoAnsiString := 'Example text';

  if not OpenPrinter(PChar(driverName), HandleImp, nil) then 
    Memo1.Lines.Add('Erro: Impressora não encontrada')

  else
  begin

  Documento.pDocName := PChar('Minha impressão');
  Documento.pOutputFile := nil;
  Documento.pDataType := 'RAW';


  StartDocPrinter(HandleImp, 1, @Documento);

  StartPagePrinter(HandleImp);


  WritePrinter(HandleImp, PAnsiChar(ComandoAnsiString), Length(ComandoAnsiString), CaracteresImpressos);

  EndPagePrinter(HandleImp);
  EndDocPrinter(HandleImp);
  ClosePrinter(HandleImp);


  end;

end;

To get the driverName use the code in a ComboBox:

cbbDriver.Items := Printer.Printers;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.