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.
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.
Amiralles,
ReplyDeleteNice 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
Thanks Nam!
ReplyDeleteAs 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.
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!
ReplyDeleteHi Vijayalaxmi, not sure if the problem is the printer or the IE version/html tag that you are using to make pagebreaks.
DeleteYou 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!
When I try to send HTML string to non default printer, I am getting blank printout as output. could you please help me out..
ReplyDeleteYou can replicate this even in the attached demo.
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...
DeleteNam, 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.
The link to the demo app and source code is asking for a login/pwd. Kindly fix it ASAP.
ReplyDeletefixed.
DeleteIts 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
ReplyDeleteI 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).
DeleteWhat 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".
I saw after enabling the windows service interaction with desktop
Deleteby 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
I saw after enabling the windows service interaction with desktop
ReplyDeleteby 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
Very helpful blog
ReplyDeleteHelpful indeed!
ReplyDeleteIt's a complicated topic but it's explained nicely.
ReplyDeleteInteresting and helpful article. Thanks for posting this.
ReplyDeleteKeep on posting
ReplyDeleteI like this new knowledge to me.
ReplyDeleteLooks interesting but your download link is not working with svn browser or with downloading from the web
ReplyDeleteYou 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.)
DeleteHi, I also would like to work through this sample. Have you managed to post to Github?
Delete