Save Gmail Messages as PDF - Google Apps Script

The Save Gmail to Google Drive program using Google Scripts to save Gmail message as PDF using the built-in PDF converter of Google Docs (now Google Drive).

The Google Apps Script reads the HTML body of the Gmail threads, strips the inline images, saves the thread as an HTML file and then converts the HTML into a PDF. If there are any file attachments in the email thread, they are saved as well. The downloads links of the attachments are appended to the PDF as well.

function saveGmailAsPDF() {

  var gmailLabels  = "PDF";
  var driveFolder  = "My Gmail";

  var threads = GmailApp.search("in:" + gmailLabels, 0, 5);

  if (threads.length > 0) {

    /* Google Drive folder where the Files would be saved */
    var folders = DriveApp.getFoldersByName(driveFolder);
    var folder = folders.hasNext() ?
        folders.next() : DriveApp.createFolder(driveFolder);

    /* Gmail Label that contains the queue */
    var label = GmailApp.getUserLabelByName(gmailLabels) ?
        GmailApp.getUserLabelByName(gmailLabels) : GmailApp.createLabel(driveFolder);

    for (var t=0; t<threads.length; t++) {

      threads[t].removeLabel(label);
      var msgs = threads[t].getMessages();

      var html = "";
      var attachments = [];

      var subject = threads[t].getFirstMessageSubject();

      /* Append all the threads in a message in an HTML document */
      for (var m=0; m<msgs.length; m++) {

        var msg = msgs[m];

        html += "From: " + msg.getFrom() + "<br />";
        html += "To: " + msg.getTo() + "<br />";
        html += "Date: " + msg.getDate() + "<br />";
        html += "Subject: " + msg.getSubject() + "<br />";
        html += "<hr />";
        html += msg.getBody().replace(/<img[^>]*>/g,"");
        html += "<hr />";

        var atts = msg.getAttachments();
        for (var a=0; a<atts.length; a++) {
          attachments.push(atts[a]);
        }
      }

      /* Save the attachment files and create links in the document's footer */
      if (attachments.length > 0) {
        var footer = "<strong>Attachments:</strong><ul>";
        for (var z=0; z<attachments.length; z++) {
          var file = folder.createFile(attachments[z]);
          footer += "<li><a href='" + file.getUrl() + "'>" + file.getName() + "</a></li>";
        }
        html += footer + "</ul>";
      }

      /* Conver the Email Thread into a PDF File */
      var tempFile = DriveApp.createFile("temp.html", html, "text/html");
      folder.createFile(tempFile.getAs("application/pdf")).setName(subject + ".pdf");
      tempFile.setTrashed(true);

    }
  }
}

Amit Agarwal is a web geek, solo entrepreneur and loves making things on the Internet. Google recently awarded him the Google Developer Expert and Google Cloud Champion title for his work on Google Workspace and Google Apps Script.

Awards & Recognition

Google Developer Expert

Google Developer Expert

Google awarded us the Developer Expert title recogizing our work in Workspace

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional title for 5 years in a row

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator award for technical expertise

Want to stay up to date?
Sign up for our email newsletter.

We will never send any spam emails. Promise 🫶🏻