PHP code example of rafaelcecchin / cups-printer

1. Go to this page and download the library: Download rafaelcecchin/cups-printer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rafaelcecchin / cups-printer example snippets


use RafaelCecchin\CupsPrinter\CupsPrinter;

$printerServer = '192.168.0.100';
$printerName = 'L8360CDW-Rafael';

$printer = new CupsPrinter($printerServer, $printerName);

/* Print an existing file */
$fileLocation = '/var/docs/test.pdf';
$printer->printFile($fileLocation);

/* Save the content to a temporary file before printing */
$output = $pdf->output();
$printer->printData($output);

/* Save the buffer content to a temporary file before printing */
$print = $printer->obPrint(function () use ($pdf) {
    $pdf->stream("dompdf_out.pdf", array("Attachment" => false));
});

use RafaelCecchin\CupsPrinter\CupsPrinter;

class Printer extends CupsPrinter
{
    function __construct(String $printerServer, String $printerName)
    {
        parent::__construct($printerServer, $printerName);
    }

    public function cmdPrintCommand(String $fileLocation)
    {
        $env = getenv('APP_ENV');
        $cmd = '';

        if ($env == 'DEV') {
            $cmd .= "ubuntu run ";
            $fileLocation = $this->convertWindowsPathToUnix($fileLocation);
        }

        $cmd .= "lp -h $this->printerServer -d $this->printerName $fileLocation";
        
        return $cmd;
    }

    private function convertWindowsPathToUnix(String $fileLocation)
    {
        $fileLocation = str_replace('\\', '/', $fileLocation);
        $fileLocation = preg_replace_callback(
            '/^([a-zA-Z]):/',
            function ($matches) {
                return '/mnt/' . strtolower($matches[1]);
            },
            $fileLocation
        );

        return $fileLocation;
    }
}