Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Mar 2004
    Location
    New Hampshire
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Can javascript control the Print dialog options?

    Is there a way to set the parameters in the print dialog when it is invoked?

    I have a page that must be printed in landscape mode and would like to set that attribute before calling print();

    All of the users are running NT or Win2K with IE6 so I don't have to worry about other/older browsers or OS's.

    Thanks
    -Chip

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Pretty sure that it can't be done, and that's a good thing too. For security reasons it wasn't implemented. How would you like someone changing your printer settings behind your back?

  • #3
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In that environment, it is possible using ActiveX objects. Here's something I wrote to guarantee landscape and absolute minimum margins - note that the user WILL see an ActiveX confirmation prompt if this is run outside the "Trusted Sites" zone.

    Code:
    var shell;
    function SetPrintProperties() {
        try {
            shell = new ActiveXObject("WScript.Shell");
            shell.SendKeys("%fu");
            window.setTimeout("javascript:SetPaperSize();", 400);
        } catch (e) {
            alert('Please verify that your print settings have a Landscape orientation and minimum margins.');
        }
    }
    
    function SetPaperSize() {
        shell.sendKeys("%a{TAB}.2{TAB}0{TAB}0{TAB}0{ENTER}");
    }
    The timeout is to allow some time for the print dialog to open - otherwise, it won't work. In the sendKeys method of the shell object, letters preceded by % are passed as ALT+(whatever key) - %a equals ALT+A.

  • #4
    New to the CF scene
    Join Date
    Mar 2004
    Location
    New Hampshire
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Thanks for the responses Antoniohawk and Choopernickel.

    I tried the SetPrintProperties by pasting the code within the <script> tags of the page but I get an [object Error] exception when it tries to create the new ActiveXObject.

    Never having used ActiveX before, is there anything else that I'm missing?

    Thank again
    -Chip

  • #5
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Check your security settings for the zone you're developing in - set all of the ActiveX permissions to "Prompt" and let me know what happens.

  • #6
    New to the CF scene
    Join Date
    Mar 2004
    Location
    New Hampshire
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK - I set all ActiveX permissions to Prompt (I even set them all to "Allow") for all zones and still no good.

    I added an alert in the catch block to display the error code and description.
    The code (e.number) is: -2146823279
    The description (e.description) is: 'ActiveXObject' is undefined

    When the settings were "Prompt" the dialog did come up before the page loaded but, the page contains 2 frames and I'm not sure how/when the browser determines to prompt for allowing ActiveX. (e.g. Does the prompt happen for other plugins like SVG).

    One frame is for navigation, the other is pure SVG with JavaScript and Java within the SVG tags which is where I added your code.

    Is there a possibility that your script with the ActiveX object won't work because of the SVG?

    It seems from the error message that it doesn't know what an ActiveXObject is.

    What's you take on it?

  • #7
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hm... yeah, it sounds like the ActiveX might be interfered with by something involved in SVG - you did say you're using IE6, right?

    Try putting the functions in your navigation frame. It may, however oddly, have to do with the type of document being used.

    By the way, if you don't want to bother with the margins, change shell's SendCommand() string to just "%a{ENTER}".

  • #8
    New to the CF scene
    Join Date
    Mar 2004
    Location
    New Hampshire
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, IE6 (v6.0.2800 SP1) and Win 2K (v5.00.2195 SP 4).

    Good news and bad news...

    The good news is that moving the SetPrintProperties , SetPaperSize and the shell var out of the SVG frame code and into the head of the parent of both frames worked for solving the 'ActiveXObject' is undefined error.

    Apparently there must be some issue with embedding the ActiveX into and SVG page.

    Now the bad news - The Page Setup dialog appears and the parameters get set but, when JavaScript calls print(); * just after the SetPrintProperties(); , and the print dialog appears, the Layout is still Portrait.

    Opening the Page Setup dialog after the scripts run shows that the orientation is Landscape though.

    I also added a window.setTimeout(shell.SendKeys('%fu'), 1000); because many times the "%a" in the SetPaperSize opens the Favorites menu instead of acting on the Page Setup dialog.

    * I can't use window.print(); here for some reason because the combination of Win 2K and IE6 gives a Permission Denied error at that line. The combination of NT and IE6 doesn't seem to mind it though.

    Very odd things going on here...

  • #9
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, the %a would call the Favorites menu, if there's no window in the way to intercept. The timeout on the %a to the Landscape option is meant to allow the %fu (File>Page Setup) window some short amount of time to open... Post the actual code you've got which manages this page size operation.

    I've never heard of a Permission Denied error on window.print(). Add that section to the code you post. This is making me insanely curious.

  • #10
    New to the CF scene
    Join Date
    Mar 2004
    Location
    New Hampshire
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here it is, fresh out of JBuilder...

    This is the code for the JSP of the parent window:

    <%@ page language = "java" import = "java.io.*, java.util.*, com.teradyne.mdm.visualaid.CommonMethods" session = "true" errorPage = "errorPage.jsp?badPage=printgraph.jsp" %>
    <jsp:useBean id = "jobData" class = "com.teradyne.mdm.visualaid.JobData" scope="session"/>

    <html>
    <head>
    <title>Graph</title>
    <link rel=stylesheet type = "text/css" href = "VisualAid.css" title = "Visual Aid Viewer">

    <script language="javascript" type="text/javascript">

    var shell = new ActiveXObject("WScript.Shell");
    function SetPrintProperties() {
    try {
    window.setTimeout("javascript:shell.SendKeys('%fu');",1000);
    window.setTimeout("javascript:SetPaperSize();", 1000);
    } catch (e) {
    alert ("An exception occured: " + e + "\nCode is: " + e.number + "\nDescription is: " + e.description);
    alert('Please verify that your print settings have a Landscape orientation and minimum margins.');
    }
    }

    function SetPaperSize() {
    shell.sendKeys("%a{TAB}.2{TAB}0{TAB}0{TAB}0{ENTER}");
    }
    </script>
    </head>

    <frameset rows="60, *" border=0>
    <frame src="menu.jsp" scroll="no" name=printgraph0>
    <frame src="printgraph1.jsp" name=printgraph1>
    </frameset>

    </html>


    This is the script section from "printgraph1.jsp":

    <script language="javascript" type="text/javascript">
    function scaleItTo()
    {
    var a = document.getElementById("diagram");
    sx = 1.0;
    sy = prompt("Scale To", 1.5);
    if (sy != '')
    if (sy != null)
    {
    if (validateFloat(sy) == false)
    sy = 1.0;
    a.setAttribute("transform", "scale(" + sx + "," + sy + ")")
    }
    }
    function scaleIt(scaleTo)
    {
    var a = document.getElementById("diagram");
    sx = 1.0;
    //sy = prompt("Scale To", 1.5);
    sy = scaleTo;
    if (sy != '')
    if (sy != null)
    {
    if (validateFloat(sy) == false)
    sy = 1.0;
    a.setAttribute("transform", "scale(" + sx + "," + sy + ")")
    }
    }
    function validateFloat(iString)
    {
    // no leading 0s allowed
    return (("" + parseFloat(iString)) == iString);
    }
    function printOut()
    {
    // Note: Changed the "window.print()" to "print()" on 3/29/2004 due to
    // bug in IE6 running under Win 2000 - CM

    var maxY = <%= yc %>;
    // var tst = new Boolean(window.print);
    // alert ("Value of 'window.print' is: '" + tst.toString() + "'...");
    if (maxY > 755) // a little less than 768
    {
    var scaleTo = 755/maxY;
    var scalePerCent = scaleTo*100;
    scalePerCent = scalePerCent.toFixed(0);
    var msg = "This SVG cannot be printed in one page.\n" +
    "It is being scaled down to " + scalePerCent + "%.";
    alert(msg);
    scaleIt(scaleTo);
    // window.print();
    top.SetPrintProperties();
    print();
    }
    else
    {
    // window.print();
    top.SetPrintProperties();
    print();
    }
    }

    </script>

    Thanks for all of the help.
    I've been developing internal programs for engineering/manufacturing for about 12 years now and we've started transitioning to web apps from Mac client side apps.

    This is part of one I inherited after one of our developers left so the JSP/JavaScript/SVG is fairly new to me.

  • #11
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The reason I initially had a timeout for SetPaperSize and not the %fu is so that the %fu could open the print setup menu, and have 400ms to do so before sending the [i]%a{TAB}[i] etc. There has to be some time between the two commands for the dialog to open. Your timeouts both expire at the same moment, which will cause errors. The %fu doesn't (shouldn't?) need a timeout at all; it's interacting with the standard menu set. Change your timeout value on %fu at least 400ms less than the timeout value for SetPaperSize.

    Pull all of the scripting which has anything to do with printing into the frameset document, and fix the references to the printgraph1 frame. Also, double-quote all attribute values in HTML. Always.

    The print() method should then be called on window.frames[1] like so:
    window.frames[1].print();
    so that the correct frame actually prints.

    I can't think of any reason why this wouldn't work.

  • #12
    New to the CF scene
    Join Date
    Mar 2004
    Location
    New Hampshire
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    It's working finally.

    The page setup using the %fu and then SetPaperSize does set the printing attributes for the page correctly.

    However, when calling window.frames[1].print(); , the print dialog window (under the Layout tab) shows the orientation as portrait (strange).

    To get around this - and building on the previous ActiveXObject code, I created another 2 functions.

    The first contains only window.setTimeout("javascript:doPrint();", 1200);

    The second, doPrint(); , contains only shell.sendKeys("%fp");

    For some reason, opening the print dialog in this manner uses the previously set parameters from SetPaperSize whereas window.frames[1].print(); did not.

    Since the user initiates printing by clicking a link in the frame to be printed, the frame already has the focus so the correct frame prints when the .shell.sendKeys("%fp"); executes (not the most desirable method but hey, it works in this weird situation).

    In summary I would say that:

    1) ActiveXObjects cannot be created in a pure SVG page/frame.

    2) In some cases, window.print(); causes the print dialog to open using the default printer settings and not those set through the Page Setup dialog.

    3) There may be a security bug in IE6 which will cause a Permission Denied error when using window.print(); in certain circumstances (moving the code into the script section of the parent window corrected this).

    4) Using ActiveXObjects can give you alternatives to browser commands and maybe helpful for mixed OS and/or browser version environments.

    5) The delay for setTimeout may have to be increased in some cases/uses to give the client time to respond to previous commands (calling SetPaperSize with a very short delay - even 400 sometimes - does not give the client enough time to do the previous %fu and invokes the Add to Favorites menu instead).

    Thank you very much for your time and help in solving this odd, elusive and complex problem.

    -Chip

  • #13
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Not a problem, it's why I tread these tracks.

    Your point 4, though, has caveats: many painful, horrid caveats. The only OS that will create ActiveXObjects is Windows - though most versions of Windows will do so. The only browser that will create ActiveXObjects is IE - though more than one version of IE will do so (not that many more than one, however).

    Anyway, I'm sure some day you'll have the tip that helps me out.

    -Choop.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •