Garbage Burrito

Printing Pestulant Popups

Printing Pestulant Popups
Ben Kittrell - 12 20, 2006 @ 10:44AM
Comments: 2
I ran into a weird one today and thought I would share it, so that no other poor sole would have to waste a day on such a stupid thing.  

It takes some setup so that you wont just think I'm an idiot.  I needed a printable view of some data, which seems easy enough however we're using Websphere Portal 5 at my day job.  This monstrosity is the bane of my current existence for many reasons, one of which is that I cannot find a way to return presentation data without the portal decoration.  I just need some plain HTML here people.  This makes it pretty much impossible to do any sort of Ajax, but also makes it very hard to generate printable views.  

I've tried to think of ways around this.  I started creating a second app that could run outside of portal, but we rely heavily on the session and the security is a nichtmare.  I finally came up with the solution of rendering the print view into a javascript string, opening a popup window and writing the string to the popup.  Cringe away. I know this probably hurts your parts but it was the only way.  It actually works quite well.

So anyway, back to the recent problem I ran into.  The users were complaining about the number of clicks it takes to print something, so we agreed to automatically show the print dialog.  Again, seems easy, but not so fast Billy Ray.  My first attempt looked something like this.

function printCase() {
  var print_win = window.open('', 'print');
  print_win.focus();
  print_win.document.write('<html><body>hello</body></html>');
  print_win.print();
}

Works great in Firefox, but guess where it doesn't work.  Apparently IE doesn't want you to call the print dialog on a popup window, after or as data is written to it.  My second try looked like this.

function printCase() {
  var print_win = window.open('', 'print');
  print_win.focus();
  print_win.print();
  setTimeout(function() { print_win.document.write('<html><body>hello</body></html>'); }, 100);
}

By delaying the data for a tenth of a second it gave IE enough time to show the print dialog, I guess.  The only problem is that when I print it I get nothing.  I suppose that when the print dialog is opened it buffers the data, and since there was no data until a tenth of a second later, it buffers nothing.

At this point I turned to google and found this page.  In the self-printing pages section I saw a block of code that looked like some ActiveX stuff.  I yanked that out and viola, it worked!

function printCase() {
  var print_win = window.open('', 'print');
  print_win.focus();
  print_win.document.write('<html><body>hello </body></html>');

  var agt = navigator.userAgent.toLowerCase();
  var is_ie = (agt.indexOf('msie') != -1);

  if (is_ie) {
    print_win.document.body.insertAdjacentHTML("beforeEnd",
    "<OBJECT ID=WebBrowser1 WIDTH=0 HEIGHT=0 CLASSID=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2><\/OBJECT>");
    print_win.WebBrowser1.ExecWB(6, 2);
    print_win.WebBrowser1.outerHTML = "";
  } else {
      print_win.print();
  }
}

I have no idea what it's actually doing and don't really care.  ActiveX voodoo is a mystery to me and I suspect even to people that use it.

So my guess is that not many people will ever have this same problem, cause no one else is stupid enough to come up with such nonsense.  But for those that do, may you find safe passage to this blog post.

Comments: 2

Comments

1. santhosh - 04 20, 2007 @ 12:41AM

The code is working well in both ie and mozilla. thanks for this.

2. william - 03 29, 2008 @ 01:55PM

por favor me dices como pongo a trabajar esto.

Post a Comment




powered by : Doodlekit Online Free Website Builder : developed by : Doodlebit™ Website Company