Printing directly to the printer

Asked

Viewed 296 times

1

I have a password system where the user clicks on a button and a password is printed for it. I needed the print to be direct, because using the js window.print() a dialog box opens. How to resolve this in PHP 7?

  • If you want it to be printed for the user you will have to use Javascript. PHP runs on the server, it would not be very interesting to print by it.

1 answer

1


As Woss said, in PHP you don’t have this control because it doesn’t work in the browser, only on the server, so the most you could do is the browser send a request to PHP on the server to ask for the print and send it to the printer, so the printer must be connected to the server or be a network-available printer.

And that’s even more interesting. One thing I always talk about, but almost every beginner (some with several years of experience, but who does not learn is still a beginner) ignores is that there are certain tools for each task. You have a task that doesn’t much matter using the wrong one, or at least the damage is not so visible (which is even worse since the person thinks it’s good), and any software that isn’t used sporadically by someone, that is not for external use of people on computers outside the organization of that software should never run in a browser. Browsers have been created for quick and easy access that does not require installation. It brings huge losses for constant use, it was not created to replace the desktop as many think.

Carro com um cavalo dentro só com a cabeça pra fora da janela

I imagine that people try to do this way because they don’t know and don’t want to know other technologies more suitable for the task. But that’s gambiarra by definition. User interaction in browsers is very impaired, and limited.

So even having a browser only available for a user to ask for a password and let PHP print is something completely meaningless, bad UX and even unsafe.

Having a web server just to deal with this and make the impression is like going from Rio to São Paulo passing through Brasília before. It is much simpler to do this with other technology directly on the computer that will handle the password, something that has better usability and is easier to control access.

If you can’t do that, ask someone else to do it. And I hope the rest of the internal systems follow the same path.

If you insist on the error (and I hope you at least have the ethics to warn the people who hire you that this is a mistake) it will operate as any web application, since it is one, and use the client in the browser only as the interaction with the user and delegate it to the server to make the print. Then the most common is to use printer_open(), but there are those who use specific server operating system resources (print, lp, etc.), or send to a special operating system file such as prn, lpt1 or something like that. Others use fsockopen(), but I’ve seen more exotic solutions.

I answered, but I’m sorry about that, it’s like giving a child a chainsaw.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.