问题
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.
回答1:
i messed around with printer.dll for ages and got sick of it.
not sure if its much use, but i purchased this application http://www.coolutils.com/TotalPDFPrinter
and it lets me print pdf files from the cmd line from php, which worked well for me, but it does have a popup screen which you can only get rid of if you purchase the server version (which you will need if your apache is running as a service). obviously the non-popup version is way more expensive.
one thing i struggled with, was getting a list of printers available, so i wrote this little app in C# that spits out a list of printers as plain text, and then use php to call that and put them into a drop list on my web forms.
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Printing;
namespace printerlist
{
class Program
{
static void Main(string[] args)
{
foreach (String printer in PrinterSettings.InstalledPrinters)
{
Console.WriteLine(printer.ToString());
}
}
}
}
回答2:
As far as I know, you should use php_printer.dll
. Check here. Then, add the php_printer.dll extension to your php.ini. If you have done that, then...
If you qualify and put clearly the ip/route in where the printer is installed, it should do the trick. Something like printer_open('\\\\xx.x.xx.xx\\_printername_');
. (Local printers wouldn't need to add the server manually, afaik)
To check for printers, try something like printer_list(PRINTER_ENUM_LOCAL | PRINTER_ENUM_SHARED)
. There is something like a printer.default_printer
directive on php.ini, if I recall ok. Tested this quite some time ago on a php 5.x installation. Good luck.
来源:https://stackoverflow.com/questions/7841720/printing-to-printers-in-php