【问题标题】:PHP Printing with Google Cloud Print使用 Google 云打印进行 PHP 打印
【发布时间】:2012-01-26 07:53:37
【问题描述】:

我目前正在向 php 后端系统添加功能以允许其直接打印,并且我正在尝试使用 Google 的云打印来处理。将应用想象成一个在线购物车,我希望它打印提货单(已完成的订单),而不需要有人登录。服务器是远程的,目的地有云就绪打印机。

到目前为止,我已经成功地使用interfaces 打印它,只要我只是将 HTML、纯文本或 URL 传递给 PDF。我可以将打印设置为彩色、无边距和打印质量。

但是我遇到的问题是,系统创建的 PDF 不可公开访问,因此我无法将 URL 传递给文件,我需要传递文件的内容。

我一直在尝试修改我在网络上找到的示例之一 HERE,但没有成功。但是我不懂这种语言,所以我很挣扎。

python HERE 中的另一个例子我一直在尝试但没有成功!

我正在使用 PHP 和 Zend 框架来处理界面。这是我尝试过的一个示例,减少到我试图准备要发送的文件的位置,就像我说我不确定从 python 转换为 php,或者 python 脚本是否有效,但这就是我想出了:

<?php
// Test print a job:
$b64_pathname = PDF_PATH.'ec22c3.pdf'.'.b64';
$fileType = "application/pdf";
// Open the original file and base64 encode it:
$dataHandle = fopen(PDF_PATH.'ec22c3.pdf', "rb");
$dataContent = fread($dataHandle, filesize(PDF_PATH.'ec22ed167763a15e8591a3776f3c65c3.pdf'));
fclose($dataHandle);
$b64data = $fileType.base64_encode($dataContent);
// Store the base64 encoded file:
$ourFileHandle = fopen($b64_pathname, 'w');
fwrite($ourFileHandle, $b64data);
fclose($ourFileHandle);
// Read the contents of the base64 encoded file and delete it:
$fileHandle = fopen($b64_pathname, "rb");
$fileContent = fread($fileHandle, filesize($b64_pathname));
fclose($fileHandle);
unlink($b64_pathname);
// URL encode the file contents:
$file = urlencode($fileContent);
// Add the file and send to the printer:
$client->setParameterPost('content', $file);
$client->setParameterPost('contentType', $fileType);
$client->request(Zend_Http_Client::POST);
?>

【问题讨论】:

    标签: php python google-cloud-print


    【解决方案1】:

    为了发送 base64 编码的内容,您需要在提交请求中发送另一个参数: $client->setParameterPost('contentTransferEncoding', 'base64');

    【讨论】:

      【解决方案2】:

      这是一个使用 cUrl 的 php 方法(注意,我有对象级变量,称为 _auth、_username、_password 和 _printerId)。

      首先,构建一个使用 cUrl 发布的函数:

      function processRequest($url, $postFields, $referer) {  
          $ret = "";
          $ch = curl_init(); 
      
          curl_setopt($ch, CURLOPT_URL,$url);
          curl_setopt($ch, CURLOPT_USERAGENT, "");
          if(!is_null($postFields)) {
              curl_setopt($ch, CURLOPT_POST, 1);
              curl_setopt($ch, CURLOPT_POSTFIELDS,
                   $postFields);
              // http_build_query() will properly escape the fields and
              //  build a query string.
          }
      
          if(strlen($this->_auth) > 0) {
              $headers = array(
              "Authorization: GoogleLogin auth=". $this->_auth,
              //"GData-Version: 3.0",
              "X-CloudPrint-Proxy", "yourappname"
              );
              curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
          }
      
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
          curl_setopt($ch, CURLOPT_REFERER, $referer);
          curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);    
      
          $ret = curl_exec ($ch);
      
          curl_close ($ch); 
      
          return $ret;
      }
      

      然后,一个针对谷歌授权的函数:

        public function authorize() {
          $url = "https://www.google.com/accounts/ClientLogin";
          $post = array("accountType" => "HOSTED_OR_GOOGLE",
          "Email" => $this->_username,
          "Passwd" => $this->_password,
          "service" => "cloudprint",
          "source" => "yourappname");
          $resp = $this->processRequest($url, $post, "");
      
          preg_match("/Auth=([a-z0-9_\-]+)/i", $resp, $matches);
          $this->_auth = $matches[1];
        }
      

      最后,构建一个提交到云打印机的函数:

      function printDocument($title, $docBytes)
      {
          $url = "http://www.google.com/cloudprint/submit?printerid=". $this->_printerId."&output=json";
      
          $post = array(
              "printerid" => $this->_printerId,
              "capabilities" => "",
              "contentType" => "dataUrl",
              "title" => $title,
              "content" => 'data:application/pdf;base64,'. base64_encode($docBytes)
          );
      
          $ret = $this->processRequest($url, $post, "");
      
          echo $ret;
      }
      

      在使用中,调用authorize()获取认证令牌。然后只需将您的文件(从任何地方)读入一个变量并将其传递给带有标题的 printDocument。

      【讨论】:

      • Auth 变量中有什么?
      猜你喜欢
      • 2012-04-12
      • 2013-09-02
      • 2016-02-17
      • 2012-04-07
      • 2011-12-14
      • 1970-01-01
      • 2020-09-08
      • 2011-12-12
      • 2014-11-22
      相关资源
      最近更新 更多