🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.pascal.delphi.misc
Article #296

Re: Printing PCL Format direct from Delphi

#296
From: John P
Date: Tue, 13 Sep 2005 08:39
80 lines
3672 bytes
venkateswarag@gmail.com wrote:
> 
> I need to send the PCL format file to printer directly for selected
> printer from the dropdown list.

 
If you could write that dropdown list your self. And if you can deal
with the the thing that the printers here are indentified by their
PrinterIndex number, then this snippet should do the job.


uses
  .. Printers, WinSpool;

procedure DumpAnyFileToPrinter(aFileName: string; aPrinterIndex:
Integer);
var
  BytesWritten: DWORD;
  aHandle: THandle;
  DocInfo: TDocInfo1;
  PrinterName: PChar;
  MSt: TMemoryStream;
  aErrCode: Integer;
  aFailed: Boolean;
begin
  if not FileExists(aFileName) then
    raise exception.create(Format('File not found "%S"', [aFileName]));
  try
    PrinterName := PChar(Printer.Printers[aPrinterIndex]);
  except
    ShowMessage(Format('Index [%d] outside current printer count (%d).',
      [aPrinterIndex, Printer.Printers.Count]));
    Exit;
  end;
  if not WinSpool.OpenPrinter(PrinterName, aHandle, nil) then
    raise exception.create('Printer not found');
  MSt := TMemoryStream.Create;
  try
    MSt.LoadFromFile(aFileName);
    {Name pDocName will be shown on the Print Task list.}
    DocInfo.pDocName := PChar('DUMP: ' + aFileName);
    DocInfo.pOutputFile := nil;
    DocInfo.pDatatype := 'RAW';
    if StartDocPrinter(aHandle, 1, @DocInfo) <> 0 then
    try
      if StartPagePrinter(aHandle) then
        WritePrinter(aHandle, MSt.Memory, MSt.Size, BytesWritten);
    finally
      EndPagePrinter(aHandle);
    end;
    EndDocPrinter(aHandle);
  finally
    aErrCode := GetLastError;
    WinSpool.ClosePrinter(aHandle);
    if aErrCode = 0 // 0 = Last operation Succeed.
      then MessageBox(0, PChar(Format('Success! %d bytes spooled',
        [BytesWritten])), PrinterName, mb_OK + mb_iconinformation)
    else MessageBox(0, PChar(SysErrorMessage(aErrCode)),
        'Printing Error', mb_OK + mb_iconexclamation);
    MSt.Free;
    ClosePrinter(aHandle);
  end;
end;


procedure TForm1.btnRaw2Click(Sender: TObject);
begin
  // TEST.PCL is a file created by printing through a LaserJet
  // PCL-driver, with 'Print To File' option.
  // Dump this file to the current active printer.
  DumpAnyFileToPrinter('C:\TEST.PCL', Printer.PrinterIndex);
end;

There may be even too much built in checkings, and too many hard coded
error messages for this kind of task. 

And the code is not perfect either, but it has served me through the
years, for my occasional Dump needs.

John P
Technical Details
Message-ID: <4326902F.9E96A9D@spam.mimidi.se>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!feeder1.news.jippii.net!reader1.news.jippii.net!53ab2750!not-for-mail
References: <1126411672.276817.12660@f14g2000cwb.googlegroups.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10baee,eaf7bf8998d29cfc
X-Google-Thread: 1043cf,eaf7bf8998d29cfc
X-Google-Thread: 10baee,eaf7bf8998d29cfc
X-Google-Attributes: gid10baee,gid1043cf,gid10baee,public
X-Google-Language: ENGLISH,ASCII-7-bit
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!feeder1.news.jippii.net!reader1.news.jippii.net!53ab2750!not-for-mail
Message-ID: <4326902F.9E96A9D@spam.mimidi.se>
From: John P <john.p@spam.mimidi.se>
Organization: -
X-Mailer: Mozilla 4.76 [en] (Windows NT 5.0; U)
X-Accept-Language: fi,en
MIME-Version: 1.0
Newsgroups: comp.lang.pascal.delphi.misc,borland.public.delphi.objectpascal,comp.lang.pascal.delphi.misc
Subject: Re: Printing PCL Format direct from Delphi
References: <1126411672.276817.12660@f14g2000cwb.googlegroups.com>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 80
Date: Tue, 13 Sep 2005 11:39:11 +0300
NNTP-Posting-Host: 213.192.169.59
X-Complaints-To: newsmaster@saunalahti.com
X-Trace: reader1.news.jippii.net 1126600888 213.192.169.59 (Tue, 13 Sep 2005 11:41:28 EEST)
NNTP-Posting-Date: Tue, 13 Sep 2005 11:41:28 EEST