Search code examples
google-apps-scriptgoogle-sheetsgoogle-slides

How to export an individual slide from a presentation as a PDF using Google App Scripts?


I have a Google Spreadsheet attached to a Form. On Form Submit, a script takes information from the submission and applies it into carefully placed textboxes in a Google Slide. There are two separate forms which are used to submit information into two separate slides on the SAME presentation.

After the information is entered, the script runs a function that is supposed to export the edited slide ONLY as a PDF and email it to an address provided on the Google Form.

I had a similar setup on a different program of mine and had someone from an online forum help me write the export PDF / Email code. The code they helped me write was for exporting and emailing an individual Spreadsheet sheet.

Here is the email code that I copied from my Spreadsheet program.

function Email3UpPDF () {
  Logger.log("Emailing!");
  var Sapp = SpreadsheetApp;
  var FS = Sapp.getActiveSpreadsheet().getSheetByName("3UP Submissions");
  var ssID = "10Up_PcLxVopXont9Qcu-yk-PrFGWfWPQ3ETsgys4v0Y"
  var shID = "g2580bcdba17_0_22"
  
  var Addy1 = FS.getRange("N"+FS.getLastRow()).getValue();
  
  var requestData = {"method": "GET", "headers":{"Authorization":"Bearer "+ScriptApp.getOAuthToken()}}; 
  var url = "https://docs.google.com/presentation/d/"+ ssID + "/export?format=pdf&id="+shID; //This creates the PDF export url
  var result = UrlFetchApp.fetch(url , requestData);  
  var contents = result.getContent();
  
  MailApp.sendEmail(Addy1,"Local Sign Template" ,"Here is your custom sign Template.", {attachments:[{fileName: "LST3UP.pdf", content:contents, mimeType:"application/pdf"}]});

}

My HOPE was that I could simply substitute the Spreadsheet ID with a Presentation ID, and the Sheet ID with a Slide ID. When I tried that I was met with this error:

Exception: Request failed for https://docs.google.com returned code 404. Truncated server response: <meta name="viewport" c... (use muteHttpExceptions option to examine full response)

The error points to line 13: var contents = result.getContent();

I should note that when I put JUST the Presentation ID in both the ssID and the shID variables, the program runs successfully but ends up sending a 2 page PDF with both slides on it. Which makes sense to me. My intention is to only have one slide though.


Solution

  • Alternative Method

    I have found this related post that you could utilise to work around this matter using a specific method mentioned in the post. I have tweaked your script below & performed a quick test run on my end.

    Sample Tweaked Script

    function extractPdfData() {
      Logger.log("Emailing!");
    
      //Replace this with your API key, this is my temporary limited 'api key' for testing purposes.
      const apiSecret = 'ZQ2A9pbkh6w3xuBY'; 
      var Sapp = SpreadsheetApp;
      var FS = Sapp.getActiveSpreadsheet().getSheetByName("3UP Submissions");
      var ssID = '10Up_PcLxVopXont9Qcu-yk-PrFGWfWPQ3ETsgys4v0Y';
    
      //define the number of slide page(s) that you want. E.g. slide number '2'
      var pages = [2].join('%2C');
    
      var Addy1 = FS.getRange("N"+FS.getLastRow()).getValue();
      var requestData = { "method": "GET", "headers": { "Authorization": "Bearer " + ScriptApp.getOAuthToken() } };
      var pdfUrl = "https://docs.google.com/presentation/d/" + ssID + "/export?format=pdf";
      var slidePDF = UrlFetchApp.fetch(pdfUrl, requestData).getBlob();
    
      //Retrieve the number of PDF page(s) you only want to attach on your email.
      var pdfPages = processPDF(apiSecret, pages, slidePDF);
    
      //Send the page(s) in your email
      pdfPages.forEach(page => MailApp.sendEmail(Addy1,"Local Sign Template" ,"Here is your custom sign Template.", {attachments:[{fileName: "LST3UP.pdf", content:UrlFetchApp.fetch(page.Url).getContent(), mimeType:"application/pdf"}]}));
    }
    
    //Utilizing the 'convertapi' method
    processPDF(apiSecret, pages, slidePDF) {
      var url = `https://v2.convertapi.com/convert/pdf/to/split?Secret=${apiSecret}&ExtractPages=${pages}&StoreFile=true`;
      var options = {
        method: "post",
        payload: { File: slidePDF },
      }
      var res = UrlFetchApp.fetch(url, options);
      var rawData = JSON.parse(res.getContentText());
    
      return rawData.Files;
    }
    

    Demo

    Dummy Slide.
    E.g. you want the slide number 2 to be the only PDF file sent to your recipient.

    enter image description here

    After running the script.
    My dummy email account received this email:

    enter image description here

    The whole PDF attachment is only the slide number 2:

    enter image description here