print PDF to physical printer

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
amyhughes
Forum Newbie
Posts: 14
Joined: Fri Apr 13, 2012 7:54 am

print PDF to physical printer

Post by amyhughes »

Is there a way that currently works in PHP 5 on Windows 7 to print a PDF to a physical printer from a PHP script?

I've read a great many suggestions to use AcroRd32.exe. This no longer works, and was never a workable solution since it apparently leaves Reader open. It currently doesn't work on WIndows 7 at all. Not even from the command line. Reader can no longer read a path name from the command line:

"The system cannot find the path specified"

I get this even when I am sitting in the document directory so I don't have to specify a path. I do have Reader associated with PDF files, and it does come up when I double-click PDFs in explorer.

I also had to put reader in my Path because spaces in the executable path give me

"C:\Program" is not recognized as an internal or external command..."

or

"The filename, directory name, or volume label syntax is incorrect"

if I put it in single quotes.

In short, none of the examples I found work syntactically.

I also looked into the Printer commands in PHP. They rely on a DLL that is no longer maintained, and for which there is not a 64-bit build.

So, is it even possible to print PDFs to a physical printer in a PHP script? Or even from the command line?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: print PDF to physical printer

Post by Celauran »

amyhughes wrote:So, is it even possible to print PDFs to a physical printer in a PHP script? Or even from the command line?
No.
amyhughes
Forum Newbie
Posts: 14
Joined: Fri Apr 13, 2012 7:54 am

Re: print PDF to physical printer

Post by amyhughes »

Here is one of many nearly identical non-working examples I've found on the net:

shell_exec("'C:\Program Files\Adobe\Acrobat 6.0\Reader\acrord32.exe' /t c:\test1.pdf \\myserver\myprinter");

As I mentioned, I eliminated the need for spaces in the executable path by includung reader in my search path:

shell_exec("acrord32.exe /t c:\test1.pdf \\amy-laptop\hp2727");

and eliminated the path to the file by running this from the same directory as the file:

shell_exec("acrord32.exe /t test1.pdf \\amy-laptop\hp2727");

and eliminated the printer just to see what that'll do (answer:nothing)

shell_exec("acrord32.exe /t test1.pdf");

and run it from the command prompt

acrord32.exe /t test1.pdf

and tried without arguments at all

acrord32.exe

This works. It launches reader with no document. So, the path thing is working. It's the only thing that's working.
amyhughes
Forum Newbie
Posts: 14
Joined: Fri Apr 13, 2012 7:54 am

Re: print PDF to physical printer

Post by amyhughes »

Celauran wrote:
amyhughes wrote:So, is it even possible to print PDFs to a physical printer in a PHP script? Or even from the command line?
No.
Seriously? Is there a way to print anything at all from a PHP script?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: print PDF to physical printer

Post by Celauran »

amyhughes wrote:
Celauran wrote:
amyhughes wrote:So, is it even possible to print PDFs to a physical printer in a PHP script? Or even from the command line?
No.
Seriously? Is there a way to print anything at all from a PHP script?
No.

Closest you can come is to display on screen and use JavaScript's window.print.
amyhughes
Forum Newbie
Posts: 14
Joined: Fri Apr 13, 2012 7:54 am

Re: print PDF to physical printer

Post by amyhughes »

This isn't an interactive script, or a web page. It's a script that periodically polls for available data through a web API, and it's supposed to leave prints on the printer in the morning.

Thanks,
Amy
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: print PDF to physical printer

Post by Celauran »

PHP probably isn't the right language, then.
amyhughes
Forum Newbie
Posts: 14
Joined: Fri Apr 13, 2012 7:54 am

Re: print PDF to physical printer

Post by amyhughes »

Here's what works from the command line:

put the executable path in double quotes, and use /p /h and the default printer

"C:\Program Files\Adobe\Acrobat 6.0\Reader\acrord32.exe" /p /h test1.pdf

It also seems to work from shell_exec
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: print PDF to physical printer

Post by mikosiko »

assuming that you are talking of an INTRANET scenario where server/clients/printer are in the same network then Yes... it is possible... sure Celauran was thinking in the INTERNET scenario.. and for that he is correct.

Instead of Acrobat I do use PDFExchange viewer... and more or less I do something like this:

Code: Select all

   // Define Main File Path, Printer and Printer command
   $filepath = $_SERVER['DOCUMENT_ROOT']."/myfolder/"; // Path to the file that you want to print
   $printer = "\\\\MyPrintServer\\yourprinternamehere";  // absolute UNC path to your printer .. note the use of backslashes to escape the necesary ones
   $printercmd =  "PDFXCview.exe /print:printer=\"$printer\" ";  // line will change depending of what viewer you are using to print

and down below my code

   $printcmd = $printercmd . " $file";  // here I do add the file to print
   exec($printcmd);  
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: print PDF to physical printer

Post by pickle »

I spent a couple weeks researching this as well, and came up empty. On one hand it is surprising that simply printing a file is not possible. On the other hand it does make sense - PHP is a language for building web pages, not printing files on a server.

You may be able to open a socket connection directly to the printer and then send the file data. You don't get to set any properties (ie: finishing, double-sided, etc) though.

I did look into a PERL solution, but I gave up when I had to install PERL.

A Linux server will work, provided the printer vendor provides drivers for Linux. I ended up having to use OS X & CUPS/lpr.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
amyhughes
Forum Newbie
Posts: 14
Joined: Fri Apr 13, 2012 7:54 am

Re: print PDF to physical printer

Post by amyhughes »

As soon as they answer me a licensing question I hope to use this instead of Reader...

http://www.coolutils.com/TotalPDFPrinter

It has control over printer settings right from the command line, and works like a charm from shell_exec.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: print PDF to physical printer

Post by mikosiko »

well... PDFExchange viewer is free... and printing in the way that I did describe previously is something that my users do everyday... and they print a lot...
Post Reply