【问题标题】:Thermal printer does not know when to stop feeding paper (Length)热敏打印机不知道何时停止进纸(长度)
【发布时间】:2022-11-01 12:29:32
【问题描述】:

我为我兄弟的餐厅制作了一个程序,将.txt 文件发送到热敏打印机。我遇到的问题(或者至少是我在想的问题)在于文件的长度。

这是我用于打印过程的代码(来自Help needed about printing text file with delphi

procedure TForm1.PrintTextFile(const FileName: string; const Numbering: boolean = true);
const
  FONT_NAME = 'Times New Roman';
  FONT_SIZE = 14;
var
  MARGIN: integer;
  sl1: TStringList;
  i, h: Integer;
  r, rFooter: TRect;
  s: string;
  DocEnd: integer;
begin
  with TPrintDialog.Create(nil) do
    try
      if not Execute then
        Exit;
    finally
      Free;
    end;
  sl1 := TStringList.Create;
  try
    sl1.LoadFromFile(FileName);
    Printer.BeginDoc;
    Printer.Title := FileName; // or application name or sth else
    Printer.Canvas.Font.Name := FONT_NAME;
    Printer.Canvas.Font.Size := FONT_SIZE;
    MARGIN := 1*Printer.Canvas.TextWidth('M');
    DocEnd := Printer.PageHeight - MARGIN;
    if Numbering then
    begin
      dec(DocEnd, 2*Printer.Canvas.TextHeight('8'));
      rFooter := Rect(0, DocEnd, Printer.PageWidth, Printer.PageHeight - MARGIN);
      DrawText(Printer.Canvas.Handle,
        PChar(IntToStr(Printer.PageNumber)),
        length(IntToStr(Printer.PageNumber)),
        rFooter,
        DT_SINGLELINE or DT_CENTER or DT_BOTTOM);
    end;
    r.Left := MARGIN;
    r.Top := MARGIN;
    for i := 0 to sl1.Count - 1 do
    begin
      r.Right := Printer.PageWidth - MARGIN;
      r.Bottom := DocEnd;
      s := sl1.Strings[i];
      if s = '' then s := ' ';
      h := DrawText(Printer.Canvas.Handle, // Height of paragraph on paper
        PChar(s),
        length(s),
        r,
        DT_LEFT or DT_TOP or DT_WORDBREAK or DT_CALCRECT);
      if r.Top + h >= DocEnd then
      begin
        Printer.NewPage;
        if Numbering then
          DrawText(Printer.Canvas.Handle,
            PChar(IntToStr(Printer.PageNumber)),
            length(IntToStr(Printer.PageNumber)),
            rFooter,
            DT_SINGLELINE or DT_CENTER or DT_BOTTOM);
        r.Top := MARGIN;
        r.Bottom := DocEnd;
      end;
      if h > Printer.PageHeight - 2*MARGIN then
        raise Exception.Create('Line too long to fit on single page.');
      DrawText(Printer.Canvas.Handle,
        PChar(s),
        length(s),
        r,
        DT_LEFT or DT_TOP or DT_WORDBREAK);
      inc(r.Top, h);
    end;
    Printer.EndDoc;
  finally
    sl1.Free;
  end;
end;

这是发送到热敏打印机的.txt 文件:

这就是它的打印方式:

【问题讨论】:

    标签: delphi


    【解决方案1】:

    我认为您需要在执行任何操作之前查看打印机的驱动程序设置。在驱动程序设置中,您可以设置最大纸张高度。您还可以使用记事本将文件打印到打印机并将结果与​​您的程序结果进行比较。 当您打印到热敏打印机时,我有一些关于您的代码的注释:

    1. 在计算中使用纸张高度不是一个好主意。
    2. 您不要添加新页面,因为这会使打印机裁切 纸。
    3. 如果您使用的是 Windows 并想了解打印机 您可以使用类似以下函数 GetActualMargins 的边距。
      
      
          //This function works on the current printer
          //Dpi : Dot per inch
              procedure GetActualMargins(var DpiX, DpiY : Integer;  var LeftMargin, TopMargin: Single);
              begin
                try
                  DpiX := GetDeviceCaps(Printer.Handle, LOGPIXELSX);
                  DpiY := GetDeviceCaps(Printer.Handle, LOGPIXELSY);
                  LeftMargin := Round((25.4 * GetDeviceCaps(Printer.Handle, PHYSICALOFFSETX)) / DpiX );
                  TopMargin := Round((25.4 * GetDeviceCaps(Printer.Handle, PHYSICALOFFSETY)) / DpiY );
                except
                //you can raise Exception here
                end;
              end;
      
      

    【讨论】:

      猜你喜欢
      • 2021-09-12
      • 2012-06-05
      • 1970-01-01
      • 1970-01-01
      • 2012-10-05
      • 2016-02-17
      • 2015-10-25
      • 2012-12-03
      相关资源
      最近更新 更多