gmail-processor icon indicating copy to clipboard operation
gmail-processor copied to clipboard

PDF password removal

Open MihaiSprinceana opened this issue 1 year ago • 9 comments

I'm using the below code to remove a password from a PDF file, but the integration with convertapi can do more. Prerequisite: secret key from convertapi.

/**
 * If the file is password protected than remove the password
 */
function createFileInsideFolderUsingRuleSettings(rule, attachment, folder) {
  if(rule.password) {
      var obj = {
        secretkey: "testKey",  // Your secret key.
        pass: rule.password, // Password of the protected PDF file.
        blob: attachment.getAs(MimeType.PDF), // pdf blob;
      }
      var blob = decrypting(obj);
      return folder.createFile(blob);
  } else {
      return folder.createFile(attachment);
  }
}

/**
 * Decrypts the PDF file
 */
function decrypting(obj) {
  var url = "https://v2.convertapi.com/convert/pdf/to/decrypt?Secret=" + obj.secretkey;
  var options = {
    method: "post",
    payload: {File: obj.blob, Password: obj.pass},
  }
  var res = UrlFetchApp.fetch(url, options);
  res = JSON.parse(res.getContentText());
  var blob = res.Files.map(function(e) {return Utilities.newBlob(Utilities.base64Decode(e.FileData), MimeType.PDF, e.FileName)});
  return blob[0];
}

Note: is based on the Gmail2Drive project and it will require an account on convertapi website.

MihaiSprinceana avatar May 14 '24 17:05 MihaiSprinceana