Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
697 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...