How :javascript call print preview

I found the script from jguru to call print preview from javascript.
I dont know whats the limitations, but its works perfectly as I expected.

Here is the script :
<html>
<head>
<title>Print Preview</title>
<script>
function printpr()
{
var OLECMDID = 7;
/* OLECMDID values:
* 6 – print
* 7 – print preview
* 1 – open window
* 4 – Save As
*/

var PROMPT = 1; // 2 DONTPROMPTUSER
var WebBrowser = ‘<OBJECT ID=”WebBrowser1″ WIDTH=0 HEIGHT=0 CLASSID=”CLSID:8856F961-340A-11D0-A96B-00C04FD705A2″></OBJECT>’;
document.body.insertAdjacentHTML(‘beforeEnd’, WebBrowser);

WebBrowser1.ExecWB(OLECMDID, PROMPT);
WebBrowser1.outerHTML = “”;
}
</script>
</head>
<body>
<form>
<input type=’button’ value=”Print Preview” onclick=”printpr();”>
</form>
</body>
</html>

Thanks in advance Ravi Verelly for the script.

32 thoughts on “How :javascript call print preview

  1. Note about Scripting Support for Web Page Printing from microsoft

    WebBrowser is known to support OLECMDID_PRINT(6,1) and OLECMDID_PAGESETUP(8) printing related commands
    The trick is to create a temporary WebBrowser object with DHTML and invoke ExecWB(6, 1) on it to print the page.
    ExecWB(6, 1) could fail if a user selected the Cancel button on the print dialog

    for detail article click :
    http://msdn2.microsoft.com/en-us/library/ms976105.aspx

  2. for Dhunk and Jack :

    I’ll try with PSP using IE7 it work, but it has some limitations.
    but I forgot whats the limitations.

    Maybe You could find with some experiment and some tricky

  3. for some reason when I copied the code from here it didn’t work ((

    But when I modified it slightly it worked just fine. I am using IE7 and didn’t try it on anything else. I think the problem was with wrong quotes originally. Sorry, if this repeatation is inappropriate, but I thought it might help someone.

    function printpr()
    {
    var OLECMDID = 7;
    /* OLECMDID values:
    * 6 – print
    * 7 – print preview
    * 1 – open window
    * 4 – Save As
    */
    var PROMPT = 1; // 2 DONTPROMPTUSER
    var WebBrowser = ”;
    document.body.insertAdjacentHTML(‘beforeEnd’, WebBrowser);
    WebBrowser1.ExecWB(OLECMDID, PROMPT);
    WebBrowser1.outerHTML = “”;
    }

  4. Oops, it didn’t work right the first time and there is no option to change what I posted. So here is another try

    !
    !
    function printpr()
    {
    var OLECMDID = 7;
    /* OLECMDID values:
    * 6 – print
    * 7 – print preview
    * 1 – open window
    * 4 – Save As
    */
    var PROMPT = 1; // 2 DONTPROMPTUSER
    var WebBrowser = ”;
    document.body.insertAdjacentHTML(‘beforeEnd’, WebBrowser);
    WebBrowser1.ExecWB(OLECMDID, PROMPT);
    WebBrowser1.outerHTML = “”;
    }
    !
    !

  5. Well… seems I cannot post it right here. Tag signs are screwed up. You can delete these comments, there’s no use of them like this.

  6. It works fine in IE .
    if it is not working-mostly because your IE already disable ActiveXobject. Please enable it.

    Firefox is not support for this code.
    Firefox browser have its own print preview function -You can call the nsIWebBrowserPrint::PrintPreview() method.

    Or simple way to do is using popup window to do the print preview job.more easier ~ ^_^

  7. Do anyone tried using [ nsIWebBrowserPrint::PrintPreview() ] command in Firefox !!! Am new to the scripting language. Any suggestions will be highly appreciated !!!

    Thanks in Advance

  8. Hi
    The print preview works for me. Is there a way to close the print preview window with javascript?
    Thanks!!

  9. You may try VIEWidget from
    http://hexatech.com/viewidget

    It works with all major browers.

    It is a javascript-programmable report & technical drawing generator and print preview control.
    You may use VIEWidget to generate reports or technical drawings,
    display the results in a scrolling and zooming viewer on your web page and then print it.
    It is a very flexible and productive RAD tool as it lets you freely draw tables or
    other objects at arbitrary x-y coordinates.
    Below is a simple Hello World example with two print-preview pages generated:

    //get length unit
    var INCH = control1.getInch();

    //create document
    control1.startDoc();

    control1.drawString(1*INCH, 1*INCH, “Hello World from Page 1”);

    //…draw other stuff here

    //create 2nd page
    control1.newPage();
    control1.drawString(1*INCH, 1*INCH, “Hello World from Page 2”);

    //…draw other stuff here

    //end document
    control1.endDoc();

    //preview first page (page index=0)
    control1.setCurrentPage(0);
    control1.preview();

  10. I was curious if you ever considered changing the structure
    of your website? Its very well written; I love what youve got to
    say. But maybe you could a little more in the way of content so people could connect with it better.
    Youve got an awful lot of text for only having one or two images.

    Maybe you could space it out better?

  11. We stumbled over here from a different website and thought
    I may as well check things out. I like what I see so i am just
    following you. Look forward to going over your web page again.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s