SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Mar 2010
    Location
    Pretoria, South Africa
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Control printer page size from javascript

    Hi all
    I'm battling to print labels on a dot matrix printer. The available page sizes are all bigger than a single label and I can't make custom page sizes on this printer (panasonic KX-P1150). Whenever I print a label, the printer form feeds the label paper and does not stop after the label. I have googled and see that printer comands can be send directly to the printer with ActiveX controls from inside a javascript but the only examples I found changed the orientation etc. None changed the page size or disabled the form feed.

    Can anybody help me with the correct comands or example?

    Thanks

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,822
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The page size needs to be set up for the printer in the operating system. Only its orientation can be changed in the browser settings. Nothing about it can be altered by a web page as the page has no way of telling if there is a printer.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Member
    Join Date
    Mar 2010
    Location
    Pretoria, South Africa
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the files that came with the printer driver I found the following snippet, unfortunately without any instructions of how to work with it:

    <PAGE LENGTH (INCHES)>
    Sets page length in inches.

    Name: ESC C 0 n (~n = 1 to 22~)DEC
    Dec.: 27 67 0 n
    Hex.: 1B 43 00 n

    Comments:
    *Upon receipt of ESC+C+0+n, the present line position becomes
    the top of form position.

    I just don't know what to do with this information and how to send it to the printer. I tried to change the settings through a generic driver, sending the Hex.:"<1B><C><0><1>" but this did not work consistently and did not do what I intendet. I also tried "ESC+C+0+1", "95" and other variations. Thats when I looked for other ways and what I have found are functions like:

    Code JavaScript:
    function custom_print() {
     if (document.all) {
      if (navigator.appVersion.indexOf("5.0") == -1) {
       var OLECMDID_PRINT = 6;
       var OLECMDEXECOPT_DONTPROMPTUSER = 2;
       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_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, 1);
       WebBrowser1.outerHTML = "";
      } else {
       self.print();
      }
     } else {
       WebBrowser1.IOleCommandTarget.Exec(OLECMDID_PRINT ,OLECMDEXECOPT_DONTPROMPTUSER,1,"","");
       WebBrowser1.outerHTML = "";
     }
    }
    This is supposed to print the page without any prompt from the user. (Strangely this only works with the VB version but not the javascript version)

    Or this code wich sets the margins and orientation:
    Code JavaScript:
    var shell = new ActiveXObject("WScript.Shell");
    function SetPrintProperties() {
      try {
        window.setTimeout("javascript:shell.SendKeys('%fu');",1000);
        window.setTimeout("javascript:SetPaper();", 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 SetPaper() {
      shell.sendKeys("%a{TAB}.2{TAB}0{TAB}0{TAB}0{ENTER}");
    }

    or this setting the header and footer:
    Code JavaScript:
    var HKEY_Root, HKEY_Path, HKEY_Key;
    HKEY_Root = "HKEY_CURRENT_USER";
    HKEY_Path = "\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
    // Set the page footer to print the header is empty
    function PageSetup_Null (){
      try{
        var Wsh = new ActiveXObject("WScript.Shell");
        HKEY_Key = "header";
        Wsh.RegWrite (HKEY_Root + HKEY_Path + HKEY_Key ,"");
        HKEY_Key = "footer";
        Wsh.RegWrite (HKEY_Root + HKEY_Path + HKEY_Key ,"");
      }catch (e) {}
    }
    // Set the page footer to print the header for the default value
    function PageSetup_Default (){
      try{
        var Wsh = new ActiveXObject ( "WScript.Shell");
        HKEY_Key = "header";
        Wsh.RegWrite (HKEY_Root + HKEY_Path + HKEY_Key, "& w & b on page 00 yards, & p / & P");
        HKEY_Key = "footer";
        Wsh.RegWrite (HKEY_Root + HKEY_Path + HKEY_Key, "& u & b & d");
      }
      catch (e) {}
    }
    So there is some way of influencing the printer, I just can't figure out how to tell it what I want it to do but I am sure that there must be a way.

    My page will not be public but just on the pc connected to the printer so I could make it a .hta page to loosen the security constrains.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,822
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Sounds like you have a choice of JScript or VBScript since Internet Explorer doesn't run JavaScript (it just treats it as JScript since for the most part JavaScript is a subset of JScript).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Member
    Join Date
    Mar 2010
    Location
    Pretoria, South Africa
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats interesting. I did not know that. But I'm not bound to Explorer, is there a solution for Firefox? I would prefer to use Firefox anyway.

    I will only be using this on the pc thats connected to the printer so the page would not be public. If I have to stick to Explorer I could even turn it into a .hta application to loosen the security. I would even do it directly from Excel or Word if I have to and if they could do it but I would prefer to do it from a browser.

    Whichever way, I still need to find out how to talk to the printer and surely it must be possible, why else would they give the Hex codes?

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,822
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Firefox can only run JavaScript and JavaScript is far more limited than JScript in what it can do. While there might be a way to uise Internet Explorer/JScript with its hooks deep into Windows, there is less likely to be any way you are going to be able to do it with any other browser.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    SitePoint Member
    Join Date
    Mar 2010
    Location
    Pretoria, South Africa
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, thanks for the replies. My JScript is very rusty but I'll try to find a solution that way. If I do, I will post it here for reference.

    Thanks again

    cheers


Bookmarks

Posting Permissions

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