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.

7
  • 2
    Unless this is a networked printer, there is no way to print from a browser without showing a print dialog. Commented Oct 30, 2014 at 15:58
  • It sounds like he might want to print from the server side (apache/php). Commented Oct 30, 2014 at 16:09
  • @epascarello please dont tell that: read this: how to configure mozilla to dont show the print dialog and print with pre-established preferences in predetermined printer. forums.mozillazine.org/viewtopic.php?t=48336 It's a little part of what I need. Commented Oct 30, 2014 at 16:43
  • @DigitalChris: I dont need print from server side, can be done from client side (it's a local-installed apache: server and client will be the same CPU) Commented Oct 30, 2014 at 16:44
  • @BarragánLouisenbairn You are changing the browser for a specific user. That is fine if you want to let any page just start randomly printing 100's of documents. Commented Oct 30, 2014 at 16:45

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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?
I've appended my answer.
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: 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?
"setprinter.exe" - another goodie from the old days :) I found out that you can also use PowerShell: Set-PrintConfiguration - technet.microsoft.com/en-us/library/hh918361.aspx
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: blogs.technet.com/b/heyscriptingguy/archive/2011/01/07/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.