Jump to content

Little printscript for IE


cssfreakie

Recommended Posts

Hi guys,

 

I am trying to play a bit with a function i found (and adjusted) but for some reason All I get is a print window popping up and after a few seconds the window bleeps but nothing happens. Anyone has an Idea how to create it so that i could print automatically (on my localhost) for say every 5 minutes.

Can this be done or should I use VBS for this. (or maybe a trained hamster to press 'enter' every 5 min ;)

Sorry that this script looks kinda stupid, but i know little of javascript, just wanted to play a bit with it.

<script type="text/javascript">

window.print();

shell = new ActiveXObject("WScript.Shell");
    setTimeout("shell.sendKeys('{ENTER}')",1300);
</script>

 

I got my inspiration from: http://isaacschlueter.com/tests/landscape.html and

http://social.msdn.microsoft.com/Forums/en/netfxjscript/thread/63734fa3-ac5d-4857-8413-5302cfd2307c

Link to comment
Share on other sites

Technically, you cannot print directly using javascript without user interaction. The reason your script doesn't work is because the print dialog will block any script exection until the user either clicked yes or no.

 

However, you can run around this issue by creating a text file and opening in with a print command. The sample code below will only print the text of your page (will get the innerText of your print div).

<div id="my_print_div">
<strong>This is a test</strong><br /><br />Hello World
</div>
<script type="text/javascript">
// the text file name (same as the html file + .txt)
var txt_file = "C:\\Users\\Desktop\\test.txt";   // you will have to change this, use double \

// creating a file and printing it
function create_file(){
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.CreateTextFile(txt_file, true);
f.WriteLine(document.getElementById('my_print_div').innerText);
f.Close();
print_file();
};

function print_file(){
var shell = new ActiveXObject("WScript.Shell");
shell.Run("notepad.exe /p "+txt_file);
};

create_file();

</script>

 

Notes: Use a <br /> for line breaks and the output is only text. Use double \ in the file path to avoid wrong path issues.

 

If you can get the printable content in a RTF format (for text format), you can replace the .txt in the file name to .rtf and change NotePad.exe to wordpad.exe

Link to comment
Share on other sites

Oh wow Thanks No gray! really appreciate this

 

I am going to test that in the morning. And let you know how it went. Wicked! ::)

Cheers!

 

P.s. may I ask where you learned this from? a book in particular?

Link to comment
Share on other sites

hummm, don't think there is a book that will show this trick. You'll just have to search for different techniques (even in different programming languages) and try to apply the same principles to your code.

Link to comment
Share on other sites

OK i just tried it but it seems something is not right, just to tell what i did.

 

I made a file named test.php and placed the following code in it.

 

<!DOCTYPE html
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />       
        <link type="text/css" rel="stylesheet" href="css/style.css" />
        <title></title>
    </head>
    <body>
        <div id="my_print_div">
            <strong>This is a test</strong><br /><br />Hello World
        </div>
        <script type="text/javascript">
        // the text file name (same as the html file + .txt)
        var txt_file = "C:\\Users\\CSSFREAK\\Desktop\\test.txt";   // you will have to change this, use double \

        // creating a file and printing it
        function create_file(){
                var fso = new ActiveXObject("Scripting.FileSystemObject");
                var f = fso.CreateTextFile(txt_file, true);
                f.WriteLine(document.getElementById('my_print_div').innerText);
                f.Close();
                print_file();
        };

        function print_file(){
                var shell = new ActiveXObject("WScript.Shell");
                shell.Run("notepad.exe /p "+txt_file);
        };

        create_file();

        </script>
    </body>
</html>

 

than on my desktop i made a file named test.txt with no content in it I right clicked it to check the path and copied that in the code above.

 

After that i ran the script on my localhost (which is wamp) in IE9. Is there something i am missing here?

 

I also tried the same without a test.txt on my desktop, but it made no difference. (could it maybe have something to do with the ActiveXObject ?

Link to comment
Share on other sites

Could you be a security setting that is blocking the activex, try to rename your .html file to .hta and try again.

 

U used IE 9 and put out all security settings for  activex for both local and online environment.

 

Could it maybe be that it not worked, because i named it .php instead of .html? I doubt it a bit, but you never know :)

I'll try it out and let you know.

 

So just to be sure if i make a .html or .hta file and put the code you supplied in there, if i were to run it it should work? or should i add something.

 

Thanks for the help mate, really appreciate it.

Link to comment
Share on other sites

.hta is an HTML application and need to run on a desktop (not on a server). I tested on IE 9 as an html file on my desktop, I did something similar long time ago with IE7.

 

There are a few zones for IE, make sure activex is allowed on your security zone (either local intranet or internet). ActiveX is not recommended for browsing the internet in general.

 

.hta files is an HTML file that runs like a desktop application without any security restrictions.

 

If you need this to run as a server app, you might have to add the your site (or localhost) to the trusted sites security zone and allow activex to run on trusted sites. Also, make sure the file bath is correct and accessible from your page.

Link to comment
Share on other sites

I I got it working, damn that was a sneaky one. I had to set 1 very specific option in the internet zone, not local but internet.

See image what it was. I got it from here

 

Thanks a million nogray, that script worked great!

cheers!

 

[attachment deleted by admin]

Link to comment
Share on other sites

Just for security reasons, place your site in the trusted sites and only enable that option for trusted sites zone. Browsing the internet with ActiveX on might compromise your computer security.

 

Good one!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.