我目前正在增加php后端系统的功能,允许它直接打印,并且我正在尝试使用Google的Cloud。想象一下,这个应用程序是一个在线购物车,我希望它可以打印笔记(已完成的订单),而不需要有人登录。服务器是远程的,目标有云准备打印机。
到目前为止,我已经成功地让它使用接口打印,只要我只是将HTML、纯文本或URL传递给PDF。我能够将打印设置成彩色,无边际和打印质量。
然而,我遇到问题的地方是,系统创建的PDF是不可公开访问的,因此我无法将URL传递给文件,我需要传递文件的内容。
我一直试图修改我在网络这里上找到的一个例子,但没有成功。然而,我不懂这门语言,所以我在挣扎。
在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);
?>发布于 2012-07-07 16:23:24
下面是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;
}然后,一个针对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。
发布于 2012-03-15 20:51:08
为了发送$client->setParameterPost('contentTransferEncoding',编码的内容,您需要在提交请求中发送另一个参数: base64 'base64');
https://stackoverflow.com/questions/8607845
复制相似问题