PHP Server Side Printing Ubuntu Server

后端 未结 3 1184
北荒
北荒 2021-01-19 04:33

I have been all over stack looking at what is required to do this and have wound up being slightly confused.

Lets get one thing straight this is a local based intra-

相关标签:
3条回答
  • 2021-01-19 05:12

    If CUPS is configured properly, printing a PDF from the shell is literally as easy as

    lpr myfile.pdf
    

    So, once you have written your PDF to a temporary file, you can use any of the available PHP functions to execute that shell command: exec(), shell_exec(), system()

    You could even do it without writing a temporary file and feed the data directly to lpr via STDIN (try cat myfile.pdf | lpr as an example on the shell).

    You can feed data to a program's STDIN in PHP if you run it using proc_open(). The first example from the PHP Manual can be adapted to something like this:

    <?php
    $descriptorspec = array(
       0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
    );
    
    $process = proc_open('lpr', $descriptorspec, $pipes);
    
    if (is_resource($process)) {
        // $pipes now looks like this:
        // 0 => writeable handle connected to child stdin
        // 1 => readable handle connected to child stdout
        // Any error output will be appended to /tmp/error-output.txt
    
        fwrite($pipes[0], $pdf_data);
        fclose($pipes[0]);
    }
    ?>
    
    0 讨论(0)
  • 2021-01-19 05:12

    Use PHP::PRINT::IPP

    It is the most secure and easiest way for printing from Web using PHP. Here you don't have to enable exploitable php functions like exec(), shell_exec() etc.

    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

    0 讨论(0)
  • 2021-01-19 05:20

    Judicious use of php's shell_exec() should allow you to print PDFs synchronously, immediately after their creation, thus avoiding the need for bash.

    I've not used shell_exec() for printing so can't help with the detail but essentially, if you can successfully compose a UNIX print command, then you can write the shell_exec() instruction.

    0 讨论(0)
提交回复
热议问题