PHP 使用 Google Cloud Print 打印

4 投票
2 回答
4909 浏览
提问于 2025-04-17 08:51

我现在正在给一个PHP后台系统添加直接打印的功能,想让它能和谷歌的云打印服务配合使用。可以把这个应用想象成一个在线购物车,我希望它能自动打印出拣货单(也就是完成的订单),而不需要有人登录。服务器是远程的,打印机是支持云打印的。

到目前为止,我已经成功让它通过接口打印,只要我传送的是HTML、纯文本或者PDF的链接就可以。我可以设置打印为彩色、无边距以及打印质量。

但是我遇到的问题是,系统生成的PDF文件是不能公开访问的,所以我不能传送文件的链接,我需要传送文件的内容。

我尝试修改网上找到的一个例子在这里,但因为我不懂那种语言,所以一直没成功。

还有一个用Python写的例子在这里,我也尝试过,但同样没有成功!

我使用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);
?>

2 个回答

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;
}

接着,创建一个用来向Google进行授权的函数:

  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函数即可。

撰写回答