Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

How to get pages printed in javascript

Posted on 2014-10-27
Last Modified: 2016-04-20
I am using the below code to open a new tab with the print dialog box and then close that that page when the user prints or cancels. Is there a way to record the number of pages the user prints so that I can insert that into a mysql table?

function openPrintWindow() {
var printWindow = window.open('<? echo "page_to_open.php?id=".$id."&name=".$name; ?>');
var printAndClose = function() {
    if (printWindow.document.readyState == 'complete') {
var sched = setInterval(printAndClose, 200);
Question by:Alex Loff
  • 5
  • 4
LVL 58

Expert Comment

ID: 40407742
Not without some kind of ajax that you inject in your new tab/print function

Author Comment

by:Alex Loff
ID: 40407745
How involved would that be? Can you give me some pseudo code to get going?

The idea is to do this for inventory management. Labels are being printed and that will give me an exact count of what is being sent. I know I can't pass javascript variables to php but my plan is to pass that variable to a hidden input and when printWindow closes, submit the form with the number of pages printed. Is there a more efficient/effective way?
LVL 58

Expert Comment

ID: 40407747
Are you using jquery or just plain javascript
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

by:Alex Loff
ID: 40407759
I'm using jquery elsewhere on this page. https://jquery-textfill.github.io/

Author Comment

by:Alex Loff
ID: 40412342
Gary, I appreciate the help thus far. Any other insight or help you can provide? Thanks
LVL 58

Expert Comment

ID: 40412349
Well you can't detect if the user cancelled the print job, but you can record when they open the print window

So in your openPrintWindow function add this jquery

    url : 'printlogger.php?url='+location.href+'&userid='+someuserid // page that logs the print

Open in new window

Then in printlogger.php just record the url and user id

Author Comment

by:Alex Loff
ID: 40412363
Thank you for your help. Would I be able to get the number of copies printed this way?
LVL 58

Assisted Solution

Gary earned 250 total points
ID: 40412367
No, not without writing a java app to handle the printing. and bypass the browser print dialog (and that is a whole different story)

Author Comment

by:Alex Loff
ID: 40412372
Yea I certainly don't want to do that...ok, thank you for your help.
LVL 43

Accepted Solution

Rob earned 250 total points
ID: 40417746
It's not possible using javascript and it's got to do with security.  javascript runs within the browser and doesn't have access to the user's operating system, including the printers.  So to answer your question, it's not possible using javascript.

That said, a few ideas come to mind such as having the user submit the page to your web server to parse the page into a pdf that is then sent back to the user to print.  That process will not only allow you to record the number of pages created in the pdf but also control the layout of the print to ensure barcodes and other data will be properly printed (or at least closer than you'd get from the browser).

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Grunt script for Build Process 1 42
Make options hidden and show just option that is on 1 29
Html form and modal / img src -problem 3 30
jquery tab header text 1 23
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question