Printing HTML Files Without Header/Footer Lines Using Microsoft Internet Explorer

Environment: VC++ 7.0

Sometimes you have to print HTML files from your Visual C++ program. If Microsoft's Internet Explorer is installed, this can be done easily by just calling ShellExecute(hwnd, "print", "my_file.html", 0, 0, SW_SHOW).

Unfortunately, this way you don't have any control over the header/footer lines that the Web browser prints.

Knowledge base article Q236777 (HOWTO: Change Print Settings for Internet Explorer and WebBrowser Control Programmatically) shows some "undocumented" ways of accessing header/footer values using C++ code. Based on this article, "print_html" has been implemented. By using this module, it is possible to print HTML pages without any header/footer lines (provided that Internet Explorer is installed as the default Internet browser).

Just say #include "print_html.h" at the beginning of your cpp file and add "print_html.cpp" to your workspace. Now it is possible to print HTML files with the following lines of code:

C_PrintHtml ph;
ph.PrintHtml("my_file.html");

You can find "print_html.cpp" and "print_html.h" (as well as a simple example of how to use the module) in the attached zip archive. I used VC++ 7.0 to create the example. The module has been tested with Internet Explorer 6 on Windows NT4 SP6a, Windows 2000 SP2, and Windows XP Professional.

Best regards,
Tilman Kuepper (kuepper@xgraphic.de)

Downloads

Download source - 28 Kb


Comments

  • Printing to network printer

    Posted by Legacy on 12/11/2003 12:00am

    Originally posted by: waqar

    Is there a way to print the html files to the network printer configured for the system.

    Reply
  • Another solution for user dialog during priting

    Posted by Legacy on 11/18/2003 12:00am

    Originally posted by: Yaron Shkop

    The default printto verb for html files on windows is ofcourse IE.
    IE6 however invoke a pop up dialog for the user during printto opperation. It's nice as long as the application is not a server one.. (no UI).
    It took me a while to understand that this issue is a browser implementation and I was convinsed so after reading many posts about this in google discussion groups searches.
    Most of the people by the way encountered the problem while developing FAX servers using MS FAX services (COM).
    The solution is ofcourse to use other browser.
    Netscape� Communicator 4.79 is the one that does the job silently as expected (NOT netscape 7.02...), it has to be set as the default browser on the machine.
    If you don't want it to be the default browser set the Registry- html files printto only: (HKLM\Software\Classes\htmlfile\shell\printto\) to the values that netscape set them to when it's the default browser. (That way only the printto operation is done through netscape).
    The other solution is to use MS-Word as I've already described in my previous post.

    Reply
  • User Dialogs Solution

    Posted by Legacy on 11/16/2003 12:00am

    Originally posted by: Yaron Shkop

    Sorry, but your solution doesn't solve the problem of print dialogs.
    The idea was to prevent user dialogs from appearing for server based applications. (No UI!)
    The solution is to edit the printto verb inside "Assaociated File Types" to a program that can do it without dialogs.
    This is not a perfect solution since the program is MS-WORD.
    If you have another software for coverting\printing html without dialogs please add a comment. (A freeware or a built in program in Windows!)

    Action:
    printto

    Application used to ... :
    "C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE" /n /dde %1 %2

    DDE Message:
    [REM _DDE_Minimize][FileOpen("%1")][FilePrintSetup "%2 on p",.DoNotSetAsSysDefault=1][FilePrint 0][DocClose 2][FilePrintSetup ""]

    Application:
    WinWord

    DDE Application not running:
    [FileOpen("%1")][FilePrintSetup "%2 on p",.DoNotSetAsSysDefault=1][FilePrint 0][FileExit 2]

    Topic:
    System

    Reply
  • Batch Print - How can i send a html file to the printer without any print dialog

    Posted by Legacy on 07/07/2003 12:00am

    Originally posted by: simon soosai

    I am trying to send more than 1 files straight to the printer from my html page. How would i do that?

    Reply
  • This method does not work with IE 6.0

    Posted by Legacy on 05/30/2003 12:00am

    Originally posted by: Kyselgov E.N.

    This method does not work with IE 6.0.
    
    This method based on registry IE settings changing.
    It modify header and footer registry settings, run IE, wait for IE exit, and restore settings back.
    !!! BUT !!! IE 6.0 (and maybe IE 5.5) prints in background (hidden) process. When you run IE 6.0 process for printing HTML page and if you will think that IE 6.0 process completion is signified printing completion than you will MISTAKEN.
    Where is the error?
    Look at process.

    1. Our program change settings.
    2. Our program runs IE 6.0 main process for printing.
    3. IE 6.0 main process runs hidden printing process.
    4. IE 6.0 main process exits.
    5. IE 6.0 hidden process shows print dialog (waits OK).
    6. Our program waits until IE 6.0 main process exits. <-- This is ERROR!
    7. Our program restores settings.
    8. IE 6.0 hidden process read settings (it's restored (not our) settings).
    9. IE 6.0 hidden process prints.
    10. IE 6.0 hidden process exits.

    In 6. our program must wait IE 6.0 hidden process completion but not main process completion!
    !!! BUT !!! There is not way for taking IE 6.0 hidden process handle.

    Reply
  • Resource leak (handle must be closed)

    Posted by Legacy on 05/29/2003 12:00am

    Originally posted by: Kyselgov E.N.

    Function C_PrintHtml::ExecIE() not close process handle - this is dangerous resource leak.
    
    Add this code after WaitForSingleObject():
    CloseHandle(sei.hProcess);

    Reply
  • BUG...

    Posted by Legacy on 04/19/2003 12:00am

    Originally posted by: Xig

    There is a bug in the code. you need to add this to print_html.cpp:
    
    

    C_PrintHtml::~C_PrintHtml()
    {
    WaitForSingleObject(printingResource, INFINITE);
    }

    otherwise, if you close your application while a doc is printing (or in print-setup for example) the app may crash and/or corrupt the registry!

    Zig

    Reply
  • Thank god for your soul.

    Posted by Legacy on 04/18/2003 12:00am

    Originally posted by: Fantom Code

    Wow,
    
    

    I've been trying to figure this out. Thanks so much for this useful bit of code. You're on the top of my favorite people list as of now. HAH!

    Thanks again,
    Fantom

    Reply
  • What about rights?

    Posted by Legacy on 02/04/2003 12:00am

    Originally posted by: Piotr

    I think this solution is good only for Administrator.
    What about writing to Registry by user on NT/2000/XP?

    Reply
  • Multitasking problem

    Posted by Legacy on 12/11/2002 12:00am

    Originally posted by: Erik Rydgren

    If you have more then one instance of this class printing at the same time then you will probably loose the original settings for header and footer.

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds