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:

Create QRCODE in DDS Create QRCODE in DDS
Posted by September 21, 2018
Programming >> RPG Programming
Base64 Encoding a File with RPG Base64 Encoding a File with RPG
Posted by September 6, 2018
Programming >> RPG Programming
Building JSON with RPG and YAJL and Writing to Standard Output Building JSON with RPG and YAJL and Writing to Standard Output
Posted by August 31, 2018
Programming >> Proof of Concept (POC)
How to Delete Files or Empty Trash From Your Google Drive with your IBM i and RPG/ILE How to Delete Files or Empty Trash From Your Google Drive with your IBM i and RPG/ILE
Posted by July 24, 2018
BVSTools >> BVSTools Software Discussion >> GreenTools for G Suite (Google Apps) (G4G) Specific Discussion
GreenTools for G Suite (G4G) Updated to Include Delete and Empty Trash Function GreenTools for G Suite (G4G) Updated to Include Delete and Empty Trash Function
Posted by July 24, 2018
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
What to Do If Your License Keys Don't Work What to Do If Your License Keys Don't Work
Posted by July 18, 2018
BVSTools >> BVSTools Software Discussion
MAILTOOL Updated to Allow Failed Message on Invalid Recipient MAILTOOL Updated to Allow Failed Message on Invalid Recipient
Posted by May 20, 2018
BVSTools >> BVSTools Announcements >> eMail Tool (MAILTOOL) Specific Announcements
Non HTTPS Callbacks Removed from GreenTools for G Suite (G4G) Non HTTPS Callbacks Removed from GreenTools for G Suite (G4G)
Posted by April 15, 2018
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
IBM i Related Survey Available IBM i Related Survey Available
Posted by April 7, 2018
IBM Power Systems >> (QGPL) IBM i
BVSTools Releases Braintree Webhook Open Source Application - Node.js BVSTools Releases Braintree Webhook Open Source Application - Node.js
Posted by April 5, 2018
Programming >> Open Source
BVSTools Now Offering Web Services (BETA) BVSTools Now Offering Web Services (BETA)
Posted by April 3, 2018
BVSTools >> BVSTools Announcements
Creating a Reverse SSL Proxy Using RPG on the IBM i - Part 2 Creating a Reverse SSL Proxy Using RPG on the IBM i - Part 2
Posted by March 29, 2018
Programming >> Web Programming
Still on V7R1 or Earlier?  Here's Why You Should Upgrade NOW! Still on V7R1 or Earlier? Here's Why You Should Upgrade NOW!
Posted by February 21, 2018
IBM Power Systems >> (QGPL) IBM i
Converting a MMDDYY date format to YYMMDD for Sorting Using SQL Converting a MMDDYY date format to YYMMDD for Sorting Using SQL
Posted by February 16, 2018
Programming >> RPG Programming
Moving All Files from a Google Drive Folder to the Trash Using GreenTools for Google Apps (G4G) Moving All Files from a Google Drive Folder to the Trash Using GreenTools for Google Apps (G4G)
Posted by February 3, 2018
BVSTools >> BVSTools Software Discussion >> GreenTools for G Suite (Google Apps) (G4G) Specific Discussion

Reply




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