DelphiFAQ Home Search:

Print a HTML web page from my application


commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.


I need to print a web page or a local HTML file from my application, but the TWebBrowser class has no print method. How can I print my document then?


Looking into MSHTML.pas, you'll notice that IHTMLWindow3 has a print method. The question is how to a IHTMLWindow3 interface von TWebBrowser.

The solution is to use a regular TWebBrowser component, use Navigate() to load the desired document and put the code for printing in the OnNavigateComplete event.

procedure TForm1.WebBrowser_V1NavigateComplete(Sender: TObject; 
  const pDisp: IDispatch; var URL: OleVariant);
  HTMLDoc: IHTMLDocument2;
  HTMLWnd: IHTMLWindow2;
  HTMLWindow3: IHTMLWindow3;
  HTMLDoc := (Sender as TWebBrowser).Document as IHTMLDocument2;
  if HTMLDoc = nil then
    raise Exception.Create('Could not convert the WebBrowser to an IHTMLDocument2');
  HTMLWnd := HTMLDoc.parentWindow;
  HTMLWindow3 := HTMLWnd as IHTMLWindow3;
  // finally, we get to the print method

// ..
You don't like the formatting? Check out SourceCoder then!
Content-type: text/html


2012-11-12, 03:06:51
coach001 from United Kingdom  
2012-11-12, 05:06:54
anonymous from United States  



NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: