PHP Classes

PHP Google Cloud Print API: Print documents using a Google Cloud printer

Recommend this page to a friend!
  Info   View files Example   View files View files (10)   DownloadInstall with Composer Download .zip   Reputation   Support forum (7)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-09 (4 days ago) RSS 2.0 feedStarStarStarStar 75%Total: 1,167 This week: 1All time: 3,245 This week: 348Up
Version License PHP version Categories
google-cloud-print 1.21BSD License5.0PHP 5, Printing, Web services
Description 

Author

This class can print documents using a Google Cloud printer.

It can send HTTP requests to the Google Accounts API to login in a Google account using a given user email address and his password to obtain a OAuth token.

The class can use the obtained token to send HTTP requests to the Google Cloud Print API to perform several types of operations.

Currently it can retrieve the list of available cloud printers, as well send a given document file to be printed in one of the printers.

Innovation Award
PHP Programming Innovation award nominee
June 2013
Number 3


Prize: One book of choice by Packt
Google Cloud Print is a service that allows finding printers accessible via the Web and print documents using printers with authorized access.

This PHP class can be used to find and send documents to available printers using the Google Cloud Print service.

Manuel Lemos
Picture of Yasir Siddiqui
  Performance   Level  
Name: Yasir Siddiqui <contact>
Classes: 14 packages by
Country: Pakistan Pakistan
Innovation award
Innovation award
Nominee: 8x

Winner: 1x

Recommendations

Example

<?php

// To add printers to your account follow the following link
// https://support.google.com/cloudprint/answer/1686197
/**
 * PHP implementation of Google Cloud Print
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */


require_once 'GoogleCloudPrint.php';

session_start();
// Create object
$gcp = new GoogleCloudPrint();
$gcp->setAuthToken($_SESSION['accessToken']);

$printers = $gcp->getPrinters();
//print_r($printers);

$printerid = "";
if(
count($printers)==0) {
   
    echo
"Could not get printers";
    exit;
}
else {
   
   
$printerid = $printers[0]['id']; // Pass id of any printer to be used for print
    // Send document to the printer
   
$resarray = $gcp->sendPrintToPrinter($printerid, "Printing Doc using Google Cloud Printing", "./pdf.pdf", "application/pdf");
   
    if(
$resarray['status']==true) {
       
        echo
"Document has been sent to printer and should print shortly.";
    }
    else {
        echo
"An error occured while printing the doc. Error code:".$resarray['errorcode']." Message:".$resarray['errormessage'];
    }
}


Details

Google Cloud Print

PHP class to print documents using Google Cloud Print using OAuth2.

First of all you have to add printers to Google Cloud Print using your Gmail account and Google Chrome browser. Follow the instructions on the following link to add printer to Google Cloud Print

https://support.google.com/cloudprint/answer/1686197

Google OAuth Prerequisites

1) Create Google API project and get OAuth credentials.

Create Google OAuth Credentials

1) Create new project and get the corresponding OAuth credentials using Google developer console https://console.developers.google.com/

2) Select APIS & AUTH –> credentials from the left menu.

3) Click Create new Client ID button. A popup will appear. In Authorized redirect URIs text area enter url that should point to oAuthRedirect.php on your server.

4) After submitting this form, we can get the client Id, secret key etc.

Replace client_id, client_secret values in $redirectConfig and $authConfig arrays in Config.php file.

You also need to replace redirect_uri in both $redirectConfig and $authConfig arrays. This URL should point to oAuthRedirect.php on your server.

For Online Access hit index.php

Online access requires authorization every time you need to send print to printer once token in Session gets expired.

For Offline Access (when you want to send prints without user presence) hit offlineAccess.php

Offline access requires authorization only once or unless user has revoked access. You should use offline access when you want to send prints to printer with out the presence of user or send prints to printer using a cron job script.

Once you hit offlineAccess.php you will be redirected to offlineToken.php which will show you your refresh token.

You need to save refresh token to database, file or some cache systems as later on when you will send print to printer in offline mode you need to replace that refrsh token at line # 29 on cron.php


  Files folder image Files  
File Role Description
Accessible without login Plain text file Config.php Conf. Configuration script
Accessible without login Plain text file cron.php Example Example script
Accessible without login Plain text file example.php Example Example script
Plain text file GoogleCloudPrint.php Class Class source
Plain text file HttpRequest.Class.php Class Class source
Accessible without login Plain text file index.php Example Example script
Plain text file oAuthRedirect.php Class Class source
Accessible without login Plain text file offlineAccess.php Appl. Application script
Accessible without login Plain text file offlineToken.php Appl. Application script
Accessible without login Plain text file README.md Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:1,167
This week:1
All time:3,245
This week:348Up
User Ratings User Comments (2)
 All time
Utility:100%StarStarStarStarStarStar
Consistency:93%StarStarStarStarStar
Documentation:75%StarStarStarStar
Examples:87%StarStarStarStarStar
Tests:-
Videos:-
Overall:75%StarStarStarStar
Rank:130