Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

Print HTML file

Has anyone managed to print HTML files without using a webbrowser? I have multiple files that will be combined to form 1 report when printed. And since the report's files may or may not be opened in the browser, I need the code to work off of the file itself instead of what is displayed. I can print a file manually by using the PrintDocument's PrintPage event, but that prints tags and all instead of the resulting HTML. The only idea I can come up with is manually formatting and parsing the file, but as massive as the .NET framework is, surely that isn't necessary.

Thanks for any input,

Chris Guest
Wednesday, October 09, 2002

Um, but tags and all IS the resulting HTML. What you want to print is not your HTML, but a rendering of the HTML. And of course the rules for the rendering aren't fixed.

Your best (and, unfortunately, tedious) bet may be to use XSLT - there's good support in the System.Xml.Xsl namespace. Whether that's workable may depend on how well-formed and predictable your document is.

Mike Gunderloy
Wednesday, October 09, 2002

Thanks for the reply. What is the concept behind the XSLT approach? I haven't dealt with any XSLT, but have done quite a bit of of work with plain vanilla XML files. I don't necessarily need any code, but an overview of what kind of approach to take would be great.

After all this, it seems that a simple static method in the File class would be perfect. (if it were there.) Something like:

File.Print(string path);

It seems like the OS itself would use a similar approach to printing anyway.

Thanks again,

Chris Guest
Wednesday, October 09, 2002

i would suggest using the IE WebBrowser Control to render & then print the file, though i'm not sure about the validity of this approach in light of .NET.  there might be something newer & better...  MSDN has some info about this:

Wednesday, October 09, 2002

Hi Chris,

I think using the WebBrowser control is viable approach. 

Other alternatives include:

1.    Using MSHTML and the IHTMLDocument2 interface.
2.    Using MSHTML from the command line (e.g. rundll32.exe %SystemRoot%\System32\mshtml.dll,PrintHTML "%1") %1 will be the path to your HTML file.

There may be issues with the second approach, as multiple simultaneous print commands to MSHTML don’t scale well.  It also prompts you with a print dialog, which you may not want.  I’m not sure if the print dialog can be suppressed.  You might also want to check out Print Templates.  A sample is provided here  “”

I hope this helps, and good luck.



Garett Chang
Wednesday, October 09, 2002

Convert to PDF.




Thursday, October 10, 2002

*  Recent Topics

*  Fog Creek Home