How to use my owner class in Laravel?

Create a php class file in App\Libraries, eg. CupsPrinter.php

namespace App\Libraries;

use Smalot\Cups\Builder\Builder;
use Smalot\Cups\Manager\PrinterManager;
use Smalot\Cups\Transport\Client;
use Smalot\Cups\Transport\ResponseParser;
use Smalot\Cups\Manager\JobManager;
use Smalot\Cups\Model\Job;

class CupsPrinter
{
    private $cups_user;
    private $cups_pass;
    private $cups_ipp_uri;

    function __construct($cups_user, $cups_pass, $cups_ipp_uri)
    {
        $this->cups_user = $cups_user;
        $this->cups_pass = $cups_pass;
        $this->cups_ipp_uri = $cups_ipp_uri;
    }

    /**
     * cups print pdf function
     *
     * @param [type] $job_name
     * @param [type] $file_path
     * @param [type] $printer_name, cups printer name
     * @return true or failed message(string)
     */
    public function print($job_name, $file_path, $printer_name, $page_range = 1)
    {
        try {
            $client = new Client($this->cups_user, $this->cups_pass);
            $builder = new Builder();
            $responseParser = new ResponseParser();

            $printerManager = new PrinterManager($builder, $client, $responseParser);
            $printer = $printerManager->findByUri($this->cups_ipp_uri . '/' . $printer_name);
            $jobManager = new JobManager($builder, $client, $responseParser);

            $job = new Job();
            $job->setName('Print ' . $job_name);

Use it in any controller.php:

            $cups_printer = new CupsPrinter(env('CUPS_USER'), env('CUPS_PASS'), env('CUPS_IPP_URI'));

            $cups_printer->print('PDF Label', $path_for_pdf_file, $plant->cups_a4_printer_name);

Leave a Reply

Your email address will not be published. Required fields are marked *