1

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.

1 Answers1

1

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 copyor 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.

Community
  • 1
  • 1
Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
  • Yes, that's the mainly answer, Now im battling with "how to print to a LPR with PHP". Do you know if you can configure the print before send the files? – Barragán Louisenbairn Nov 03 '14 at 09:34
  • I've appended my answer. – Jens A. Koch Nov 03 '14 at 13:37
  • ok, perfect answer, I'll g ive you vote up when have 15rep. I just add a little point of information: I founded how to "configure a driver in windows". Installing the Resource Kit Tools Windows Server 2003 (from here: http://www.microsoft.com/en-us/download/details.aspx?id=17657)(you can install it in win 7 x64 with compatibility mode and selecting Work in previous version of windows-> Windows Server 2003). Then you can use the "setprinter.exe" command to configure it. I'm learning now how to use it. (configure 64 printers, no thanks :_D? – Barragán Louisenbairn Nov 03 '14 at 16:45
  • "setprinter.exe" - another goodie from the old days :) I found out that you can also use PowerShell: Set-PrintConfiguration - http://technet.microsoft.com/en-us/library/hh918361.aspx – Jens A. Koch Nov 03 '14 at 17:06
  • PowerShell 2.0 is hidden-previously-installed in Windows 7: look at this link: (you have a link inside to install v3.0 in Win7 inside: http://blogs.technet.com/b/heyscriptingguy/archive/2011/01/07/how-do-i-install-powershell-on-windows-7-and-other-questions.aspx – Barragán Louisenbairn Nov 03 '14 at 17:28
  • I just tried Set-PrintConfiguration in PowerShell 2.0: + Set-PrintConfiguration <<<< + CategoryInfo : ObjectNotFound: (Set-PrintConfiguration:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException I'll try with PowerShell 3 – Barragán Louisenbairn Nov 03 '14 at 17:36
  • You need PowerShell 4.0. Quote: "Applies To: Windows 8.1, Windows PowerShell 4.0, Windows Server 2012 R2" – Jens A. Koch Nov 03 '14 at 17:39