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:

Microsoft Office 365 Servers and Random Errors Issue Microsoft Office 365 Servers and Random Errors Issue
Posted by November 14, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Sending/Resending Emails Using a MIME File with MAILTOOL Sending/Resending Emails Using a MIME File with MAILTOOL
Posted by November 8, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Sending an HTML Email on Your IBM i Using MAILTOOL Sending an HTML Email on Your IBM i Using MAILTOOL
Posted by November 1, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Transferring License Keys from One System to Another Transferring License Keys from One System to Another
Posted by October 31, 2022
BVSTools >> BVSTools Software Discussion
Calculating the Size of a File Before Base64 Encoding Calculating the Size of a File Before Base64 Encoding
Posted by August 13, 2022
Programming >> RPG Programming
GreenTools for Microsoft Apps (G4MS) v9.12 Now Includes Function to Send Emails using MIME File GreenTools for Microsoft Apps (G4MS) v9.12 Now Includes Function to Send Emails using MIME File
Posted by August 11, 2022
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
GreenTools for Google Apps (G4G) v15.20 Now Supports Shortcuts GreenTools for Google Apps (G4G) v15.20 Now Supports Shortcuts
Posted by August 6, 2022
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
GreenTools for Microsoft Apps (G4MS) Groups Admin Authority Instructions GreenTools for Microsoft Apps (G4MS) Groups Admin Authority Instructions
Posted by July 26, 2022
BVSTools >> BVSTools Software Discussion >> GreenTools for Microsoft Apps (G4MS) Specific Discussion
GreenTools for Microsoft Apps (G4MS) v9.10 Now Includes OneDrive Functions that Work With Groups/Shared Drives GreenTools for Microsoft Apps (G4MS) v9.10 Now Includes OneDrive Functions that Work With Groups/Shared Drives
Posted by July 19, 2022
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
GreenTools for Google Apps (G4G) v15.10 Now Includes Drive Functions that Work With Shared Drives GreenTools for Google Apps (G4G) v15.10 Now Includes Drive Functions that Work With Shared Drives
Posted by July 15, 2022
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
GreenTools for Microsoft Apps (G4MS) v9.00 Now Offers Functions to Bypass Registration Command and BVSTools Landing Page GreenTools for Microsoft Apps (G4MS) v9.00 Now Offers Functions to Bypass Registration Command and BVSTools Landing Page
Posted by July 4, 2022
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
What Objects Should I Omit from Replication to Ensure My License Keys Work on my HA/DR System? What Objects Should I Omit from Replication to Ensure My License Keys Work on my HA/DR System?
Posted by June 27, 2022
BVSTools >> BVSTools Software Discussion
GreenTools for Google Apps (G4G) v15.00 Now Offers Functions to Bypass Registration Command and BVSTools Landing Page GreenTools for Google Apps (G4G) v15.00 Now Offers Functions to Bypass Registration Command and BVSTools Landing Page
Posted by May 3, 2022
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
How Do I Switch From MAILTOOL Plus to GreenTools for Google (OAuth 2.0) or Microsoft Office 365? How Do I Switch From MAILTOOL Plus to GreenTools for Google (OAuth 2.0) or Microsoft Office 365?
Posted by April 18, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
PTFs Issued for SSL/TLS Issues PTFs Issued for SSL/TLS Issues
Posted by March 12, 2022
IBM Power Systems >> PTF Watch

Reply




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