問題描述
我正在嘗試設(shè)置 CLI PHP 應(yīng)用程序以將一組網(wǎng)頁打印到默認(rèn)或指定的打印機(jī).我在 Windows 7 機(jī)器上,PHP 5.2.11 在 CLI 中運(yùn)行.為了測試打印功能,我加載了 PHP_printer.dll 并打印到 Onenote,這是一個打印到文件選項,使用 PRINTER_ENUM_LOCAL 中給出的確切打印機(jī)名稱.
更新:這是最新的代碼:
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);
我已獲得此代碼以將空白頁打印到正確的打印機(jī),但我無法打印傳遞給printer_write 的字符串.我確認(rèn) $contents
正確填充了我的測試 html 文件的內(nèi)容.無論我提供什么作為第二個 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 應(yīng)用程序打印出現(xiàn)的網(wǎng)頁(包括 CSS),該網(wǎng)站是用 PHP 編寫的,我試圖將復(fù)雜性降至最低.如果有更好的方法來打印這些(顯然轉(zhuǎn)換為 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.
不確定它是否有多大用處,但我購買了此應(yīng)用程序http://www.coolutils.com/TotalPDFPrinter
not sure if its much use, but i purchased this application http://www.coolutils.com/TotalPDFPrinter
它讓我從 php 的 cmd 行打印 pdf 文件,這對我來說效果很好,但它確實有一個彈出屏幕,只有在您購買服務(wù)器版本時才能擺脫(如果您需要apache 作為服務(wù)運(yùn)行).顯然非彈出版本要貴得多.
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.
我掙扎的一件事是獲取可用的打印機(jī)列表,所以我用 C# 編寫了這個小應(yīng)用程序,它以純文本形式輸出打印機(jī)列表,然后使用 php 調(diào)用它并將它們放入下拉列表在我的網(wǎng)絡(luò)表單上.
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());
}
}
}
}
這篇關(guān)于用 PHP 打印到打印機(jī)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!