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
  10. Looks interesting but your download link is not working with svn browser or with downloading from the web

    ReplyDelete
    Replies
    1. You are right. Thanks for pointing that out. I don't host projects on xp-dev anymore, maybe they remove the repos from their website. I'll try to find the code and posted to github (and update this post, of course.)

      Delete
    2. Hi, I also would like to work through this sample. Have you managed to post to Github?

      Delete

Post a Comment

Popular posts from this blog

Migrating an ASP.NET MVC 4 app from Azure websites to WinHost

About a week ago I've to migrate an ASP.NET MVC 4/EF5 application from Azure websites to WinHost. While the process was really smooth, there were some caveats related to database connections that I want to share with you. Create and setup the ftp profile on VS and configure the connection string was really easy, WinHost provide you those values and there is nothing special here. But once you deploy your website and try to see it online, you may get the “yellow screen of dead” with the message: "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)" Assuming you wrote the connection string properly, this happens because you cannot use the default connection name in your web.c

How to show excel files inside the .NET Webbrowser Control

If you are reading this, chances are you been banging your head against the wall for a couple of hours (or even days) trying to show excel files inside the WinForms webbrowser control. Possible reasons you ended up in here: You had working code that got broke after upgrading from Win 7. Your code doesn’t work the same way between machines running different (newer) versions of IE. A download box pops up every time your app tries to show an excel file inside the webbrowser control (you wanna show the actual content). You just have no clue on how to get excel working into the .NET embedded webbrowser control. You are trying to implement IInternetSecurityManager and don’t know where to start. (Or how don’t know how to delegate calls to your security manager). Among many other, maybe….. Yes, COM is a PITA, so is ActiveX and IE (Embedded or full for that matter). And no, showing excel files inside the webbrowser control shouldn’t be that hard, but sometimes we have

Moving to Medium

It's been a long time since I want to give medium a try, and finally, I made some time to do it. To get started on the new platform, I'll be doing series on "Getting programming concepts, languages and tools". If it sounds interesting to you, please take a look at the first post  Getting AWK  and spread the word if you like it. I'm not going to migrate old entries to the new web site. They will remain here safe and sound! As usual, thanks for reading!