Automatically Print Files Placed in Drive with Google Cloud Print and Apps Script

With Google Cloud Print, you can manage your printers and create print jobs from any computer or mobile device even if they are not connected to the printer or they are on a different network. Cloud Printer also includes a special “Save to Google Docs” virtual printer that lets you save web pages and documents as PDFs in your Google Drive.

In this tutorial, you’ll learn how to use the Google Cloud Print API from Google Apps Scripts. You can use such a system to enable auto-printing, i.e., print a file as soon as it is uploaded in your Google Drive or print an email thread from Gmail. Remote printing is also possible via Dropbox but Cloud Print runs on Google Servers and all you need is a Mac or Windows computer running the Chrome browser (the connector).

To get started, create a new Google Apps Script project and include the OAuth2 library. Next go to the developer console of that project and create a new web application. Set the Redirect URL as below and set the allowed domains as script.google.com (the Project Key is under File – Project Properties).

Add this code to the Apps Script project, replace the Client ID and Client Secret with the actual values from Developer Console and save the project. Go to Run – ShowURL and authorize the script. Open the Logger (Cmd + Enter), copy the URL and paste it in a new browser tab to complete the authorization.

Now that your Cloud Print API script is authorized, we can send print jobs or run queries. For instance, if you are to retrieve a list of printers connected to Google Cloud Print, the code would be something like this:

Similarly, to print a file in your Google Drive, you can write a routine like this. The docID is the ID of your Google Document that resides in Drive.

These can be extended to print web pages, images or any file that is preset in your Google Drive. You’ll have to modify the “content-Type” based on the mimeType of the file. The valid values are:

If you looking for a automated system to email Google Spreadsheets as PDF, Google Cloud Print maybe too complex a solution, try this alternative.