2011-12-22 120 views
4

我目前正在向php后端系统添加该功能以允许其直接打印,并且我正尝试使用Google的云打印工作。将该应用想象为一个在线购物车,并且我希望它能够打印采摘备忘录(已完成订单),而无需登录。服务器是远程的,目的地是Cloud Ready打印机。使用Google云打印进行PHP打印

到目前为止,只要我简单地将HTML,纯文本或URL传递到PDF,我就能成功地使用interfaces进行打印。我可以将打印设置为彩色,无边距和打印质量。

但是,当我遇到问题时,系统创建的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); 
?> 
+0

我会试试看。 – Cymbals 2012-04-11 21:35:44

回答

1

为了你需要发送的另一个参数提交请求发送base64编码的内容: $客户 - > setParameterPost(“contentTransferEncoding”,“的base64”);

2

下面是使用cUrl(注意,我有对象级变量称为_auth,_username,_password & _printerId)的方法。

首先,建立一个功能后的卷曲度:

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并添加标题即可。

+0

什么是Auth变量? – 2013-03-25 09:07:33

+0

@MatthewDolman'$ this - > _ auth = $ matches [1];' – Armin 2015-09-26 17:58:50