1

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?

Santhucool
  • 1,656
  • 2
  • 36
  • 92
  • you can't call a .bat file using javascript because of security reasons, you may need a server side language for you needs – madalinivascu Mar 30 '16 at 05:00
  • @madalin ivascu please check my problem and check the link above. If you have any alternative to crack this issue then it will be more helpful. – Santhucool Mar 30 '16 at 05:16
  • why aren't you using the default ctrl+p ? – madalinivascu Mar 30 '16 at 05:23
  • @@madalin ivascu buddy I am using dot matrix printer. In web browsers it prints in Graphics mode so the printer prints very slow. I want to print in text mode just like printing from a notepad. Here speed is the main concern. I hope you got it. – Santhucool Mar 30 '16 at 05:26
  • What do you mean by Graphics mode ? – madalinivascu Mar 30 '16 at 05:34
  • If we take the print using javascript print() it will open in a new window just same as cntrl+p and it will print in Graphics mode. please check out the link buddy http://www.codingforums.com/archive/index.php/t-235333.html. It will not make much effect on other printers but in dot matrix it will makes very slow. – Santhucool Mar 30 '16 at 05:38
  • Ok BUDDY from what i can tell you want to print only the text without any formating and style, a simple whay is to select all the text from the page and then click right /print, another whey is to use javascript and take the text from the body tag and append it to a new windows and then call print() to that – madalinivascu Mar 30 '16 at 05:43
  • i actually wrote that coding forums post linked by OP, years ago, but the site's been down for a week or two, maybe forever? it was a good post, so see my answer below for a write up... – dandavis Mar 30 '16 at 06:07

1 Answers1

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.

dandavis
  • 16,370
  • 5
  • 40
  • 36
  • I have put `download("Hello World", "temp.prn", "x-application/print");` inside button click method but I am getting error as `Uncaught ReferenceError: download is not defined` help me buddy – Santhucool Mar 30 '16 at 05:53
  • Possible to create a fiddle? – Santhucool Mar 30 '16 at 05:54
  • yeah, you need to add the download.js file linked above to your page, then it should work. live demo link added... tested with Chrome and windows 8 – dandavis Mar 30 '16 at 05:54
  • is there any option to automate this I mean without downloading? I mean can we directly pass to batch file? In this current scenario client needed a batch file always to print. – Santhucool Mar 30 '16 at 06:14
  • it downloads to the temp folder, which windows will eventually clean out. if you have the batch file, it should be 100% automatic, no human intervention required for each job. i've printed 1,000 of text documents overnight like this. you need the batch file, no way around that. let me know if you have any trouble getting it automated and i'll see if i can help. – dandavis Mar 30 '16 at 06:18
  • Buddy mine is a web application and client may print from various system, so he may not have a batch file with him. I mean on button click it should start download. Can I associate all the things in server side and do the printing in client side??One more thins it is now downloading in downloads folder and not in temp folder as you mentioned. – Santhucool Mar 30 '16 at 06:24
  • the device that will print must have the batch file, there's no way around that. – dandavis Mar 30 '16 at 06:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/107691/discussion-between-santhucool-and-dandavis). – Santhucool Mar 30 '16 at 06:27
  • Buddy one simple doubt. It will download a file, Is that any way so that I can directly pass to the batch file without downloading?So that it will print automatically. As per your suggestion I should click the downloaded file to start printing. Can I automate that without user click on that file? – Santhucool Mar 30 '16 at 06:53
  • Yes, there's two options you need to pay attention to: 1. "Use this app for all .prn files" on the OS's browse dialog when coosing the bat file. 2. in chrome, Settings>Show Advanced Settings>Downloads>Ask where to save each file before downloading should be UN-checked. then it will be fully automatic. – dandavis Mar 30 '16 at 18:05