HTML Printing

Hello Experts,


How can I print HTML code from Delphi application?

The option of converting the html to bitmap is not acceptable, I want to be able to print the HTML the same way Microsoft IE printing the document.
S_WarriorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cmp3kCommented:
Hello,

Here is my solution:

uses ComObj;

procedure PrintHTMLByIE(const url: string);
const
  OLECMDID_PRINT = $00000006;
  OLECMDEXECOPT_DONTPROMPTUSER = $00000002;
var
  ie: Variant;
begin
  ie := CreateOleObject('InternetExplorer.Application');
  ie.Visible := False;
  ie.Navigate(url);
  while ie.Busy do Sleep(100);
  ie.Left := -999;
  ie.Top := -999;
  ie.Height := 1;
  ie.Width := 1;
  Sleep(1000);
  ie.Visible := True;
  ie.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER);
  Sleep(5000);
  ie.quit;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  PrintHTMLByIE('http://www.experts-exchange.com');
  // if you will print local file, use PrintHTMLByIE('file:\\C:\yourfile.htm');
end;
0
S_WarriorAuthor Commented:
What if I will need to manuplate the image before printing?

How can I select a printer?
0
cmp3kCommented:
If you need to change HTML and if it is a local file, you can load it into memo or stringlist, make modifications and save.

Here is the updated code that will print using your provided printer:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComObj, StdCtrls, printers;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

function GetDefaultPrinterA(prnName:PAnsiChar; var bufSize:DWORD):
BOOL; stdcall; external 'winspool.drv' name 'GetDefaultPrinterA';

function SetDefaultPrinter(prnName:PAnsiChar):
BOOL; stdcall; external 'winspool.drv' name 'SetDefaultPrinterA';

implementation

{$R *.dfm}

function getDefaultPrinterName:string;
var
  lprnName: PAnsiChar;
  lBufSize: DWord;
begin
  GetMem(lprnName, MAX_PATH);
  try
     lBufSize := MAX_PATH;
     GetDefaultPrinterA(lprnName, lBufSize);
     Result := lprnName;
  finally
     FreeMem(lprnName);
  end;
end;

procedure PrintHTMLByIE(printername, url: string);
const
  OLECMDID_PRINT = $00000006;
  OLECMDEXECOPT_DONTPROMPTUSER = $00000002;
var
  ie: Variant;
  olddefprinter: String;
begin
  olddefprinter := getDefaultPrinterName();
  if olddefprinter <> printername then SetDefaultPrinter(PChar(printername));
  ie := CreateOleObject('InternetExplorer.Application');
  ie.Visible := False;
  ie.Navigate(url);
  while ie.Busy do Sleep(100);
  ie.Left := -999;
  ie.Top := -999;
  ie.Height := 1;
  ie.Width := 1;
  Sleep(1000);
  ie.Visible := True;
  ie.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER);
  Sleep(5000);
  ie.quit;
  if olddefprinter <> printername then SetDefaultPrinter(pchar(olddefprinter));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  PrintHTMLByIE('PRINTERNAME','http://www.experts-exchange.com'); // REPLACE PRINTERNAME WITH DESIRED PRINTER NAME
// if you will print local file, use PrintHTMLByIE('PRINTERNAME','file:\\C:\yourfile.htm');
end;

end.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nhojCommented:
Take a look at

http://pbear.com/

There is an HTML component for Delphi that handles HTML display and printing. It is easy to use has print preview and, for me the best of all , handles tables properly unlike IE, that is it does not cut tables off in the middle of a row when it reaches the end of a page. There is a free "Lite" version but my comapny have been using the the full version for years and it has saved us a lot of work. It is also very reasonably priced.

Regards

John
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.