最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to force a website to print - Stack Overflow

programmeradmin1浏览0评论

This is going to sound like an odd request, but... I have a web application that is set up to collect data and store it into a database on regular intervals. The application is designed to basically stay open all the time, and the screen displays what is most recently added into the database (by using ajax to check the database and refresh the page when there is something new). I also need to create a printout whenever something is added into the database as well. I currently have a print button, which forces the print dialogue box to come up and uses a different style sheet for a "print view" but the user is still required to press the print button, and then press print in the dialogue that comes up. Is there a way to fully automate the process to where the site will print the page to the default printer and bypass the print button and the dialogue screen that comes up?

Edit: I know I can do window.print(); to make the print dialog come up... I was asking if I can actually make a page come out of the printer with no human input. And based on the posts, it seems like it can't be done with just the web browser.

This is going to sound like an odd request, but... I have a web application that is set up to collect data and store it into a database on regular intervals. The application is designed to basically stay open all the time, and the screen displays what is most recently added into the database (by using ajax to check the database and refresh the page when there is something new). I also need to create a printout whenever something is added into the database as well. I currently have a print button, which forces the print dialogue box to come up and uses a different style sheet for a "print view" but the user is still required to press the print button, and then press print in the dialogue that comes up. Is there a way to fully automate the process to where the site will print the page to the default printer and bypass the print button and the dialogue screen that comes up?

Edit: I know I can do window.print(); to make the print dialog come up... I was asking if I can actually make a page come out of the printer with no human input. And based on the posts, it seems like it can't be done with just the web browser.

Share Improve this question edited Nov 8, 2010 at 20:41 Bill asked Nov 8, 2010 at 20:14 BillBill 5,66817 gold badges66 silver badges97 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 13

You could write a separate application that has a web-browser control, and have it do the printing for you. There's no way to do it in a regular browser, though --- think of the implications of that.

The print functionality is handled by the browser (and is thus unique per browser), it can be called (by javascript), but cannot be forced to print

(here's the javascript to call the print method in the browser)

<script language="Javascript1.2">
  <!--
  function printpage() {
  window.print();
  }
  //-->
</script>

you can bring up the print dialog using JS, i think its window.print(), which you could put with your ajax call. regarding the auto click i am not sure.

There is a similar Question at How do I create a web based print server?

We optet for creating a native print client. See http://mdornseif.github.com/2011/01/08/PDF-on-appengine.html for details.

As others have pointed out – isn't really possible to complete the print task in a web browser dialog window without human intervention or macros to control the mouse.

Instead of having the browser do the printing, I would suggest you have your ajax requests trigger something that saves the data to be printed into a file (wkhtmltopdf is one option) and have your web server (which could be Linux, OSX, Windows Server) monitor a folder and print (and then delete) whatever appears in that folder. In OSX you could do this quite easily with Automator. In Linux you could write a bash script that monitors the folder every X minutes (via cron) and runs the lpr program to send files to the printer. On Windows... well, I have no idea what you'd do, but I do recall seeing something like a folder-as-print-queue option in older versions of Windows in the printer's properties. I could be making that up, though.

发布评论

评论列表(0)

  1. 暂无评论