How to automate printing using .bat file by call using javascript/jquery

I have web application and it have a print function. It simply print some HTML aligned content using a Dot matrix printer. But in web application it prints in Graphics mode, due to this printing is very slow. So I tried the solution using the link. My print method as follows:

var data=[].slice.call($("#printdiv").find("table tr")).map(function(row){
                return row.textContent.trim().replace(/\n/g,"\t");
            }).join("\n");

window.open("data:application/print;," + escape(data));

My print.bat as follows:

notepad /p %1

When I click print button it simply download a file and then it needs to be open with print.bat file to print and it works perfectly.

How can I automate that. I mean on clicking print function I want call the print.bat file and need to pass the content or notepad file to the bat file and should work without manual intervention.

I tried to call .bat file using ActiveXObject but it is not working in anywhere (not even working in IE).

How can I automate calling .bat file from browser using javascript/jquery and automate printing?

Answers 1

  • For windows, you CAN do this for plain text, with a custom MIME handler application, which is provided by a custom batch file.

    1. Create a new print.bat file containing @notepad /p %1.
    2. Use a tool like https://github.com/rndme/download to download your text to print with a custom mime: download("Hello World", "temp.prn", "x-application/print");
    3. In Chrome, Right-Click the download in the download bar, and choose "Always open files of this type"
    4. repeat step 2, triggering a File Open Application Selection Dialog
    5. Check "Use this app for all .prn files", then browse manually to your bat file and choose it. Might be under "More Options", "Look for another App...".
    6. you should hear the printer warming up and printing.

    Firefox directions are similiar, you basically just need to associate the bat file with a custom MIME (in this example i choose "x-application/print", but any un-used non-text one should work). Then you can use a downloader (or a manual <a download> link to pass a string to your printer, as text.

    Live demo: http://pagedemos.com/kwk9wzpq46j3/1 - make sure to open with print.bat file above.

    I've used this trick to automated print 1,000s of media labels based on data stored in a PHP CMS with no obvious export path, and it worked well for us.

    i don't think you can do this with graphics or other non-text-like things.

    Lastly, you can edit the print headers in notepad's config, and your customization should be remembered, for better automated printing of arbitrary text.


Related Articles