Board index » delphi » How to print Postscript/HPGL?

How to print Postscript/HPGL?

Hi all,

How do I use Delphi's Printers unit to print postscript (or HPGL) files?

I'm currently creating a batch file which uses the WinNT print command.
This is far from ideal - it's ugly, and the user can't select the
printer or see the document in the print queue.

I've tried using AssignPrn and copying the Postscript code line by line,
but of course all that did is print out pages of Postscript code!

Help!
--
Jeremy Collins

 

Re:How to print Postscript/HPGL?


Quote
Jeremy Collins wrote:
> Hi all,

> How do I use Delphi's Printers unit to print postscript (or HPGL) files?

> I'm currently creating a batch file which uses the WinNT print command.
> This is far from ideal - it's ugly, and the user can't select the
> printer or see the document in the print queue.

> I've tried using AssignPrn and copying the Postscript code line by line,
> but of course all that did is print out pages of Postscript code!

> Help!
> --
> Jeremy Collins

Hi Jeremy,

I asked about the same question. I got the answer fomr the newsgroup:
borland.public.delphi.objectpascal, a newsgroup worth browsing.

I found a question like printing directly to lpt1. That was it!

The following lines will show you how it's done.
The original code was a procedure. I changed to a function that's all.

function TFormular1.PrintDirect(TmpPath : String;
                                  AIndex  : ShortInt): Boolean;

const
  BufSize = 16384;

type
  TDoc_Info_1 = record
                  pDocName    : pchar;
                  poutputfile : pchar;
                  pDataType   : pchar;
                end;

var
  Count        : Integer;
  BytesWritten : Integer;
  MyPrinter   : TPrinter;
  hPrinter     : THandle;
  Device       : Array[0..255] of char;
  Driver       : Array[0..255] of char;
  Port         : Array[0..255] of char;
  hDeviceMode  : THandle;
  DocInfo      : TDoc_Info_1;
  f            : File;
  Buffer       : Pointer;
  fl_quit      : Boolean;

begin

  MyPrinter := TPrinter.Create;

  PrintDirect := False;

  fl_quit := False;

  MyPrinter.PrinterIndex := -1;   {Set printer to default}
  MyPrinter.GetPrinter(Device, Driver, Port, hDeviceMode);

  if WinSpool.OpenPrinter(@Device, hPrinter, nil) then
  begin

    DocInfo.pDocName := 'MyDoc';
    DocInfo.pOutPutFile := nil;
    DocInfo.pDataType := 'RAW';

    if (StartDocPrinter(hPrinter, 1, @DocInfo) = 0) then
    begin

      WinSpool.ClosePrinter(hPrinter);
      fl_quit := True;

    end;

    if (not StartPagePrinter(hPrinter)) and (not fl_quit) then
    begin

      EndDocPrinter(hPrinter);
      Winspool.ClosePrinter(hPrinter);
      fl_quit := True;

    end;

    if not fl_quit then
    begin

      AssignFile(f,TmpPath);
      Reset(f,1);
      GetMem(Buffer,BufSize);

      while not eof(f) do
      begin

        Blockread(f, Buffer^, BufSize, Count);

        if (Count > 0) then
        begin

          if not WritePrinter(hPrinter, Buffer, Count, BytesWritten) then
            EndPagePrinter(hPrinter);

        end;

      end;

      CloseFile(f);
      FreeMem(Buffer, BufSize);
      EndDocPrinter(hPrinter);
      WinSpool.ClosePrinter(hPrinter);
      PrintDirect := True;

    end;

  end;

 MyPrinter.Free;

end;

If you get into troubles if your selected printer is a PostScriptprinter or
not then
use the following code too.

function IsPostScriptPrinter(dc : hdc) : bool;
var
  TestInt : integer;
  a : array[0..255] of char;
  err : integer;
  s : string;
begin
  Result := false;
  TestInt := GETTECHNOLOGY;
 {$IFDEF WIN32}
  if ExtEscape(Dc,
               QUERYESCSUPPORT,
               sizeof(TestInt),
               @TestInt,
               0,
               Nil) > 0 then begin
     Err := ExtEscape(Dc,
                      GETTECHNOLOGY,
                      0,
                      nil,
                      256,
                      @a);
 {$ELSE}
  if Escape(Dc,
            QUERYESCSUPPORT,
            sizeof(TestInt),
            @TestInt,
            Nil) > 0 then begin
     Err := Escape(Dc,
                   GetTechnology,
                   0,
                   nil,
                   @a);
  {$ENDIF}
     if Err > 0 then begin
       s:= UpperCase(StrPas(a));
       if Pos('POSTSCRIPT',s) > 0  then
         result := true;
     end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if PrintDialog1.Execute then
    if IsPostScriptPrinter(Printer.Handle) then
      ShowMessage('This is a Postscript Printer') else
      ShowMessage('This is not a Postscript Printer');
end;

All this codelines were written by Joe C. Hecht (Borland)

Joe, thank you a lot, you're great help

So Jeremy I hope you will able to solve you problem that way

Good luck and keep stiff

Dieter

email: dieter.zimmerm...@tamedia.ch

Other Threads