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:

How to Whitelist GreenTools for G Suite (G4G) For Your Organization How to Whitelist GreenTools for G Suite (G4G) For Your Organization
Posted by November 5, 2023
BVSTools >> BVSTools Software Discussion >> GreenTools for G Suite (Google Apps) (G4G) Specific Discussion
QuickBooks Online Releases QuickBooks Online Releases "New Invoices!"... and It's Terrible!
Posted by October 8, 2023
QuickBooks >> QuickBooks Online
Admin/4i - What is it? Admin/4i - What is it?
Posted by September 30, 2023
Vendor Corner >> MSD Information Technology
BVSTools Releases Send Job Log to BVSTools (SNDLOG2BVS) Command BVSTools Releases Send Job Log to BVSTools (SNDLOG2BVS) Command
Posted by August 28, 2023
BVSTools >> BVSTools Announcements
MAILTOOL Now Allows Email Redirection for Development and Testing MAILTOOL Now Allows Email Redirection for Development and Testing
Posted by May 27, 2023
BVSTools >> BVSTools Announcements >> eMail Tool (MAILTOOL) Specific Announcements
GreenTools for Microsoft Apps (G4MS) Now Supports Footers When Sending Email GreenTools for Microsoft Apps (G4MS) Now Supports Footers When Sending Email
Posted by March 29, 2023
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
QuickBooks Online - Subtotals and Discounts Frustration QuickBooks Online - Subtotals and Discounts Frustration
Posted by March 16, 2023
QuickBooks >> QuickBooks Online
Making the Switch From QuickBooks Desktop to QuickBooks Online - No Picnic Making the Switch From QuickBooks Desktop to QuickBooks Online - No Picnic
Posted by March 16, 2023
QuickBooks >> QuickBooks Online
BVSTools Software Verified on V7R5 and Power10 BVSTools Software Verified on V7R5 and Power10
Posted by December 9, 2022
BVSTools >> BVSTools Announcements
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

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).