0

编辑这个问题很少收到反对票。我不知道原因,仍然看不出它有什么问题。如果反对票的人可以评论他们希望看到更好的书面内容或缺乏我没有给出的有价值的信息,我可以编辑这个)。

我有 Intermec PM4i 标签打印机和通用文本打印驱动程序。我能够从记事本或 Delphi 调用 ShellExecute('printto',..) shellapi 函数打印文本文件脚本。

我发现很少有原始打印示例,但没有一个工作。Delphi 应用程序如何在没有 shellapi 功能的情况下打印到 Generic_text_driver?这不是 GDI printer.canvas 驱动程序。

我已经尝试过“一切”,甚至是传统的 PASSTHROUGH 打印示例。唯一的工作方法是 Notepad.exe 或 shellexecute('printto', 'tempfile.txt',...) 我猜这是在内部使用记事本。我可以看到记事本打印对话框在屏幕上闪烁。我想直接从 Delphi 应用程序控制它。

此 printFileToGeneric 不起作用。

// https://groups.google.com/forum/#!topic/borland.public.delphi.winapi/viHjsTf5EqA
Procedure PrintFileToGeneric(Const sFileName, printerName, docName: string; ejectPage: boolean);
Const
  BufSize = 16384;
Var
  Count : Integer;
  BytesWritten: DWORD;
  hPrinter: THandle;
  DocInfo: TDocInfo1;
  f: file;
  Buffer: Pointer;
  ch: Char;
Begin
  If not WinSpool.OpenPrinter(PChar(printerName), hPrinter, nil) Then
    raise Exception.Create('Printer not found');

  Try
    DocInfo.pDocName := PChar(docName);
    DocInfo.pOutputFile := nil;
    DocInfo.pDatatype := 'RAW';
    If StartDocPrinter(hPrinter, 1, @DocInfo) = 0 Then
      Raise Exception.Create('StartDocPrinter failed');

    Try
      If not StartPagePrinter(hPrinter) Then
        Raise Exception.Create('StartPagePrinter failed');

      System.Assign(f, sFileName);
      Try
        Reset(f, 1);
        GetMem(Buffer, BufSize);
        Try
          While not eof(f) Do Begin
            Blockread(f, Buffer^, BufSize, Count);
            If Count > 0 Then Begin
              If not WritePrinter(hPrinter, Buffer, Count, BytesWritten) Then
                Raise Exception.Create('WritePrinter failed');
            End;
          End;
        Finally
          If ejectPage Then Begin
            ch:= #12;
            WritePrinter( hPrinter, @ch, 1, BytesWritten );
          End;
          FreeMem(Buffer, BufSize);
        End;
      Finally
        EndPagePrinter(hPrinter);
        System.Closefile( f );
      End;
    Finally
      EndDocPrinter(hPrinter);
    End;
  Finally
    WinSpool.ClosePrinter(hPrinter);
  End;
End;

以下 prtRaw 帮助器实用程序示例不起作用。

prtRaw.StartRawPrintJob/StartRawPrintPage/PrintRawData/EndRawPrintPage/EndRawPrintJob
http://www.swissdelphicenter.ch/torry/showcode.php?id=940

以下 AssignPrn 方法不起作用。

function testPrintText(params: TStrings): Integer;
var
  stemp:String;
  idx: Integer;
  pOutput: TextFile;
begin
  Result:=0;    
    idx := getPrinterIndexByName( params.Values['printer'] );
    if (idx<0) then Raise Exception.Create('Printer not found');
    WriteLn('Use printer(text) ' + IntToStr(idx) + ' ' + Printer.Printers[idx] );

    Printer.PrinterIndex := idx;
    stemp := params.Values['jobname'];
    if (stemp='') then stemp:='rawtextprint';
    Printer.Title:=stemp;

    AssignPrn(pOutput);
    ReWrite(pOutput);

    stemp := 'INPUT ON'+#10;
    stemp := stemp + 'NASC 1252'+#10;
    stemp := stemp + 'BF OFF'+#10;
    stemp := stemp + 'PP 30,480:FT "Swiss 721 BT",8,0,100'+#10;
    stemp := stemp + 'PT "Test text 30,480 position ABC'+#10;
    Write(pOutput, stemp);
    //Write(pOutput, 'INPUT ON:');
    //Write(pOutput, 'NASC 1252:');
    //Write(pOutput, 'BF OFF:');
    //Write(pOutput, 'PP 30,480:FT "Swiss 721 BT",8,0,100:');
    //Write(pOutput, 'PT "Test text 30,480 position ABC":');
    //Write(pOutput, 'Text line 3 goes here#13#10');
    //Write(pOutput, 'Text line 4 goes here#13#10');    

    CloseFile(pOutput);
end;

