4

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

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

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

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

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);
?>
4

2 回答 2

2

这是 php 中使用 cUrl 的方法(注意,我有名为 _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。

于 2012-07-07T16:23:24.743 回答
1

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

于 2012-03-15T20:51:08.627 回答