Linux 从 PHP 应用程序通过网络打印

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5694144/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:41:36  来源:igfitidea点击:

printing over network from PHP app

phplinuxprintingcupsnetwork-printers

提问by user160108

I have a set of printers connect over a network with Static IP assigned to each printer.

我有一组打印机通过网络连接,静态 IP 分配给每台打印机。

Now i have a PHP web application running on a linux server which needs to send print jobs, to these printer over the network.

现在我有一个运行在 Linux 服务器上的 PHP Web 应用程序,它需要通过网络将打印作业发送到这些打印机。

Is this possible using lpr or cups and how do i go about it.

这是否可以使用 lpr 或 cups,我该怎么做。

采纳答案by Nirmal

You could use the LPR Printer class from here:

您可以从这里使用 LPR 打印机类:

http://www.phpclasses.org/package/2540-PHP-Abstraction-for-printing-documents.html

http://www.phpclasses.org/package/2540-PHP-Abstraction-for-printing-documents.html

Example:

例子:

<?php 
include("PrintSend.php");
include("PrintSendLPR.php");

$lpr = new PrintSendLPR(); 
$lpr->setHost("10.0.0.17"); //Put your printer IP here 
$lpr->setData("C:\wampp2\htdocs\print\test.txt"); //Path to file, OR string to print. 

$lpr->printJob("someQueue"); //If your printer has a built-in printserver, it might just accept anything as a queue name.
?>

回答by Spudley

This question has been asked before. See print to a network printer using PHP

这个问题以前有人问过。请参阅使用 PHP 打印到网络打印机

The answer given that time was exec("lpr -P 'printer' -r 'filename.txt');

那个时候给出的答案是 exec("lpr -P 'printer' -r 'filename.txt');

However, the answer was never accepted so not sure whether the OP found it helpful; it certainly looks like it ought to do the trick, but it's not quite a direct and easy method of doing it from within PHP.

但是,答案从未被接受,因此不确定 OP 是否认为它有帮助;它当然看起来应该可以解决问题,但从 PHP 内部实现它并不是一种直接且简单的方法。

A number of other resources I found were also recommending variations on this approach.

我发现的许多其他资源也推荐了这种方法的变体。

Digging a bit deeper, I see PHP has got a Printer module in PECL. However it's only for Windows, and looks like it's not well maintained. But in case it helps, the link it here: http://www.php.net/manual/en/intro.printer.php

再深入一点,我发现 PHP 在 PECL 中有一个打印机模块。但是,它仅适用于 Windows,并且看起来维护得不好。但如果有帮助,请在此处链接:http: //www.php.net/manual/en/intro.printer.php

I think the answer ultimately is that PHP isn't really designed for this kind of thing, and doesn't have built-in functionality to do it. But since you can shell out to external commands using exec()and similar, it shouldn't be too hard to get it working, albeit not quite ideal.

我认为最终的答案是 PHP 并不是真正为这种事情设计的,并且没有内置的功能来做到这一点。但是,由于您可以使用exec()和 类似的命令来执行外部命令,因此让它工作应该不会太难,尽管不是很理想。

回答by Parteek

i was also doing research on this...and i think the below written code can help you in handling printer in linux

我也在做这方面的研究……我认为下面写的代码可以帮助你在 linux 中处理打印机

<?php
$printer = "\\Pserver.php.net\printername");
if($ph = printer_open($printer))
{
   // Get file contents
   $fh = fopen("filename.ext", "rb");
   $content = fread($fh, filesize("filename.ext"));
   fclose($fh);

   // Set print mode to RAW and send PDF to printer
   printer_set_option($ph, PRINTER_MODE, "RAW");
   printer_write($ph, $content);
   printer_close($ph);
}
else "Couldn't connect...";
?>

回答by Harikrishnan

Try PHP::PRINT::IPP

试试PHP::PRINT::IPP

It worked perfectly for me.

它非常适合我。

Basic Usage

基本用法

 <?php
  require_once(PrintIPP.php);

  $ipp = new PrintIPP();                        
  $ipp->setHost("localhost");
  $ipp->setPrinterURI("/printers/epson");
  $ipp->setData("./testfiles/test-utf8.txt"); // Path to file.
  $ipp->printJob();                                                          
?>

Reference

参考