- Print to network printer from PHP
- 2 Answers 2
- Printing to printers in PHP
- What I’m trying to do
- More information
- What I’ve been somewhat successful with
- What I’ve tried to do
- Ideally.
- Additionally
- Edits
- Unsuccessful methods
- Method: PHP Printer functions
- Method: JavaScript/AJAX
- Untested methods
- Methods: Not yet tested
- Solution?
Print to network printer from PHP
It seems like this should be a simple task that has a widely accepted approach, but so far I’m not finding it. Thanks!
Not sure if this is possible in your case, but to avoid all sorts of problems with trying to go from webserver to printer, we are using the FTP mode of the printers to accomplish this. We just dump the pdf on the ftp-spool thingy the printer itself has, and you’re done (once you’re able to convert everything to PDF obviously). Only usefull if your printer supports this, so that’s why you’re getting a comment, not an answer 😉
Use php_printer.dll . What’s wrong with it? Why isn’t it working for you? Don’t reinvent the wheel by talking directly to IP printers.
Why not let the OS handle the printer? What happens if you script is trying to hit that ‘default printer’ and the network (or route to the printer) is unavailable?
@Brad — I can successfully connect to a printer, and then use fopen/fread to get the pdf contents, also with no errors. If I change the PRINTER_MODE to RAW I get no errors, but nothing happens. If I don’t change the printer mode, I get 15 pages of garbage. In searching, I’ve seen that there can be some problems when Apache is running as the SYSTEM user, but I am running Apache as a different user with admin privileges, so that shouldn’t be a problem here.
2 Answers 2
This is a tough nut to crack. I’ve had my own adventures in Windows printing from Ruby and came up with a few potential solutions that work by invoking an external command, which in PHP-land is system() or exec() (don’t forget escapeshellcmd() / escapeshellarg() —they tend to make this stuff easier, especially on Windows). All of them assume Windows knows about the printer and it can be referenced by name.
- You can literally just redirect the file to the networked printer, e.g.:
copy /b \path\to\filename.pdf > \\Printer_Machine\Printer_Queue
print /d:\\Printer_Machine\Printer_Queue \path\to\filename.pdf
AcroRd32.exe /t \path\to\filename.pdf "Printer Name" "Driver Name" "Port Name"
gswin32.exe -sDEVICE=mswinpr2 -sOutputFile="%printer%Printer Name" \path\to\filename.pdf
Finally, a general tip: You can register a network printer with a device name with the net use command e.g.:
C:\> net use LPT2 \\Printer_Machine\Printer_Queue /persistent:yes
This should let you use LPT2 or LPT2: in place of \\Printer_. with most commands.
Printing to printers in PHP
I’m trying to set up a CLI PHP application to print a set of web pages to a default or specified printer. I’m on a Windows 7 machine with PHP 5.2.11 running in a CLI. To test the print functionality I’ve loaded PHP_printer.dll and I’m printing to Onenote, a print to file option, using the exact printer name given in PRINTER_ENUM_LOCAL.
Update: Here’s the latest code:
$handle = printer_open("Send To OneNote 2010"); printer_start_doc($handle, "My Document"); printer_start_page($handle); $filename='index.html'; $fhandle=fopen($filename, 'r'); $contents = fread($fhandle, filesize($filename)); fclose($fhandle); printer_set_option($handle, PRINTER_MODE, "RAW"); printer_write($handle,$contents); printer_end_page($handle); printer_end_doc($handle); printer_close($handle);
I’ve gotten this code to print a blank page to the correct printer, but I’m unable to print the strings I pass to printer_write. I confirmed that $contents is properly filled with the contents of my test html file. No matter what I provide as the second arg (string to be printed) I get a blank page. Is there something I’m missing to at least allow me to print some text onto a page? Alternately is there a better way to do this (using PHP/javascript files)? What I am trying to do is print web pages as they appear (CSS included) via a CLI app, the web siteis written in PHP and I’m trying to minimize complexity. If there is a better way to print these (converting to PDF and printing is an option apparently) I’m open but it sounded like this was the simplest/de facto method in PHP.
What I’m trying to do
Print a PDF document on a network printer while passing printing parameters for things such as colour, orientation, duplex, etc.
More information
- We have multiple network printers, for which the IPs are static and known (i.e. 192.168.0.10)
- I found a document containing a list of parameters/options that can be passed to the printer for changing print settingshere (most of which can be found on page 25)
- We are creating a process by which we will be printing a document in black and white EXCEPT the second page. This means that the first page, as well as page 3 and beyond are printed in black and white; page 2 is to be printed in colour. (This is due to cost of colour printing, plus our other pages don’t contain colour, so printing them in colour is EXTREMELY pointless and approx. 8x more expensive — This process will be printing thousands of pages each month, which adds up to a lot of $$$)
What I’ve been somewhat successful with
I logged into one of the printers (192.168.0.10 — Ricoh MP C5503 [if you really must know]) and added FTP access
Printing a document using the command prompt in Windows works!
> ftp 192.168.0.10 > User (192.168.0.10:(none)): username > Password: password > put path\to\file.pdf filetype=PDF > bye
What I’ve tried to do
Attempt 1 using PHP’s exec() function
I’ve tried MANY ways to make exec work, but to no avail. I have not been able to run multiline commands using PHP’s exec function (ideally, running the following). When running the following inside exec(), I am unable to connect to FTP since each line must be executed after the previous line has run. I have found nothing online (multiple Google searches yield no results [except how to echo multiple output lines from cmd — not how to insert multiple cmd lines consecutively])
> ftp 192.168.0.10 > User (192.168.0.10:(none)): username > Password: password > put path\to\test.pdf filetype=PDF > bye
Attempt 2 using PHP’s exec() function
I attempted to run the ftp command by passing a text file as some answers on this post suggest. This solution does not work as expected. When running ftp -i -s:test.txt from a command prompt, this works; in PHP, it does not.
open 192.168.0.10 username password put test.pdf filetype=PDF bye
Attempt 3 using PHP’s FTP functions
What I can’t figure out now, is how to send the file to the printer over ftp and how to set the printer settings
$ftp = [ 'server' => gethostbyaddr('192.168.0.10'), 'username' => 'username', 'password' => 'password', ]; $conn = ftp_connect($ftp['server']); $login = ftp_login($conn, $ftp['username'], $ftp['password']); if (is_readable($file)) < if (ftp_put($conn, $file, $file, FTP_ASCII)) < echo 'Successfully executed command'; >else < echo 'Failed execution of command'; >> else
Ideally.
I’m looking for a solution that would work on both Windows and Linux systems as we are also in the process of moving from IIS to NGINX (thank god. ). The method that I believe would be the best implementation is using ftp for Windows and rcp or rsh for Linux (since the printer documentation I attached under More information in the What I’m trying to do section mentions these methods for print).
I’d also like if we did not have to generate txt files or some other file type in order to print these documents. Our users may be processing hundreds of files at once, which I understand we can uniquely name each txt file and then delete after the script has successfully run, although I’d much prefer a clean solution where we can pass in parameters such as printer (IP or name from gethostbyaddr() function), username, password, file to be printed, options (i.e. colour, duplex, filetype, orientation, binding, etc.). We are using MPDF to generate our PDF files, so a method that would place the file without actually creating it on our server where we would then have to delete it would be preferred (i.e. MPDF string attachment for email) but these are not required.
Additionally
The solution must work with PHP.
I will continue to investigate more methods for printing documents on network printers after I post this until a viable solution has been found and update my post accordingly following each attempt.
Any help regarding this is greatly appreciated.
Edits
Unsuccessful methods
Method: PHP Printer functions
The printer functions do allow for printing, however they do not allow for control over the print jobs (options such as color/black and white printing, filetype, duplex, etc.). Thus, this options (unless someone has a «hack», will not work)
Method: JavaScript/AJAX
Although JavaScript/AJAX would work for printing (and allow print settings), we will be processing potentially hundreds of pages at once and the processing may be pushing print jobs to multiple printers (i.e. in different offices). The idea is to automate our printing for this process and for future processes alike.
Untested methods
Methods: Not yet tested
Solution?
I ended up creating a C# script to accomplish everything I needed to do. Since my requirements are fairly specific with regards to print settings, here’s a link to Microsoft’s System.Drawing.Printing namespace. Using this namespace, I was able to create appropriate methods for my needs. A few StackOverflow questions/answers below that provide more details on usage:
How to run C# in PHP? That’s up to you. You can interface with a C# API (having C# run on another web server for example), reference the .NET DLL, use PeachPie, or inject declarations at runtime.
You may also be able to conjure up something using PowerShell (see PrintManagement documentation here). This would allow you to run the script using exec() — see this question regarding the execution of PowerShell from PHP.