Reputation: 167
Today I have a hard challenge with php+javascript+html+css:
I'm creating an application to "authorization+print it": the app must run in a local apache(under windows preferably), and need a conection to a DB in the cloud. That's easy. With the user authenticated, a list of authorized files must be shown, another easy task too. It's working. But now comes the crazy: I want to print them without let the user select and see any print option, only a button "Print". The print configuration comes in a .txt file and I need to configure the printing, sending the file and the configuration to the printer.
I searched a lot, but I only see the "print this page" buttons or shell solutions (gsview and gsprint for windows, but I cannot use that because I cannot configure the print options). I need anything more complex. Could you help me? (Im trying now fpdf, but...omg, I cannot understand If this could be used to do that I want.
Non-free/installed solutions could be in help too.
In addition, I need to print multiple files, but that's optional (I can do anything like "while")
PD: sorry for my english level.
Upvotes: 1
Views: 5128
Reputation: 41756
Print from client-side = form the browser via javascript
It's not possible to do this from client-side (= from inside the Browser). There are hackish solutions out there, which might work for IE, like the one here: HTML / Javascript One Click Print (no dialogs), but in general "if you try to print, the dialog will pop up" = default behaviour of "window.print()".
Print from server-side
Basically you use the server-side (PHP) to print the document, instead of the client. So you might use an Ajax request (user clicks on the print button) handing the filename or the content to print over to the "print.php" file on the server, which does the job of pushing the content to the printer.
Of course, you'd have to know which printer the user wants the content to be printed at...
There are several ways to print from PHP.
One option would be to use the php_printer
extension:
$handle = printer_open();
printer_set_option($handle, PRINTER_MODE, "raw");
printer_write($handle,$myfile);
printer_close($handle);
Or just copy
or print
to the printer:
exec('copy C:\file.txt com1');
exec('copy C:\file.txt lpt1');
exec('print /d:LPT1: C:\file.txt');
If you have network printer, you could try to send your content to the network address. There are some PHP utils around to work with LPR: https://github.com/Craswer/PhpNetworkLprPrinter
Referencing: https://stackoverflow.com/a/5695181/1163786
Question from comment: How can i set printer options from PHP on Windows?
This very easy on Linux because lpr accepts options lpr <options>
- but that's not the case on Windows. So, here are some Windows specific tricks to configure the printer:
Windows7 has PRINTUI.EXE
- a shorthand for RUNDLL32 PRINTUI.DLL,PrintUIEntry
Please see PrintUI Reference for examples.
You can configure your printer manually, for instance activate duplex mode, then save the settings file and re-use it, when printing from PHP. This allows to work with multiple printer configuration files.
The easiest way is to configure the printer in your env and then access it by name, "Printer-HP-XY-DuplexOn-2PagesOn1". In other words: it's configured outside and not from within PHP, only accessed from there.
Upvotes: 1