这个 Printer.Canvas 没有打印任何东西,它应该已经打印了一些东西,因为记事本在内部使用 GDI 打印输出。这里缺少一些东西。

function testPrintGDI(params: TStrings): Integer;
var
  filename, docName:String;
  idx: Integer;
  lines: TStrings;
begin
  Result:=0;
  idx := getPrinterIndexByName( params.Values['printer'] );
  if (idx<0) then Raise Exception.Create('Printer not found');
  WriteLn('Use printer(gdi) ' + IntToStr(idx) + ' ' + Printer.Printers[idx] );
  docName := params.Values['jobname'];
  if (docName='') then docName:='rawtextprint';

  filename := params.values['input'];
  If Not FileExists(filename) then Raise Exception.Create('input file not found');

  Printer.PrinterIndex := idx;
  Printer.Title := docName;
  Printer.BeginDoc;
  lines := readTextLines(filename);
  try
    for idx := 0 to lines.Count-1 do begin
      Printer.Canvas.TextOut(10, 10*idx, lines[idx]);
    end;
  finally
    FreeAndNil(lines);
    Printer.EndDoc;
  end;
end;

只有三种有效的方法是从 Notepad.exe 打印、Delphi ShellExecute 调用或打开原始 TCP 套接字到 IP:PORT 地址并将文本行写入套接字输出流。

function testPrintPrintTo(params: TStrings): Integer;
var
  filename, printerName:String;
  idx: Integer;
  exInfo: TShellExecuteInfo;
  device,driver,port: array[0..255] of Char;
  hDeviceMode: THandle;
  timeout:Integer;
  //iRet: Cardinal;
begin
  Result:=0;

  idx := getPrinterIndexByName( params.Values['printer'] );
  if (idx<0) then Raise Exception.Create('Printer not found');
  WriteLn('Use printer(printto) ' + IntToStr(idx) + ' ' + Printer.Printers[idx] );

  filename := params.values['input'];
  If Not FileExists(filename) then Raise Exception.Create('input file not found');
  filename := uCommon.absoluteFilePath(filename);

  Printer.PrinterIndex := idx;
  Printer.GetPrinter(device, driver, port, hDeviceMode);
  printerName := Format('"%s" "%s" "%s"', [device, driver, port]);

  FillChar(exInfo, SizeOf(exInfo), 0);
  with exInfo do begin
    cbSize := SizeOf(exInfo);
    fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_FLAG_DDEWAIT;
    Wnd := 0;
    exInfo.lpVerb := 'printto';
    exInfo.lpParameters := PChar(printerName);
    lpFile := PChar(filename);
    lpDirectory := nil;
    nShow := SW_HIDE;
  end;
  WriteLn('printto ' + printerName);

  if Not ShellExecuteEx(@exInfo) then begin
    Raise Exception.Create('ShellExecuteEx failed');
    exit;
  end;

  try
    timeout := 30000;
    while WaitForSingleObject(exInfo.hProcess, 50) <> WAIT_OBJECT_0 do begin
      Writeln('wait timeout ' + IntToStr(timeout));
      dec(timeout, 50);
      if (timeout<1) then break;
    end;
  finally
    CloseHandle(exInfo.hProcess);
  end;

  {iRet:=ShellExecute(0, 'printto',
      PChar(filename),
      PChar(printerName), //Printer.Printers[idx]),
      nil, //PChar(filepath),
      SW_HIDE  // SW_SHOWNORMAL
  );
  Writeln('printto return code ' + IntToStr(iRet)); // >32 OK }

end;
4

2 回答 2

1

您可以使用此过程:LabelFile 是标签文件的完整路径,我们正在使用此代码并与通用文本驱动程序打印机一起使用,并且打印机被设置为默认打印机。它适用于斑马打印机和 windows xp 操作系统。

我希望这能帮到您。

function PrintLabel(LabelName: String): Boolean;
var
  EfsLabel,dummy: TextFile;
  ch : Char;
begin
try
try
   Result:= False;
   AssignFile(EfsLabel,LabelName);
   Reset(EfsLabel);
   Assignprn(dummy);
   ReWrite(Dummy);

   While not Eof(EfsLabel) do
   begin
    Read(EfsLabel, Ch);
    Write(dummy, Ch);
   end;

   Result:= True;

except
  Result:=False;
  LogMessage('Error Printing Label',[],LOG_ERROR);
end;

finally
CloseFile(EfsLabel);
CloseFile(dummy);
end;

end;
于 2014-12-25T12:06:54.753 回答
0

您可以从记事本打印到此打印机。使用标准 GDI 机制的记事本打印。如果记事本可以做到这一点,那么你也可以。使用Printer.BeginDocPrinter.Canvas打印到此打印机。

于 2014-11-08T11:09:07.870 回答