⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 idlpr.pas

📁 Indy控件的使用源代码
💻 PAS
📖 第 1 页 / 共 2 页
字号:
{ $HDR$}
{**********************************************************************}
{ Unit archived using Team Coherence                                   }
{ Team Coherence is Copyright 2002 by Quality Software Components      }
{                                                                      }
{ For further information / comments, visit our WEB site at            }
{ http://www.TeamCoherence.com                                         }
{**********************************************************************}
{}
{ $Log:  10241: IdLPR.pas 
{
{   Rev 1.0    2002.11.12 10:44:46 PM  czhower
}
unit IdLPR;

(*******************************************************}
{                                                       }
{       Indy Line Print Remote TIdLPR                   }
{                                                       }
{       Version 9.1.0                                   }
{                                                       }
{       Original author Mario Mueller                   }
{                                                       }
{       home: www.hemasoft.de                           }
{       mail: babelfisch@daybyday.de                    }
{                                                       }
{       27.07. rewrite component for integration        }
{              in Indy core library                     }
{                                                       }
{*******************************************************)

interface

uses
  Classes,
  IdAssignedNumbers, IdException, IdGlobal, IdTCPClient, IdComponent, SysUtils;

type
  TIdLPRFileFormat =
    (ffCIF, // CalTech Intermediate Form
     ffDVI, //   DVI (TeX output).
     ffFormattedText, //add formatting as needed to text file
     ffPlot, //   Berkeley Unix plot library
     ffControlCharText, //text file with control charactors
     ffDitroff, // ditroff output
     ffPostScript, //Postscript output file
     ffPR,//'pr' format    {Do not Localize}
     ffFORTRAM, // FORTRAN carriage control
     ffTroff, //Troff output
     ffSunRaster); //  Sun raster format file

const
  DEF_FILEFORMAT = ffControlCharText;
  DEF_INDENTCOUNT = 0;
  DEF_BANNERPAGE = False;
  DEF_OUTPUTWIDTH = 0;
  DEF_MAILWHENPRINTED = False;
type
  TIdLPRControlFile = class(TPersistent)
  protected
    FBannerClass: String;			// 'C'    {Do not Localize}
    FHostName: String;				// 'H'    {Do not Localize}
    FIndentCount: Integer;		// 'I'    {Do not Localize}
    FJobName: String;					// 'J'    {Do not Localize}
    FBannerPage: Boolean;			// 'L'    {Do not Localize}
    FUserName: String;					// 'P'    {Do not Localize}
    FOutputWidth: Integer;		// 'W'    {Do not Localize}

    FFileFormat : TIdLPRFileFormat;
    FTroffRomanFont : String; //substitue the Roman font with the font in file
    FTroffItalicFont : String;//substitue the Italic font with the font in file
    FTroffBoldFont : String;  //substitue the bold font with the font in file
    FTroffSpecialFont : String; //substitue the special font with the font
                                //in this file
    FMailWhenPrinted : Boolean; //mail me when you have printed the job
  public
    constructor Create;
    procedure Assign(Source: TPersistent); override;
    property HostName: String read FHostName write FHostName;
  published
    property BannerClass: String read FBannerClass write FBannerClass;
    property IndentCount: Integer read FIndentCount write FIndentCount
      default DEF_INDENTCOUNT;
    property JobName: String read FJobName write FJobName;
    property BannerPage: Boolean read FBannerPage write FBannerPage
      default DEF_BANNERPAGE;
    property UserName: String read FUserName write FUserName;
    property OutputWidth: Integer read FOutputWidth write FOutputWidth
      default DEF_OUTPUTWIDTH;
    property FileFormat: TIdLPRFileFormat read FFileFormat write FFileFormat
      default DEF_FILEFORMAT;
    {font data }
    property TroffRomanFont : String read FTroffRomanFont write FTroffRomanFont;
    property TroffItalicFont : String read FTroffItalicFont
      write FTroffItalicFont;
    property TroffBoldFont : String read FTroffBoldFont write FTroffBoldFont;
    property TroffSpecialFont : String read FTroffSpecialFont
      write FTroffSpecialFont;
    {misc}
    property MailWhenPrinted : Boolean read FMailWhenPrinted
      write FMailWhenPrinted default DEF_MAILWHENPRINTED;

  end;

type
  TIdLPRStatus = (psPrinting, psJobCompleted, psError, psGettingQueueState,
    psGotQueueState, psDeletingJobs, psJobsDeleted, psPrintingWaitingJobs,
    psPrintedWaitingJobs);

type
  TIdLPRStatusEvent = procedure(ASender: TObject;
    const AStatus: TIdLPRStatus;
    const AStatusText: String) of object;

type
  TIdLPR = class(TIdTCPClient)
  protected
    FOnLPRStatus: TIdLPRStatusEvent;
    FQueue: String;
    FJobId: Integer;
    FControlFile: TIdLPRControlFile;
    procedure DoOnLPRStatus(const AStatus: TIdLPRStatus;
    const AStatusText: String);
    procedure SeTIdLPRControlFile(const Value: TIdLPRControlFile);
    procedure CheckReply;
    function GetJobId: String;
    procedure SetJobId(JobId: String);
    procedure InternalPrint(Data: TStream);
    function GetControlData: String;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Print(AText: String); overload;
    procedure Print(ABuffer: array of Byte); overload;
    procedure PrintFile(AFileName: String);
    function GetQueueState(const AShortFormat: Boolean = false;
      const AList : String = '') : String;    {Do not Localize}
    procedure PrintWaitingJobs;
    procedure RemoveJobList(AList : String; const AAsRoot: Boolean =False);
    property JobId: String read GetJobId write SetJobId;
  published
    property Queue: String read FQueue write FQueue;
    property ControlFile: TIdLPRControlFile read FControlFile write SeTIdLPRControlFile;
    property OnLPRStatus: TIdLPRStatusEvent read FOnLPRStatus write FOnLPRStatus;
  end;

type EIdLPRErrorException = class(EIdException);

implementation
uses IdResourceStrings, IdStack;

{*********************** TIdLPR **********************}
constructor TIdLPR.Create(AOwner: TComponent);
begin
  Inherited Create(AOwner);

  Port := IdPORT_LPD;
  Queue := 'pr1';    {Do not Localize}
  FJobId := 1;
  FControlFile := TIdLPRControlFile.Create;

  // Restriction in RFC 1179
  // The source port must be in the range 721 to 731, inclusive.

//  known -problem with this some trouble while multible printjobs are running
//  This is the FD_WAIT port problem where a port is in a FD_WAIT state
//  but you can bind to it.  You get a port reuse error.
  BoundPortMin:=721;
  BoundPortMax:=731;

end;


procedure TIdLPR.Print(AText: String);
var ds: TMemoryStream;
begin
  ds:=TMemoryStream.Create;
  if Length(AText) > 0 then
  begin
    ds.Write(AText[1], Length(AText));
  end;
  InternalPrint(ds);
  FreeAndNil(ds);
end;

procedure TIdLPR.Print(ABuffer: array of Byte);
var ds: TMemoryStream;
begin
  ds:=TMemoryStream.Create;
  ds.Write(ABuffer[0], Length(ABuffer));
  InternalPrint(ds);
  FreeAndNil(ds);
end;

procedure TIdLPR.PrintFile(AFileName: String);
var fs: TFileStream;
  p: Integer;
begin
  p := RPos(GPathDelim, AFileName);
  ControlFile.JobName:=Copy(AFileName, p+1, Length(AFileName)-p);
  fs:=TFileStream.Create(AFileName, fmOpenRead);
  InternalPrint(fs);
  FreeAndNil(fs);
end;


function TIdLPR.GetJobId: String;
begin
  Result:=Format('%.3d', [FJobId]);    {Do not Localize}
end;

procedure TIdLPR.SetJobId(JobId: String);
begin
  if StrToInt(JobId) < 999 then
    FJobId:=StrToInt(JobId);
end;

procedure TIdLPR.InternalPrint(Data: TStream);

begin
  try
    if Connected then
    begin
      Inc(FJobID);
      if FJobID > 999 then
      begin
        FJobID:=1;
      end;
      DoOnLPRStatus(psPrinting, JobID);
      try
       	ControlFile.HostName:=Self.IOHandler.LocalName;
      except
       	ControlFile.HostName:='localhost';    {Do not Localize}
      end;

      // Receive a printer job
      Write(#02 + Queue + LF);
      CheckReply;
      // Receive control file
      Write(#02 + IntToStr(Length(GetControlData)) +
        ' cfA' + JobId + ControlFile.HostName + LF);    {Do not Localize}
      CheckReply;
      // Send control file
      Write(GetControlData);
      Write(#0);
      CheckReply;
      // Send data file
      Write(#03 + IntToStr(Data.Size) +	' dfA'  + JobId +    {Do not Localize}
        ControlFile.HostName + LF);
      CheckReply;
      // Send data

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -