Can you print a page without opening it?

Is it possible to print a page without opening it? I was thinking in principal you could create a new window, write the JavaScript dynamically using write() and call the print() function. Is this possible?

Thanks.

Hello !

As far as i know, it is not possible.
When you ask for printing with Javascript, you call the print dialog box, so in any case, the visitor must validate that window.

May be you can dispose some scripts that wait for a blur of the window (when the dialog box opens), and when the focus returns, the window closes itself.
I don’t know if it works in any browser.

Hope this helps.
McBenny

What I do is have an iFrame on the page that has it’s location pointing to the page you want tp print. When you are ready, you first give it the focus by calling its focus() method, then print it using its print() method.

Couple of things to remember:

  1. You can set the iFrame’s location on the fly with Javascript

  2. You can make the iFrame 1px by 1px no matter how big the page is that it contains. It will still print the whole page. At 1px square, it looks like a period or dot. Use absolute positioning to park it somewhere it will not notice, like under another GIF.

If you need actual code, post a message and I’ll hunt some down.

I have the same requirement of printing a page without opening it. Would appreciate if you could post some sample code here…I am an newbie, so any help is appreciated.

Printing a page you haven’t opened is only possible on intranets running Internet Explorer where you can attach a different page to be printed in place of the one displayed.

The best you can do on the internet is to double up your page content with one version to display on the screen and the other to print.

Dear mathopenref,

I need to print a web page without showing the print dialog box. I understand that you know how to do it. Could you pleaes kindly send me the code?

Many thanks and kind regards

Sue2006

All browsers other than Internet Explorer will automatically open the print dialog so that the person can confirm where they want to print to. Only Internet Explorer running on an intranet can print directly to the printer and that is set up in the operating system on each individual computer rather than in the web page.

This hack certainly works fine with IE6 under WinXP. The idea here is to use JavaScript to call VBScript in a way that confuses the issue enough so that the dialog does not come up even if you have your security settings to force ActiveX to ask permission.

I hope it does not work in IE7 but I have not tested there yet.

Brett

function print() {
var WebBrowser = ‘<object id=“WebBrowser1” width=0 height=0 classid=“CLSID:8856F961-340A-11D0-A96B-00C04FD705A2”></object>’;

document.body.insertAdjacentHTML(‘beforeEnd’, WebBrowser);
execScript(“on error resume next: WebBrowser1.ExecWB 6, -1”, “VBScript”);
execScript(‘on error resume next: WebBrowser1.outerHTML = “”’, ‘VBScript’);
}

IE7 has ActiveX turned off by default (since that is the web’s most common cause of security holes). ActiveX was Microsoft’s biggest mistake and they are abandoning it as quickly as they can.

<<ActiveX was Microsoft’s biggest mistake and they are abandoning it as quickly as they can.>>

Microsoft would disagree. IE7, as previous versions, is neither more nor less than a collection of ActiveX controls. What they have done is change the user interaction behaviors of some of the controls.

As I said, I hope the hack does not work in IE7 but I will not know until I actually test it tomorrow in IE7 RC1. Even before release, IE7 seems way out-moded by Firefox 1.5.

Brett Merkey
http://web.tampabay.rr.com/bmerkey/

Here is how to have a print button which will print some other page from your site. NOTE: It will only run if it is on a web server (not C:) and the page to be printed is on the same server, same domain.

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<HTML>
<HEAD>

<script type=“text/javascript”>
var okToPrint=false;

function isIE()
{
return (navigator.appName.toUpperCase() == ‘MICROSOFT INTERNET EXPLORER’);
}

function doPrint()

{ window.printFrame.location.href=“http://www.mysite.com/somepage.html”;
okToPrint=true;
}

function printIt()
{if (okToPrint)
{ if ( isIE() )
{ document.printFrame.focus();
document.printFrame.print();
}
else
{ window.frames[‘printFrame’].focus();
window.frames[‘printFrame’].print();
}
}
}

</script>
</HEAD>

<body>
Press print button
<button onclick=“doPrint(); blur(this);”>Print</button>

<iframe width=“0” height=“0” name =“printFrame” id=“printFrame” onload=“printIt()”></iframe>

</BODY>
</HTML>

what it does:
1 An iframe with zero size holds the page you want to print. It will work on any size page.

2 When the button is pressed the iframe is loaded with the desired page. Can be any page the browser will render, PHP etc.

3 We wait. Once the page has been loaded the iframe’s onload handler fires and initiates the print.

4 The okToPrint flag is to prevent the iframe from initiating a print when the outer page is first loaded.

hi mathopenref,

thanks for posting your code!

it works fine on Mac/FF & Mac/Safari but Opera prints the parent page and (dare i go here) Mac/IE5.2 nothing happens.

I can live with Mac/IE not working because most things don’t work on that waste of harddrive space. any ideas about Opera, though?

also, if anyone has tested this could you post what browsers/OS it does and does not work on?

thanks
eric

Opera always gives the choice of which frame to print. You use the print preview option to select which frame or frames that you want to print out. The ones visible in the window at the time the print is selected are the ones that will be printed. So to get Opera to print a particular page all you need to do is to make that the only frame that is visible in the browser window and hide (or shrink to zero size) all the rest.

My guess is that the browser test need improving. I haven’t tried it but my hunch is that Opera wants to be treated like IE, but my code doesn’t do that.
It should probably be some thing like: if (IE or Opera) …
John

Couple of other thoughts:

On FF, even though the iframe is zero by zero, you still see a dot. You can get rid of it by setting it’s border to zero either in line or with CSS.

For debugging, just make the iframe say 400 by 200. You then see what is being loaded into it for checkout purposes.

i got rid of the little box by using this:
<iframe width=“0” height=“0” style=“visibility:hidden; border-color: white; border-width: 0px; border-style: none;”

it’s a little redundant but it works.

i’ll try adding the opera code.

all in all, it works in the major os/browser combinations.

Are you sure the solution you gave there works? In my experience, when the visibility is set to hidden it refuses to print in IE. I would suggest
border-width:0; instead.

John

hmmm… that might be the reason why i was having problems in safari. i will delete the hidden line and hopefully that will help.

thanks for your help! :slight_smile: