Does anybody know how to print the contents of a HTML document without
having to open the browser. I don't want the HTML source just the document
as it appear in the browser.
At the moment what I do is send the page to the browser, open it and
hope
that the user will know how to print it.
Any help would be welcome...
--------
There used to be an HTMLView component on something like www.pbear.com
-
I think it had a printing capability.
--------
A less expensive idea is to send a sequence of keyboard
commands to browser
that imitate user input, like <Alt-F>
<P> <Enter>. I remember with some
difficulty a certain component that crossed my way whose name might
be SendKey.
It automated this kind of activity and was a clone of a
[built-in] capability
in VB.
--------
When ALL else fails.....
Try the code below. It has some flaws as mentioned within the code. But it works.
_________________________________________________
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
ShellAPI, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
function GetHTMLTitle(HTMLFile : String) : String;
procedure PrintHTMLFromExplorer(HTMLFile : String);
{Private declarations }
public
{Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
PrintHTMLFromExplorer('C:\HomeSiteGen.html');
end;
procedure TForm1.PrintHTMLFromExplorer(HTMLFile : String);
var
Hwnd : THandle;
begin
ShellExecute(Handle,'OPEN','C:\Program Files\Internet
Explorer\IExplore.exe',
PChar(HTMLFile),'C:\',SW_SHOW);
Sleep(4000);{You will need to change this}
{The Title of Explorer depends on the Title
within the HTML file
and not the File name of the HTML file.
So we need to get that
name. I'm not sure about Netscape.
}
Hwnd := FindWindow(nil,PChar(GetHTMLTitle(HTMLFile)));
{For the Keybd_Event to work Explorer MUST
be the ACTIVE Window. MINIMIZE
will NOT work either !!}
Keybd_Event(VK_MENU,0,0,0);
Keybd_Event(VK_MENU,0,KEYEVENTF_KEYUP,0);
Keybd_Event(VkKeyScan('F'),0,0,0);
Keybd_Event(VkKeyScan('F'),0,KEYEVENTF_KEYUP,0);
Keybd_Event(VkKeyScan('P'),0,0,0);
Keybd_Event(VkKeyScan('P'),0,KEYEVENTF_KEYUP,0);
Sleep(2000);{Change is required to give time to print}
PostMessage(Hwnd,WM_QUIT,0,0);
end;
function TForm1.GetHTMLTitle(HTMLFile : String) : String;
var
F : TextFile;
Str : String;
StartPos : Integer;
EndPos : Integer;
begin
{This function assumes that the <TITLE>
and </TITLE> tags are on the same line
needs to be modified !!
}
AssignFile(F,HTMLFile);
Reset(F);
While Not EOF(F) do
begin
Readln(F,Str);
if Pos('<TITLE>',UpperCase(Str))
<> 0 then
begin
StartPos := Pos('<TITLE>',UpperCase(Str));
if Pos('</TITLE>',UpperCase(Str)) <> 0 then
begin
EndPos := Pos('</TITLE>',UpperCase(Str));
Result := Copy(Str,StartPos +7,EndPos - (StartPos + 7)) +
' - Microsoft Internet Explorer';
Break;
end;
end;
end;
CloseFile(F);
end;