yasirsiddiqui / php-google-cloud-print

PHP class to print documents using Google Cloud Print with OAuth2 authorization
91 stars 42 forks source link

How to send html or text to printer #25

Closed abdebenjou closed 4 years ago

abdebenjou commented 8 years ago

Is it possible to send html to printer ? line 43 -> example.php $resarray = $gcp->sendPrintToPrinter($printerid, "Printing Doc using Google Cloud Printing", "./pdf.pdf", "application/pdf");

gogl92 commented 7 years ago

yes, it is possible I created this method to avoid the base64 and it works perfectly:

public function sendPrintToPrinterContent($printerid, $printjobtitle, $content, $contenttype)
    {

        // Check if we have auth token
        if (empty($this->authtoken)) {
            // We don't have auth token so throw exception
            throw new Exception("Please first login to Google by calling loginToGoogle function");
        }
        // Check if prtinter id is passed
        if (empty($printerid)) {
            // Printer id is not there so throw exception
            throw new Exception("Please provide printer ID");
        }

        // Prepare post fields for sending print
        $post_fields = array(

            'printerid' => $printerid,
            'title' => $printjobtitle,
            'contentTransferEncoding' => 'utf-8',
            'content' => $content, // encode file content as base64
            'contentType' => $contenttype
        );
        // Prepare authorization headers
        $authheaders = array(
            "Authorization: Bearer " . $this->authtoken
        );

        // Make http call for sending print Job
        $this->httpRequest->setUrl(self::PRINT_URL);
        $this->httpRequest->setPostData($post_fields);
        $this->httpRequest->setHeaders($authheaders);
        $this->httpRequest->send();
        $response = json_decode($this->httpRequest->getResponse());

        // Has document been successfully sent?
        if ($response->success == "1") {

            return array('status' => true, 'errorcode' => '', 'errormessage' => "", 'id' => $response->job->id);
        } else {

            return array('status' => false, 'errorcode' => $response->errorCode, 'errormessage' => $response->message);
        }
    }

I call the function this way:

$gcp->sendPrintToPrinterContent($printerid, "Printing Doc using Google Cloud Printing", "<b>Hello</b>", "text/html");