bvstone

Converting a File to PDF with RPG and Google Cloud Print/Google Drive

Posted:

Converting a File to PDF with RPG and Google Cloud Print/Google Drive

I often see the question:

"How can I convert an HTML file to PDF?"  Or variations of this theme.  Well, there's a little know fact that when you use Google Cloud Print you can choose to "print" to your Google Docs (Google Drive) folder as an option.  Not to mention you can set up virtually any printer anywhere in the world as a Google Cloud Printer and print directly to it using your PC, mobile device or even your IBM i!

When you print a file using Google Cloud Print to Google Docs/Drive, the file is converted to PDF automatically.  Once the file is there, you have something you can download and use... a nicely converted PDF file.

With the GreenTools for Google Apps (G4G) product (which is a Google Cloud Print Supported App) you can do all of this in a simple RPG ILE program.  Below is an example of a program I created that takes the following steps:

  • Uploads a file from the IFS to Google Docs/Drive.  This automatically converts the file from HTML to PDF
  • Moves the file from the root folder of my Google Drive account (where the Google Cloud Print option places the file because as of now there is no way to tell it which directory to put the file in)
  • Downloads the newly created file to the IFS
     H DFTACTGRP(*NO) BNDDIR('BVSTOOLS')
      ****************************************************************
      * Imports
      ****************************************************************
      /COPY QCOPYSRC,P.G4GGCP
      /COPY QCOPYSRC,P.G4GDRV
      ****************************************************************
     D wpID            S            256    Inz('bvstone@gmail.com')
     D wpPrinterKey    S             64    Inz('googledocs')
     D wpFile          S            256    Inz('/tmp/test.html')
     D wpTitle         S             64    Inz('mynewfile')
      *
     D fileID          S            256
     D errorMsg        S            256
     D rc              S             10i 0
      ****************************************************************
      /free
       rc = #g4ggcp_setValue('id':wpID:errorMsg);
       rc = #g4ggcp_setValue('printer_key':wpPrinterKey:errorMsg);
       rc = #g4ggcp_setValue('file':wpFile:errorMsg);
       rc = #g4ggcp_setValue('title':wpTitle:errorMsg);

       // after calling this, if successful "fileID" will contain to Google File ID of the file
       rc = #g4ggcp_print(fileID:errorMsg);

       if (rc < 0) or (fileID = ' ');
         //error
         exsr $return;
       endif;

       rc = #g4gdrv_setValue('id':wpID:errorMsg);
       rc = #g4gdrv_setValue('file_id':fileID);
       // Set from folder to root (retrieved using G4GLSTFIL)
       rc = #g4gdrv_setValue('from_folder_id':'0AGSd62H63YjoUk9PVA'); //root
       // Set to folder to /test (retrieved using G4GLSTFIL)
       rc = #g4gdrv_setValue('to_folder_id':'0B2Sd62H63YjoSGhMaGJkSGJLYW8');

       rc = #g4gdrv_moveFile(errorMsg);

       if (rc < 0);
         //error
         exsr $return;
       endif;

       rc = #g4gdrv_setValue('id':wpID:errorMsg);
       rc = #g4gdrv_setValue('file_id':fileID);
       rc = #g4gdrv_setValue('download_directory':'/tmp');
       rc = #g4gdrv_setValue('download_filename':'downloadtest.pdf');

       rc = #g4gdrv_downLoadFile(errorMsg);

       if (rc < 0);
         //error
         exsr $return;
       endif;

       EXSR $return;
       //***************************************************************
       //* Return
       //***************************************************************
       begsr $return;

         *INLR = *ON;
         return;

       endsr;
      /end-free

The first step we use is calling the #g4ggcp_print() function to "print" the HTML file from the IFS to our Google Docs/Drive location.  This is when the automatic HTML to PDF conversion happens.  If this call is successful we will have the Google File ID of the file created in the fileID variable.

Next we want to test moving the file.  For this we need the Google File ID (from the previous step) as well as the Google IDs of the to and from folders.  With the G4G application there is a command named G4GLSTFIL that will list files and folders in your Google Drive.  Included in this information are the IDs needed for these parameters.  Once completed, the file will have been moved from the root folder to a subfolder in our Google Drive Account.

