Printing PHP with Google Cloud Print

I am currently adding an option for an internal php system that allows it to print directly, and I'm trying to get it to work with a Google cloud printer. Imagine that the application is an online store, and I want it to print notes (completed orders) without having to log in. The server is remote, and there are cloud printers at the destination.

So far, I have been able to print it using interfaces , while I just skip HTML, plain text, or a PDF URL. I can set the print to color, borderless and print quality. A.

However, when I run into a problem, the PDF that the system creates is not publicly available, so I cannot transfer the URL to the file, I need to transfer the contents of the file.

I tried without success to change one of the examples that I found on the Internet HERE . However, I do not know the language, so I struggle with it.

Another example in python HERE again I tried without success!

I use PHP and the Zend platform to work with the interface. Here is one example that I tried, cut down where I am trying to prepare the file for sending, for example, I say that I am not sure about the translation from python to php, or if the python script even works, but this is what I came up with:

<?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
source share
2 answers

The php method is used here using cUrl (note that I have object level variables called _auth, _username, _password and _printerId).

First create a function to publish with 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; } 

Then, the authorization function against 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]; } 

Finally, create a function to send to the cloud printer:

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

When using, call authorize () to get the authentication token. Then just read your file (from where) into the variable and pass it to printDocument with a header.

+2
source

To send base64 encoded content, you need to send another parameter to the send request: $ client-> setParameterPost ('contentTransferEncoding', 'base64');

+1
source

Source: https://habr.com/ru/post/1387608/


All Articles