Google Drive #GFile: Export as PDF

Google Drive: GFile, Export as PDF
Stores a Google file as Workflow data after converting it to a PDF file. It is also possible to change the file name. If a file other than a Google file (Docs/Sheets/Slides, etc.) is specified an error will occur.
Configs
  • U: Select HTTP_Authz Setting *
  • A1: Set FILE-ID in Drive *#{EL}
  • B1: Set File Name if you want to save as a different name#{EL}
  • B2: Select FILE DATA that stores Exported File (append) *
  • B3: Select STRING DATA that stores File Name in Drive (update)
  • B4: Select STRING DATA that stores Mime-Type in Drive (update)
Script (click to open)
// GraalJS Script (engine type: 2)

//////// START "main()" /////////////////////////////////////////////////////////////////

main();
function main(){ 

//// == Config Retrieving / 工程コンフィグの参照 ==
const strAuthzSetting     = configs.get      ( "AuthzConfU" );   /// REQUIRED
  engine.log( " AutomatedTask Config: Authz Setting: " + strAuthzSetting );
const strInputfileId      = configs.get      ( "StrConfA1" );    /// REQUIRED
  if( strInputfileId    === "" ){
    throw new Error( "\n AutomatedTask ConfigError:" +
                     " Config {A1: FileID} is empty \n" );
  }
let   strSaveas           = configs.get      ( "StrConfB1" );    // NotRequired
  if( strSaveas         === "" ){
    engine.log( " AutomatedTask ConfigWarning:" +
                " Config {B1: ExportedSaveas} is empty" );
  }
const filesPocketDownload  = configs.getObject( "SelectConfB2" ); /// REQUIRED
const strPocketNameOnDrive = configs.getObject( "SelectConfB3" ); // NotRequired
const strPocketMimetype    = configs.getObject( "SelectConfB4" ); // NotRequired


//// == Data Retrieving / ワークフローデータの参照 ==
let filesAttached = engine.findData( filesPocketDownload ); // java.util.ArrayList
if( filesAttached === null ) {
  engine.log( " AutomatedTask FilesArray {B2} (empty)" );
  filesAttached = new java.util.ArrayList();
}else{
  engine.log( " AutomatedTask FilesArray {B2}: " +
              filesAttached.size() + " files" );
}


//// == Calculating / 演算 ==
/// Gets a file's metadata by ID.
/// Google Workspace for Developers > Google Drive for Developers > v3
/// https://developers.google.com/drive/api/v3/reference/files/get
// request1, prepare
let request1Uri = "https://www.googleapis.com/drive/v3/files/" + strInputfileId;
let request1    = httpClient.begin(); // HttpRequestWrapper
    request1    = request1.authSetting( strAuthzSetting ); // with "Authorization: Bearer XX"
    // https://questetra.zendesk.com/hc/en-us/articles/360024574471-R2300#HttpRequestWrapper
    request1    = request1.queryParam( "supportsAllDrives", "true" );
// request1, try
const response1     = request1.get( request1Uri ); // HttpResponseWrapper
engine.log( " AutomatedTask ApiRequest1 Start: " + request1Uri );
const response1Code = response1.getStatusCode() + "";
const response1Body = response1.getResponseAsString() + "";
engine.log( " AutomatedTask ApiResponse Status: " + response1Code );
if( response1Code !== "200"){
  throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
                    response1Code + "\n" + response1Body + "\n" );
}
// response1, parse
/* engine.log( response1Body ); // debug
{
  "kind": "drive#file",
  "id": "0BwSW_lixFpUuQ2JyV1pqU3VzTGs",
  "name": "Questetra-Intro-en.pdf",
  "mimeType": "application/pdf"
}
*/
const response1Obj = JSON.parse( response1Body );
engine.log( " AutomatedTask ApiResponse: File Name OnDrive: " + response1Obj.name );
engine.log( " AutomatedTask ApiResponse: File MIME-Type: " + response1Obj.mimeType );
if( strSaveas === "" ){
  strSaveas   = response1Obj.name + ".pdf";
}

/// Export as PDF from Google Drive to BPMS.
/// Google Workspace for Developers > Google Drive for Developers > v3
/// https://developers.google.com/drive/api/v3/reference/files/export
let request2Uri = "https://www.googleapis.com/drive/v3/files/" + strInputfileId + "/export";
let request2    = httpClient.begin(); // HttpRequestWrapper
    request2    = request2.authSetting( strAuthzSetting ); // with "Authorization: Bearer XX"
    // https://questetra.zendesk.com/hc/en-us/articles/360024574471-R2300#HttpRequestWrapper
    request2    = request2.queryParam( "mimeType", "application/pdf" );
// request2, try
const response2     = request2.get( request2Uri ); // HttpResponseWrapper
engine.log( " AutomatedTask ApiRequest2 Start: " + request2Uri );
const response2Code = response2.getStatusCode() + "";
engine.log( " AutomatedTask ApiResponse Status: " + response2Code );
if( response2Code !== "200"){
  const response2Body = response2.getResponseAsString() + "";
  throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
                    response2Code + "\n" + response2Body + "\n" );
}
// response2, parse
const fileTmp = new com.questetra.bpms.core.event.scripttask.NewQfile(
                 strSaveas, response2.getContentType(), response2.getResponse()
                );
filesAttached.add( fileTmp );


//// == Data Updating / ワークフローデータへの代入 ==
engine.setData( filesPocketDownload,    filesAttached );

if( strPocketNameOnDrive !== null ){
  engine.setData( strPocketNameOnDrive, response1Obj.name );
}
if( strPocketMimetype    !== null ){
  engine.setData( strPocketMimetype,    response1Obj.mimeType );
}

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


/*
Notes:
- Invoices and quotations created with Google Docs and Sheets can be automatically converted to PDF.
- The File ID can be obtained from the URI or the sharing settings screen.
- TimeoutException error may occur in case many pages or images.
- The exported content is limited to 10MB.
Notes-ja:
- Google ドキュメントやスプレッドシート等で作成された請求書や見積書を自動的にPDF化できます。
- File ID は URI や共有設定画面等から取得します。
- Exportされるファイルのサイズ上限は 10MB です。
- ページ数や画像数が多い等の場合に TimeoutException エラーが発生する場合があります。

APPENDIX-en
- Setting example of "HTTP Authentication" (OAuth2)
    - 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, Consumer Secret:
        - ( from https://console.developers.google.com/ )
        - Redirect URLs: https://s.questetra.net/oauth2callback
APPENDIX-ja
- "HTTP認証"(OAuth2)の設定例
    - 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, Consumer Secret:
        - ( from https://console.developers.google.com/ )
        - Redirect URLs: https://s.questetra.net/oauth2callback
*/

Download

2021-04-10 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/google-drive-gfile-export-as-pdf-2021/
The Addon-import feature is available with Professional edition.

Notes

  • Invoices and quotations created with Google Docs and Sheets can be automatically converted to PDF.
  • The File ID can be obtained from the URI or the sharing settings screen.
  • TimeoutException error may occur if there are too many pages or images.
  • The exported content is limited to 10MB.

Capture

Stores Google file as Workflow data with converting to a PDF file. It is also possible to change the file name. If a file other than a Google file (Docs/Sheets/Slides, etc.) is specified, an error will occur.

Appendix

See also

2 thoughts on “Google Drive #GFile: Export as PDF”

  1. Pingback: Google Drive: GFile; Export as PDF – Questetra Support

  2. Pingback: Google Docs: Document, Replace All – 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