首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >德尔菲打印到通用文本驱动程序(Intermec PM4i)?

德尔菲打印到通用文本驱动程序(Intermec PM4i)?
EN

Stack Overflow用户
提问于 2014-11-08 05:43:13
回答 2查看 2K关注 0票数 0

(编辑)这个问题几乎没有被否决。我不知道原因,也不知道是怎么回事。我可以编辑这个,如果选民可以评论,他们希望看到更好的写作或缺乏有价值的信息,我没有提供)。

我有Intermec PM4i标签打印机和通用文本打印驱动程序。我可以从记事本或德尔菲调用ShellExecute('printto',.)打印文本文件脚本。贝类功能

我发现很少有原始的印刷例子,但都没有用。Delphi应用程序如何在没有shellapi函数的情况下打印到Generic_text_driver?这不是GDI printer.canvas驱动程序。

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

这个printFileToGeneric不起作用。

代码语言:javascript
复制
// 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助手实用程序示例不起作用。

代码语言:javascript
复制
prtRaw.StartRawPrintJob/StartRawPrintPage/PrintRawData/EndRawPrintPage/EndRawPrintJob
http://www.swissdelphicenter.ch/torry/showcode.php?id=940

下面的AssignPrn方法没有工作。

代码语言:javascript
复制
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打印输出。这里少了点什么。

代码语言:javascript
复制
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:端口地址和将文本行写入套接字输出流。

代码语言:javascript
复制
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;
EN

回答 2

Stack Overflow用户

发布于 2014-12-25 12:06:54

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

我希望这能帮到你。

代码语言:javascript
复制
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;
票数 1
EN

Stack Overflow用户

发布于 2014-11-08 11:09:07

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26814192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档