使用PHP打印到打印机 [英] Printing to printers in PHP

查看:1259
本文介绍了使用PHP打印到打印机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个CLI PHP应用程序,以将一组网页打印到默认或指定的打印机上.我在Windows 7机器上,PHP 5.2.11在CLI中运行.为了测试打印功能,我已经加载了PHP_printer.dll,并使用PRINTER_ENUM_LOCAL中指定的确切打印机名称打印到Onenote,即打印到文件"选项. 更新:这是最新的代码:

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);

我已经获得了这段代码,可以将空白页打印到正确的打印机上,但是我无法打印传递给printer_write的字符串.我确认$contents已正确填充我的测试html文件的内容.无论我作为第二个arg(要打印的字符串)提供什么,我都会得到一个空白页.我是否缺少某些东西,至少可以让我在页面上打印一些文本?

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?

或者有更好的方法(使用PHP/javascript文件)吗?我想做的是通过CLI应用程序打印显示的网页(包括CSS),该网站是用PHP编写的,并且我试图将复杂性降到最低.如果有更好的方法来打印这些文件(显然可以选择转换为PDF并进行打印),那么我是开放的,但是听起来这是PHP中最简单/事实上的方法.

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.

推荐答案

我花了很长时间才弄混了printer.dll并厌倦了它.

i messed around with printer.dll for ages and got sick of it.

不知道它的用途是否广泛,但我购买了此应用程序 http://www.coolutils.com/TotalPDFPrinter

not sure if its much use, but i purchased this application http://www.coolutils.com/TotalPDFPrinter

它使我可以从php的cmd行中打印pdf文件,这对我来说效果很好,但是它确实有一个弹出屏幕,如果您购买服务器版本,则只能删除该屏幕(如果您购买了服务器版本,则需要此屏幕) apache作为服务运行).显然,非弹出式版本要昂贵得多.

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.

我苦苦挣扎的事情是获得了可用的打印机列表,所以我用C#编写了这个小应用程序,以纯文本形式显示了打印机列表,然后使用php进行调用并将其放入下拉列表中在我的网络表单上.

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());
            }
        }
    }
}

这篇关于使用PHP打印到打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