Note

After December 31, 2020, Google Cloud Print will be no longer supported.
https://www.google.com/intl/en-GB_ALL/cloudprint/learn/
Submit Print Jobs via Google Cloud Print Service
Prints files attached as workflow data (via Google Cloud Print).
2018 © Questetra, Inc. (MIT License)
Configs
  • A: Set OAuth2 Config Name (at [OAuth 2.0 Setting]) *
  • B: Set Printer Id (e.g. “a2c4e6g8-…”) * #{EL}
  • C: Select FILES DATA for Print (PDF, PNG, DOCX, etc) *
  • D: Set CJT Options (e.g. ‘{“version”:”1.0″,”print”:{}}’) #{EL}
Script
// OAuth2 Example Setting:
// <Google Developers Console>
// Application Type: Web Application
// Name: questetra-print
// Redirect URL: https://s.questetra.net/oauth2callback
// <Questetra OAuth2.0 Setting of Workflow App>
// Name: google-cloud-print
// Authorization Endpoint URL: https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force
// Token Endpoint URL: https://accounts.google.com/o/oauth2/token
// Scope: https://www.googleapis.com/auth/cloudprint
// Consumer Key: (Copy from Google Developers Console)
// Consumer Secret: (Copy from Google Developers Console)
/// https://console.developers.google.com/apis/credentials

// PrinterID: Get via "chrome://devices/"
// Cloud Job Ticket (CJT):
// REF https://developers.google.com/cloud-print/docs/cdd#cjt
// If not specified, it is printed with the printer's default setting '{"version": "1.0", "print": {}}'
// Other Examples
// {"version":"1.0","print":{"vendor_ticket_item":[],"duplex":{"type":"LONG_EDGE"}}}
// {"version":"1.0","print":{"vendor_ticket_item":[],"color":{"type":"STANDARD_MONOCHROME"},"copies":{"copies": 2}}}

// Supported File Types:
// "text/html", "text/plain", "application/postscript",,,
// "application/pdf", "image/jpeg", "image/png",,,
// "application/vnd.openxmlformats-officedocument.wordprocessingml.document" (docx),,,
// "application/vnd.openxmlformats-officedocument.presentationml.presentation" (pptx),,,
// REF https://developers.google.com/cloud-print/docs/appDevGuide
// REF https://developers.google.com/cloud-print/docs/appInterfaces



//////// START "main()" ////////
main();
function main(){ 

//// == Config Retrieving / 工程コンフィグの参照 ==
const dataIdC = configs.get( "conf_DataIdC" ) + "";
// 'java.lang.String' (String Obj) to javascript primitive 'string'
// https://wiki.openjdk.java.net/display/Nashorn/Rhino+Migration+Guide#RhinoMigrationGuide-JavaScriptvsJavaStrings
// https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/api.html#sthref21

const oauth2    = configs.get( "conf_OAuth2" ) + "";
const printerId = configs.get( "conf_PrinterId" ) + "";
let   jobTicket = configs.get( "conf_JobTicket" ) + "";
  if( jobTicket === "" ){
    jobTicket = '{"version": "1.0", "print": {}}';
  }


//// == Data Retrieving / ワークフローデータの参照 ==
const myFiles   = engine.findDataByNumber( dataIdC ); // java.util.ArrayList
// com.questetra.bpms.core.event.scripttask.NewQfile
if( myFiles === null ){
  throw new Error( "Files data is NULL" );
}

//// == Calculating / 演算 ==
const jobTitle  = "Questetra-" + processInstance.getProcessInstanceId() + "";
const token     = httpClient.getOAuth2Token( oauth2 );

for( let i = 0; i < myFiles.size(); i++ ){
  // prepare API Request
  // com.questetra.bpms.core.event.scripttask.HttpClientWrapper
  let apiRequest = httpClient.begin(); // HttpRequestWrapper
  apiRequest     = apiRequest.bearer( token );
  apiRequest     = apiRequest.multipart( "title", jobTitle );
  apiRequest     = apiRequest.multipart( "printerid", printerId );
  apiRequest     = apiRequest.multipart( "ticket", jobTicket );
  apiRequest     = apiRequest.multipart( "content", myFiles.get(i) );

  // access API (with post())
  let response   = apiRequest.post( "https://www.google.com/cloudprint/submit" );
  let httpStatus = response.getStatusCode() + "";
  let responseStr= response.getResponseAsString() + "";
  engine.log( "Submit Print Job: " + jobTitle + " (" + httpStatus + ")" );
  if( httpStatus !== "200" ){
    let errorMessage = "\n- HTTP STATUS is not 200, Service Task stopped. -\n";
    errorMessage += responseStr + "\n";
    throw new Error( errorMessage );
  }
}


//// == Data Updating / ワークフローデータへの代入 ==
// (nothing)

} //////// END "main()" ////////

Download

Capture

Notes

  • All the files attached in the File-type Data Item are printed
  • File data in various formats can be printed, such as PDF, Docx
  • A delay of up to 30 seconds may occur before receiving a print job
  • It is necessary to add a printer in “chrome://devices/” in advance and acquire the printer ID (B)
    • “Cloud compatible printer” may not be displayed in “new device” in “chrome://devices/” if it is registered with someone else’s account.
    • “Conventional printers” (cloud non-compatible) will perform printing when the power to the host PC is turned on.
  • It is necessary to acquire the Client ID and Client secret for OAuth2 communication in the Google Developers Console beforehand.
  • OAuth2 communication must have been registered and authorized (allowed) on the details screen of the Workflow App
  • Print options are set in Cloud Job Ticket (CJT).
    • If not specified, it will be printed with the default setting of the printer(CJT= {“version”: “1.0”, “print”: {}} )
    • Duplex printing = {“version”:”1.0″,”print”:{“vendor_ticket_item”:[],”duplex”:{“type”:”LONG_EDGE”}}}
    • 2 copies printing = {“version”:”1.0″,”print”:{“vendor_ticket_item”:[],”color”:{“type”:”STANDARD_MONOCHROME”},”copies”:{“copies”: 2}}}
  • It is also possible to set values to the Config item “Printer ID” or “CJT printing option ” referring to Workflow data by setting Expression Language (e.g. “#{data [‘1’]}”)
%d bloggers like this: