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


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
      * Imports
     D wpID            S            256    Inz('')
     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
       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 = ' ');
         exsr $return;

       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);
         exsr $return;

       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);
         exsr $return;

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

         *INLR = *ON;


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


Copyright 1983-2024 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).