• 8

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

printing over network from PHP app

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

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

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

You could use the LPR Printer class from here:




$lpr = new PrintSendLPR(); 
$lpr->setHost(""); //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.
  • 10
Reply Report
    • I would have given you +1, but since the question specifies Linux and your example code uses a Windows directory path, I'm not sure. Can you confirm that this class works in Linux?
      • 1
    • @spudley thanks for the information will have to test the same with my network printer. I was also having a look at google cloud print which looks promising.
      • 2
    • @Spudley - The creator of that class didn't mention any specifics about Windows. So it must work on Linux too. Since I don't know or work on Linux, that directory path is all I understand.
      • 1
    • Fair enough. I'll give you your +1 ;-) I don't have time to check the class for myself as the moment though.

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

The answer given that time was 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.

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

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.

  • 5
Reply Report

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

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

   // Set print mode to RAW and send PDF to printer
   printer_set_option($ph, PRINTER_MODE, "RAW");
   printer_write($ph, $content);
else "Couldn't connect...";
  • -1
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions