0

Are there ANY code examples of how to use Google Cloud Print (using the new OAuth 2) and how when a document comes into the Google Cloud Print queue to automatically print it?

Pretty much what I am trying to do is not spend thousands of dollars that when an order is submitted to our online store, that the order automatically gets printed to our printer. Any ideas, pointers, code examples.

I have done a bunch of searching, and a lot of examples using C#, use Google's old service, not the OAuth2, documentation.

Pretty much, I need a service that will sent a print command to our printer when we get an order in. I can write the part from the store to the service, it is the service to the printer part I have a ton of trouble with.

Thanks in advance.

ClosDesign
  • 3,894
  • 9
  • 39
  • 62

2 Answers2

0

There's a brilliant PHP class you can download and use that does exactly that:

https://github.com/yasirsiddiqui/php-google-cloud-print

MKC
  • 186
  • 1
  • 15
0

The problem with what you want to achieve is that Google Cloud Print is meant for authenticated users submitting their own print jobs. If I understand correctly, you want to have the server submit a print job as a callback after receiving an order. Therefore, print jobs need to be submitted by a service account, not a Google user. This can be done (we use it in production at the company I work for) using a little hack, described here:

Share printer with Google API Service Account

I can't help you with C# or PHP code, basically you need to be able to make JWT authenticated calls to Google Cloud Print, here you are a code snippet in NodeJS, hope it helps:

var request = require('google-oauth-jwt').requestWithJWT();

service.submitJob = function(readStream,callback) {
    // Build multipart form data
    var formData = {
      printerid: cloudPrintConfig.googleId,
      title: 'My Title', 
      content: readStream,
      contentType: "application/pdf",
      tag: 'My tag', 
      'ticket[version]': '1.0',
      'ticket[print]': ''
    };
    // Submit POST request
    request({
      uri: cloudPrintConfig.endpoints.submit, 
      json: true,
      method: 'post',
      formData: formData,
      jwt: cloudPrintConfig.jwt
    }, function (err, res, body) {
      if (err) {
        callback(err,null);
      } else {
        if (body.success == false) {
          callback('unsuccessful submission',null);
        } else {
          callback(null, body);
        }
      }
    });
  }

Details about JWT credentials can be found here

Community
  • 1
  • 1
Halogen
  • 131
  • 2
  • 9