Skip to main content

Printing html using the embedded web browser control


In this post I’ll try to answer some questions about the web browser control and provide some workarounds for known issues involved in the printing process.

I'm assuming that you have some experience with the web browser control and basic knowledge of COM and hosting APIs. So I’m not going to cover those topics.

At the bottom of this page I’ve added the links to download a small library I wrote that takes care of printing HTML and a demo app so you can try it out without having to write any code by yourself.

Using the code
The HtmlPrinter class will allow you to print html from an URL or just passing the html as string, you can also specify the title and the number of copies you want to print. The code may look something like this:
Now that we know how to use the API let get answer some questions.

Why my app crashes when I try to print multiple copies of a page?
Well, apparently when you send a lot of print commands to the web browser control, there is a lot of COM crap in between that leave the control into nonresponsive state and after a couple of calls, your app will hang and eventually it will die.
I ran into this issue a couple of days ago and I can solve it adding this code inside the print method:

While the COM API is busy, we just keep on pumping messages to the OS loop, avoiding app hungups, when the API es ready, we proceed with the printing. So far, always work ;)


Why my images don't print or how can I print them?
The only thing you should take care of when it comes to print images is specify the image URL in a way that is “reachable” by the web browser control. The code down below ain’t gonna work.






whereas this should work like a charm:

It’s possible to print without showing the print preview dialog?
Yes it is possible. Just download the sample app and run it. All the samples presented in this post print html without showing the print preview dialog.


How can I change the page orientation?
The answer is you cannot. It is possible to change the default page orientation modifying this key on the registry:

But I recommend not to that. (and sometimes it doesn't work either).

* I also tried adding this header to the html without any luck




So I think that’s all for those issues I've heard of. If you have any question about printing HTML, feel free to ping me, I'll try to answer it.









Comments

  1. Amiralles,

    Nice work. Like your code, how can we programmatically print to a printer of our choice (instead of the default printer) without displaying the Printer dialog box?

    Thanks,
    Nam

    ReplyDelete
  2. Thanks Nam!
    As far as I know that is not possible. A fellow developer at work did something to overcome this limitation forcing the default printer using interopcalls and then restore it to the original value.
    This is a risky endeavor, though, and of course will only work in a very limited set of scenarios, where you can be sure that there is no apps relying on the default printer (which is almost never) and things like that. So I encourage not to do so.
    Another option (not related to the webbroser control) is to use a external program capable to batch printing html files. But I have no experiencie whit them, so I can't recommend any.

    ReplyDelete
  3. When I try to print html, some data gets lost in printout. The html page shows.proper data, but the printout does not; especially with the page breaks. What might be causing problem? Tried on numerous printers, some work, some don't!

    ReplyDelete
    Replies
    1. Hi Vijayalaxmi, not sure if the problem is the printer or the IE version/html tag that you are using to make pagebreaks.
      You have to have in mind that the web brower control it is a wrapper around IE6 (you can hack it to work IE7 tops), css support on those versions of IE is lousy. I think the problem may be there... For instance, if I don't miss remember it, the only way to make reliable pagebreaks on early versions of IE was using tables tags (not divs or customs tags as we use nowadays).
      Hope this help!

      Delete
  4. When I try to send HTML string to non default printer, I am getting blank printout as output. could you please help me out..
    You can replicate this even in the attached demo.

    ReplyDelete
    Replies
    1. Hi Kishore, as far as I know, when you are printing using the embeded web browser dialog, you cannot print to a non default printer without showing the print preview dialog (Which is what the code in the example does). I think the problem may be there...
      Nam, experienced a similar issue.(see the comments above)

      Another option (not related to the webbroser control) is to use a external program capable to batch printing html files, but I haven't used any.. sorry.

      Delete
  5. The link to the demo app and source code is asking for a login/pwd. Kindly fix it ASAP.

    ReplyDelete
  6. Its working fine when we run at visual studio but when we made the windows service installer and after installing the windows service it doesn't work

    ReplyDelete
    Replies
    1. I don't think you can use this code as is in a Windows Service. Mostly, because by default, windows services are not allowed to talk to desktop components (such as forms, controls, etc...) You can however check the "Allow to interact with desktop option" (or something like that) and see if it works, but I guess you will end up with some permission issues (in general, services don't run in user mode, which has its own set of problems).
      What you can try, though, is to convert the app into a tray icon app. The behavior will be similar to a service, but you will be running in "user mode".

      Delete
    2. I saw after enabling the windows service interaction with desktop
      by going to services Logon and by checking the checkbox for Allow service interaction with desktop then i restart the service to effect the changes..
      but still not working ... so is there any effective way to solve this problem .. Please let me know if yes ASAP

      thank for code its really nice dear but not working for me
      Again Many thanks

      Delete
  7. I saw after enabling the windows service interaction with desktop
    by going to services Logon and by checking the checkbox for Allow service interaction with desktop then i restart the service to effect the changes..
    but still not working ... so is there any effective way to solve this problem .. Please let me know if yes ASAP

    thank for code its really nice dear but not working for me
    Again Many thanks

    ReplyDelete
  8. It's a complicated topic but it's explained nicely.

    ReplyDelete
  9. Interesting and helpful article. Thanks for posting this.

    ReplyDelete

Post a Comment

Popular posts from this blog

How to create MS Word documents from Office templates using C#

The OpenXML SDK allows you to do pretty much anything you want with office files such as Excel, Word, etc… While many people like this library, I found it complex, unintuitive and poorly documented, not to mention the awful xml format that uses under the hood to represent the documents, styles, etc. So I decided not to use it and build my own solution. If you, like me, don’t like that library, you will find in this post an alternative approach to build word documents from templates using c#.
A neat trick to work with Office is to use the macro recorder to understand how things work. The macro recorder allows you to start a macro, do something by hand, stop it, and then take a look at the generated VBA code. Once you do this, you are pretty much set.
This is how it looks the template I’am going to use.


Note: save the file as a Word template (.dotx)
This is the code to create Word documents from C#:

By running the code, you should get a document that looks like this.


Note that the font, forma…

WinForms, paging the DataGridView the right way

I know this may sound like old history, but in the enterprise world there is still a lot of WinForms development. Just a couple of days ago, I had to implement a custom DataGridView capable to work over a butt load of data (100K+ records) and keep responses times acceptables.
I thought paging will be a good way to go, and as WinForms is pretty old nowadays, I supposed it will be easy to find a couple examples on the web.
While in fact I found examples, all of them were incompletes and/or they wouldn't perform well in real world apps... So I decided to roll my own component and post it online. Hopefully, someone else will find it useful ;).


The bread and butter of this solution relies on LINQ and deferred execution. As LINQ takes care of all complicated work, it was quite easy to implement.
This component also supports conditional format, sorting and some search capabilities, but in this post I will concentrate on paging only (I'll cover the rest of the features in future posts)…