Ergun
Ergun

Reputation: 520

Google Apps Script how to create pdf file from base64 encoded data

Hi I have a web service that returns base64 encoded pdf data which I need to save as pdf in a google drive folder. Apreciate a short demo to how to proceed especially for converting the base64 encoded pdf data to pdf file.

    function myFunction() {
  var folder = DriveApp.getFolderById("0B9EI2E_9Cj9rT1JmVk5ESnhVbjA");
  var contents = DriveApp.getFileById("18cmOQXdAaW1lmYLvDhpi4BrYFI5LWMh2");



   var Body = contents.getAs('text/plain');
   var bytes = Utilities.base64Decode(Body, Utilities.Charset.UTF_8);



   folder.createFile(bytes);


 Logger.log(folder);
}

Upvotes: 0

Views: 2333

Answers (1)

macorreag
macorreag

Reputation: 372

So far there is no mechanism to manipulate .pdf documents directly through Google Apps Script.

However, there is the librarian pdf-lib.js in Javascript that is compatible with the Google Apps Script execution engine. If you include it in your script you can get to process these files as needed.

I will give an example to extract a .pdf from Google Drive, add a sheet to it and save it back to Google Drive handling its content encoded in Base64.

  1. Get the file from Google Drive using your ID and transform it into a PDFDocument file as required by the library.
    /**
     * This function returns an object of type PDFDocument processable with the PDFLib-js library you need use await 
     * when use this function.
     * @param {string} IdPDFOfDrive - The ID of your file in Google Drive
     */
    async function drivePDFtoPDFLib( IdPDFOfDrive ){
        // Get the file from Drive
        var driveFile = DriveApp.getFileById(IdPDFOfDrive);
    
        var bytesMine = Utilities.base64Encode(driveFile.getBlob().getBytes()); 
        const PDFLibDocument = await PDFLib.PDFDocument.load(bytesMine);
        return PDFLibDocument;
    }
  1. Make a change to the PDF using the library, for example adding a sheet.
    myPDFOnGoogleDrive.addPage([350, 400]);
  1. Store the file in Google Drive.
    /**
     * Save a PDFDocument Object from the PDFLib-js library in the root folder of Google Drive you need use await 
     * when use this function. 
     * @param {string} nameFile - The Name To Save this .pdf File
     * @param {PDFDocument} PDFLibDocument - Object from library PDFLib 
     */
    async function savePDFLibToDrive( nameFile, PDFLibDocument ){
    
        const base64String = await PDFLibDocument.saveAsBase64()
        const data = Utilities.base64Decode(base64String)
        var blob = Utilities.newBlob(data).setName(nameFile).setContentType("application/pdf")
        //Create a new file from a blob in root folder
        DriveApp.createFile(blob);
    
    }


  1. Now using everything at the same time you should get something like this

     const myPDFOnGoogleDrive =  await drivePDFtoPDFLib(IDFILE);

     myPDFOnGoogleDrive.addPage([350, 400]);

     await savePDFLibToDrive('HelloWorld.pdf', myPDFOnGoogleDrive);

At the moment I am preparing a library to manipulate pdf documents in a simpler way in Apps Script, as soon as I finish it it will be available on my Github

Upvotes: 1

Related Questions