Finally, we again use the Google File ID as the file to download.  The File ID is still the same even if we've moved it.  The File ID in Google Drive is a unique identifier for a file.  It is possible to have two or more files with the same name (ie, reports.pdf) in a Google Drive Folder.  What makes them unique is this File ID.  Once we have it we set the download directory, the name of the file once it is downloaded and call the #g4gdrv_downloadFile() function.

We now have the file which started out as an HTML file in the IFS and converted to PDF now on our system to do with what we want.

Of course, if you wish to duplicate this process you will need to change the Google ID and the Google Drive Folder IDs to ones specific to your account.

You can find more information about G4G at our website or in the documentation.

Requirements for this are GreenTools for Google Apps (G4G )v10.10 or higher and a Google account.

 


Last edited 08/18/2016 at 12:53:11



Latest Posts:

GreenTools For G Suite (G4G) v12.00 Released With Base OAuth 2.0 Functionality GreenTools For G Suite (G4G) v12.00 Released With Base OAuth 2.0 Functionality
Posted by July 28, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
BVSTools Small Price Increase in 2020 BVSTools Small Price Increase in 2020
Posted by July 26, 2019
BVSTools >> BVSTools Announcements
GreenTools for Vertex Cloud (VTXCLOUD) Now Available GreenTools for Vertex Cloud (VTXCLOUD) Now Available
Posted by July 22, 2019
BVSTools >> BVSTools Announcements >> GreenTools for Vertex Cloud (VTXCLOUD) Specific Announcements
GreenTools for Google Apps (G4G) - Drive Addon Successfully Verified by Google GreenTools for Google Apps (G4G) - Drive Addon Successfully Verified by Google
Posted by July 22, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
Why I Cancelled my DynDNS Service and How I Replaced It with an IBM i Application Why I Cancelled my DynDNS Service and How I Replaced It with an IBM i Application
Posted by July 17, 2019
IBM Power Systems >> (QGPL) IBM i
Green Tools for G Suite (G4G) Product Updates (Licensing, Functionality, Base Product) Green Tools for G Suite (G4G) Product Updates (Licensing, Functionality, Base Product)
Posted by July 13, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
Reading JSON Data from Standard Input With YAJL and RPG Reading JSON Data from Standard Input With YAJL and RPG
Posted by July 12, 2019
Programming >> Proof of Concept (POC)
MAILTOOL Updated to Allow Use of IBM Global Security Kit (GSKIT) for SSL/TLS Communications MAILTOOL Updated to Allow Use of IBM Global Security Kit (GSKIT) for SSL/TLS Communications
Posted by June 19, 2019
BVSTools >> BVSTools Announcements >> eMail Tool (MAILTOOL) Specific Announcements
GETURI v10.00 Released Supporting IBM Global Security Kit (GSKIT) and Server Name Indication (SNI) GETURI v10.00 Released Supporting IBM Global Security Kit (GSKIT) and Server Name Indication (SNI)
Posted by June 11, 2019
BVSTools >> BVSTools Announcements >> Get URI (GETURI) Specific Announcements
BVSTools Now Offers Vertex Cloud Interface BVSTools Now Offers Vertex Cloud Interface
Posted by April 15, 2019
BVSTools >> BVSTools Announcements
Token Has an Invalid Signature Error for Office 365 Email Token Has an Invalid Signature Error for Office 365 Email
Posted by March 22, 2019
BVSTools >> BVSTools Software Discussion >> GreenTools for Microsoft Apps (G4MS) Specific Discussion
Resending Emails that have Errored Out with Updated Router or Authentication Information Resending Emails that have Errored Out with Updated Router or Authentication Information
Posted by March 1, 2019
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
BVSTools Offers Toolset to Work With HubSpot OAuth 2.0 APIs On Your IBM i BVSTools Offers Toolset to Work With HubSpot OAuth 2.0 APIs On Your IBM i
Posted by January 27, 2019
BVSTools >> BVSTools Announcements
G4MSDRV Currently Not Supported G4MSDRV Currently Not Supported
Posted by January 17, 2019
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
Removing Trailing Carriage Returns and/or Line Feeds from a String with RPG Removing Trailing Carriage Returns and/or Line Feeds from a String with RPG
Posted by December 26, 2018
Programming >> RPG Programming

Reply




Copyright 1983-2019 BVSTools
GreenBoard(v3) Powered by the eRPG SDK, MAILTOOL Plus!, GreenTools for Google Apps, jQuery, jQuery UI, BlockUI, CKEditor and running on the IBM i (AKA AS/400, iSeries, System i).