dozr162106 2019-08-11 13:30
浏览 61

如何在Google Cloud Print PHP SDK上指定媒体设置

Im trying to print to our printers connected to Google Cloud Print via Google Chrome using PHP SDK. The Printer we are trying to print is a Thermal Printer and we use the same code to print on other Thermal Printer without any issues, the issue we had is specific to this Printer http://www.zjiang.com/en/init.php/product/index?id=42

We had installed the said PHP SDK and tried to send a URL to print, and we are getting the response as An error occurred while printing the doc. Error code:

403 Message:Could not convert to PDF.

This is due to the Layout error, as we understand from here, So i'm looking on how to specify this Layout settings on https://github.com/yasirsiddiqui/php-google-cloud-print SDK.

Here is the function which is doing that,

public function sendPrintToPrinter($printerid,$printjobtitle,$filepath,$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");   
    }
    // Open the file which needs to be print
    $handle = fopen($filepath, "rb");
    if(!$handle)
    {
        // Can't locate file so throw exception
        throw new Exception("Could not read the file. Please check file path.");
    }
    // Read file content
    $contents = file_get_contents($filepath);

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

        'printerid' => $printerid,
        'title' => $printjobtitle,
        'contentTransferEncoding' => 'base64',
        'content' => base64_encode($contents), // 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 expect the print to go smoothly to the said printers without any errors, since we use the same code to print on almost all printers expect these.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 kali环境运行volatility分析android内存文件,缺profile
    • ¥15 写uniapp时遇到的问题
    • ¥15 vs 2008 安装遇到问题
    • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
    • ¥15 找一个网络防御专家,外包的
    • ¥100 能不能让两张不同的图片md5值一样,(有尝)
    • ¥15 informer代码训练自己的数据集,改参数怎么改
    • ¥15 请看一下,学校实验要求,我需要具体代码
    • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
    • ¥20 MATLAB绘制两隐函数曲面的交线