用 PHP 打印到打印机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7841720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing to printers in PHP
提问by Ben Brocka
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:
我正在尝试设置 CLI PHP 应用程序以将一组网页打印到默认或指定的打印机。我在 Windows 7 机器上,PHP 5.2.11 在 CLI 中运行。为了测试打印功能,我加载了 PHP_printer.dll 并且我正在打印到 Onenote,一个打印到文件选项,使用在 PRINTER_ENUM_LOCAL 中给出的确切打印机名称。
更新:这是最新的代码:
$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?
我已经使用此代码将空白页打印到正确的打印机,但是我无法打印传递给printer_write 的字符串。我确认$contents
已正确填充了我的测试 html 文件的内容。无论我提供什么作为第二个参数(要打印的字符串),我都会得到一个空白页。有什么我想念的东西至少可以让我在页面上打印一些文本吗?
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.
或者有没有更好的方法来做到这一点(使用 PHP/javascript 文件)?我想要做的是通过 CLI 应用程序打印出现的网页(包括 CSS),该网站是用 PHP 编写的,我试图将复杂性降至最低。如果有更好的方法来打印这些(显然转换为 PDF 和打印是一个选项)我是开放的,但听起来这是 PHP 中最简单/事实上的方法。
采纳答案by bumperbox
i messed around with printer.dll for ages and got sick of it.
我弄乱了printer.dll很长时间并且厌倦了它。
not sure if its much use, but i purchased this application http://www.coolutils.com/TotalPDFPrinter
不确定它是否有很多用处,但我购买了这个应用程序 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.
它让我从 php 的 cmd 行打印 pdf 文件,这对我来说效果很好,但它确实有一个弹出屏幕,只有在您购买服务器版本时才能摆脱(如果您的 apache 正在运行,您将需要它)作为服务)。显然,非弹出版本要贵得多。
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.
我挣扎的一件事是获取可用的打印机列表,所以我用 C# 编写了这个小应用程序,它以纯文本形式输出打印机列表,然后使用 php 调用它并将它们放入我的网络上的下拉列表中形式。
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());
}
}
}
}
回答by AleksanderKseniya
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...
据我所知,你应该使用php_printer.dll
. 检查这里。然后,将 php_printer.dll 扩展添加到您的 php.ini。如果你这样做了,那么......
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)
如果您有资格并清楚地将 ip/route 放在安装打印机的位置,它应该可以解决问题。类似的东西printer_open('\\\\xx.x.xx.xx\\_printername_');
。(本地打印机不需要手动添加服务器,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.
要检查打印机,请尝试类似printer_list(PRINTER_ENUM_LOCAL | PRINTER_ENUM_SHARED)
. printer.default_printer
如果我没记错的话,php.ini 上有类似指令。很久以前在 php 5.x 安装上对此进行了测试。祝你好运。