How to get pages printed in javascript

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') {
        clearInterval(sched);
        printWindow.print();
        printWindow.close();
    }
}
var sched = setInterval(printAndClose, 200);
};
Alex LoffAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
Not without some kind of ajax that you inject in your new tab/print function
0
Alex LoffAuthor Commented:
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?
0
GaryCommented:
Are you using jquery or just plain javascript
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Alex LoffAuthor Commented:
I'm using jquery elsewhere on this page. https://jquery-textfill.github.io/
0
Alex LoffAuthor Commented:
Gary, I appreciate the help thus far. Any other insight or help you can provide? Thanks
0
GaryCommented:
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

$.ajax({
    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
0
Alex LoffAuthor Commented:
Thank you for your help. Would I be able to get the number of copies printed this way?
0
GaryCommented:
No, not without writing a java app to handle the printing. and bypass the browser print dialog (and that is a whole different story)
0
Alex LoffAuthor Commented:
Yea I certainly don't want to do that...ok, thank you for your help.
0
RobOwner (Aidellio)Commented:
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).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Services

From novice to tech pro — start learning today.