Search code examples
delphidelphi-7

Delphi print memo to zebra printer


I need help. I have this code to print by zebra, and it prints nothing.

  procedure TForm1.Button3Click(Sender: TObject);
    var
    cm: AnsiString;
    zpl : TStringList;
    p: TPrinter;
    i: integer;
    begin
    p := Printer;
    zpl := TStringList.Create;
    memo2.Lines.Add('^XA');
    memo2.Lines.Add('^LH5,5');
    memo2.Lines.Add(concat ('^FO50,20^ADN,36,20^FD', edit1.text,'^FS'));
    memo2.Lines.Add(concat ('^FO35,60^ADN,15,10^FD', edit4.text,'^FS'));
    memo2.Lines.Add(concat ('^FO50,115^BCN,60,N,N,N^FD', edit1.text, '^FS'));
    memo2.Lines.Add('^XZ');
    zpl.AddStrings (memo2.lines);
    p.BeginDoc;
    for i := 0 to memo2.Lines.Count-1 do
    begin
    zpl.Assign(memo2.lines);
    end;
    p.EndDoc
    end;

thank you to help me I know to print the lines as a text, but I need to send these lines as command to Zebra printer.


Solution

  • There are a couple of options. You can open the printer in RAW mode and write those commands using WritePrinter(). There is a non-working example here and below:

    uses
      Printers, WinSpool;
    
    procedure Print;
    var 
      ADevice, ADeviceName, ADevicePort: array[0..255]of Char;
      PrinterHandle: THandle
      DocInfo: TDocInfo1;
      dwJob: cardinal;
      dwBytesWritten: cardinal;
      AUtf8: UTF8string;
      ADeviceMode: THandle;
    begin
      //your printer (a windows generic printer works fine)
      Printer.PrinterIndex := LocalPrinterIndex; 
      Printer.GetPrinter(ADevice, ADeviceName, ADevicePort, ADeviceMode);
    
      //Need a handle to the printer
      if not OpenPrinter(ADevice, FPrinterHandle, nil) then 
        Exit;
    
      //Fill in the structure with info about this "document"
      DocInfo.pDocName := PChar('Spooler Document Name');
      DocInfo.pOutputFile := nil;
      DocInfo.pDatatype := 'RAW';
    
      //Inform the spooler the document is beginning
      dwJob := StartDocPrinter(PrinterHandle, 1, @DocInfo);
      if dwJob = 0 then 
      begin
        ClosePrinter(PrinterHandle);
        FPrinterHandle := 0;
        Exit;
      end;
    
      //Start a page
      if not StartPagePrinter(PrinterHandle) then 
      begin
        EndDocPrinter(PrinterHandle);
        ClosePrinter(PrinterHandle);
        FPrinterHandle := 0;
        Exit;
      end;
    
      //your zebra code... 
      AUtf8 := UTF8string('Hello world');
      WritePrinter(PrinterHandle, @AUtf8[1], Length(AUtf8), dwBytesWritten);
    
      //End the page
      if not EndPagePrinter(PrinterHandle) then 
      begin
        EndDocPrinter(PrinterHandle);
        ClosePrinter(PrinterHandle);
        FPrinterHandle := 0;
        Exit;
      end;
    
      //Inform the spooler that the document is ending
      if not EndDocPrinter(PrinterHandle) then 
      begin
        ClosePrinter(PrinterHandle);
        FPrinterHandle := 0;
        Exit;
      end;
    
      //Tidy up the printer handle
      ClosePrinter(PrinterHandle);
      FPrinterHandle := 0;
    end;
    

    Or you can install a Generic Text Only driver for that printer and print using old fashioned Pascal WriteLn calls, as described here. You will need to create a SelectPrinter() method of your own. Included below:

    procedure PrintLabel(LabelCommands: TStringList);
    var
      F: TextFile;
      I: Integer;
    begin
      SelectPrinter('LABEL PRINTER');
    
      AssignPrn(F);
      ReWrite(F);
    
      for I := 0 to LabelCommands.Count - 1 do
        WriteLn(F,LabelCommands[I]);
    
      CloseFile(F);
    end;