Skip to content

Instantly share code, notes, and snippets.

@tejastank
Forked from leviathan747/gcp.py
Created October 21, 2021 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tejastank/1a9f19fc6a068d1f305bea8434156645 to your computer and use it in GitHub Desktop.
Save tejastank/1a9f19fc6a068d1f305bea8434156645 to your computer and use it in GitHub Desktop.
Python script for submitting Google Cloud Print jobs from macOS native print dialog using PDF Services.
#!/usr/local/bin/python3
# This script can be used to submit Google Cloud Print jobs from the PDF services menu in macOS
#
# Prerequisites:
# - Python 3.x
# - oauth2client installed
#
# Setup instructions:
# 1. Clone this gist into a directory called ".gcp" in your home directory. Assure
# that "gcp.py" is executable.
# 2. Create a symbolic link to "gcp.py" in the "~/Library/PDF Services" directory.
# The name of this link will be displayed in the print dialog.
# 3. Create a set of app credentials in the Google developer console. Download the
# credentials and store in "~/.gcp/client_secret.json".
# 4. Modify the value of the "PRINTER_ID" constant to be the UUID of the could printer
# you want to submit jobs to.
#
# Usage:
# 1. Print using the system print dialog.
# 2. In the "PDF" drop down, select your custom service.
# 3. The default settings print one copy in black and white. To change the print settings,
# modify "ticket.json". See documentation on the ticket format here:
# https://developers.google.com/cloud-print/docs/cdd#cjt
import sys
import os
import logging
import time
import requests
import httplib2
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run_flow
# constants
#PRINTER_ID = "__google__docs" # Save to Google Drive
PRINTER_ID = "f10fbf9c-0da8-6467-d9a4-861711e98e33" # MatchBOX cloud print printer ID
GCP_HOME = os.environ["HOME"] + "/.gcp/"
CLIENT_SECRET_FILE = GCP_HOME + "client_secret.json"
SCOPE = "https://www.googleapis.com/auth/cloudprint"
STORAGE = Storage(GCP_HOME + "credentials.storage")
CLOUDPRINT_URL = "https://www.google.com/cloudprint/submit"
# set up logging
logging.basicConfig(filename=GCP_HOME + "gcp.log", level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logging.info("Starting print job...")
# check command line arguments
if len(sys.argv) != 4:
logging.error("Incorrect number of arguments: %d", len(sys.argv))
sys.exit(1)
title = sys.argv[1]
filename = sys.argv[3]
sys.argv = [sys.argv[0]]
# authorize with google
credentials = STORAGE.get()
if credentials is None or credentials.invalid:
flow = flow_from_clientsecrets(CLIENT_SECRET_FILE, scope=SCOPE)
http = httplib2.Http()
credentials = run_flow(flow, STORAGE, http=http)
# build and submit print job
ticket = open(GCP_HOME + "ticket.json", "r")
body = {
"printerid": PRINTER_ID,
"title": [title],
"ticket": ticket.read()
}
ticket.close()
files = {"content": open(filename,"rb")}
headers = {"Authorization":"Bearer "+str(credentials.get_access_token().access_token)}
r = requests.post(CLOUDPRINT_URL, data=body, files=files, headers=headers)
logging.debug(r.text)
# done
logging.info("Done.")
{
"version": "1.0",
"print": {
"vendor_ticket_item": [],
"color": {
"type": "STANDARD_MONOCHROME"
},
"copies": {
"copies": 1
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment