Google Drive: GFile; Export as PDF

Google Drive: GFile; Export as PDF

Exports the specified file in the Drive to the Workflow Data Item in PDF format. If a file other than a Google file (Docs / Sheets / Slides etc.) is specified, an error will occur (Please use File Download). File ID can be obtained from URI.

2019-09-18 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/google-drive-gfile-export-as-pdf/

Configs
  • A: Select OAuth2 Config Name (at [OAuth 2.0 Setting]) *
  • B: Set FILE-ID to Download * #{EL}
  • C: Select FILES DATA for Exported File (added) *
  • D: Set FILE NAME if you want to save as a different name #{EL}
  • E: Select STRING DATA for File Name in Drive (update)
Script (click to open)

// (c) 2019, Questetra, Inc. (the MIT License)

//// == OAuth2 Setting example ==
// 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/drive.readonly"
// Client ID:
//  ( from https://console.developers.google.com/ )
// Consumer Secret:
//  ( from https://console.developers.google.com/ )
//  *Redirect URLs: "https://s.questetra.net/oauth2callback"


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

//// == Config Retrieving / 工程コンフィグの参照 ==
const oauth2  = configs.get( "conf_OAuth2"  ) + "";
const fileId  = configs.get( "conf_FileId") + "";
const dataIdC = configs.get( "conf_DataIdC" ) + "";
let   saveAs  = configs.get( "conf_SaveAs") + "";
const dataIdE = configs.get( "conf_DataIdE" ) + "";
// 'java.lang.String' (String Obj) to javascript primitive 'string'
engine.log( " AutomatedTask Config: File ID: " + fileId );
if( fileId === "" ){
  throw new Error( "\n AutomatedTask ConfigError:" + 
                   " String {B} (File ID) is required \n" );
}
engine.log( " AutomatedTask Config: Save As: " + saveAs );


//// == Data Retrieving / ワークフローデータの参照 ==
var tmpFiles = engine.findDataByNumber( dataIdC ); // java.util.ArrayList
if( tmpFiles === null ) {
  engine.log( " AutomatedTask FilesArray {C} (empty)" );
  tmpFiles = new java.util.ArrayList();
}else{
  engine.log( " AutomatedTask FilesArray {C}: " + 
              tmpFiles.size() + " files" );
}


//// == Calculating / 演算 ==
/// obtain OAuth2 Access Token
const token   = httpClient.getOAuth2Token( oauth2 );

/// get File Name (Metadata)
let apiRequest = httpClient.begin(); // HttpRequestWrapper
apiRequest = apiRequest.bearer( token );
apiRequest = apiRequest.queryParam( "supportsAllDrives", "true" );
// This parameter will only be effective until June 1, 2020.
// see https://developers.google.com/drive/api/v3/reference/files/get
const apiUri = "https://www.googleapis.com/drive/v3/files/" + fileId;
engine.log( " AutomatedTask Trying: GET " + apiUri );
const response = apiRequest.get( apiUri );
const responseCode = response.getStatusCode() + "";
engine.log( " AutomatedTask ApiResponse: Status " + responseCode );
if( responseCode !== "200"){
  throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
         responseCode + "\n" + response.getResponseAsString() + "\n" );
}
const responseStr = response.getResponseAsString() + "";
const responseObj = JSON.parse( responseStr );
engine.log( " AutomatedTask ApiResponse: File Name: " + responseObj.name );
engine.log( " AutomatedTask ApiResponse: File MIME Type: " + responseObj.mimeType );
if( saveAs === "" ){
  saveAs = responseObj.name + ".pdf";
}

/// get PDF File
let apiRequest2 = httpClient.begin(); // HttpRequestWrapper
apiRequest2 = apiRequest2.bearer( token );
apiRequest2 = apiRequest2.queryParam( "mimeType", "application/pdf" );
const apiUri2 = "https://www.googleapis.com/drive/v3/files/" + 
                 fileId + "/export";
engine.log( " AutomatedTask Trying: GET " + apiUri2 );
const response2 = apiRequest2.get( apiUri2 );
const responseCode2 = response2.getStatusCode() + "";
engine.log( " AutomatedTask ApiResponse: Status " + responseCode2 );
if( responseCode2 !== "200"){
  throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
         responseCode2 + "\n" + response2.getResponseAsString() + "\n" );
}
const qfile = new com.questetra.bpms.core.event.scripttask.NewQfile(
    saveAs, response2.getContentType(), response2.getResponse()
  );
tmpFiles.add( qfile );


//// == Data Updating / ワークフローデータへの代入 ==
engine.setDataByNumber( dataIdC, tmpFiles );
if( dataIdE !== "" ){
  engine.setDataByNumber( dataIdE, responseObj.name );
}

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

Download

Capture

Google Drive PDF Export

Notes

  1. Supports all drives such as My Drive and Shared Drive.
  2. If a file other than a Google file (Docs / Sheets / Slides etc.) is specified, an error “fileNotExportable” will occur.
  3. fileNotExportable: Export only supports Google Docs.
  4. TimeoutException error may occur in case many pages or images.

See also

6 thoughts on “Google Drive: GFile; Export as PDF”

  1. Pingback: Google Drive: GFile, Export as Text – Questetra Support

  2. Pingback: Google Drive: Copy File – Questetra Support

  3. Pingback: Google Drive: File; Download – Questetra Support

  4. Pingback: Google Drive: GFile; Export as Microsoft-Office – Questetra Support

  5. Pingback: Google Docs: Document; Replace All Text – Questetra Support

  6. Pingback: Google Sheets: Spreadsheets Value, Update – Questetra Support

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading