• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

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.
0
S_Warrior
Asked:
S_Warrior
  • 2
1 Solution
 
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
 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now