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

Is there anyway to download current slide in Google Slides with the AppScript?


I'd like to download the current page open in google slides as pdf. Google Slides allows in their own UI to download the whole presentation as PDF but I am interested in downloading slides in the presentation one by one.

Right now I am able to select current page with,

export const getCurrentPage = () => {
  return SlidesApp.getActivePresentation()
    .getSelection()
    .getCurrentPage();
};

This function returns a Page Class.

The problem is I don't know how to cast this 'Page Class' to a PDF and download it. I've checked the available methods but couldn’t found anything useful yet. Any suggestions?


Solution

  • Save Google Slide as PDF using Google Apps Script that was mentioned by @Luke converts the whole slide file to PDF and save it to your Google Drive. If you want to download a specific slide on your local computer, You can refer to the sample code.

    Sample Code:

    function onOpen() {
      // get the UI for the Spreadsheet
      const ui = SlidesApp.getUi(); 
      
      // add the menu
      const menu = ui.createMenu("Download")
                     .addItem("Download Current Slide",'downloadModal')
                     .addToUi();
    }
    
    function downloadModal() {
    
      var pdfFile = convertToPdf();
      
      //Get the pdf file's download url
      var html = "<script>window.open('" + pdfFile.getDownloadUrl() + "');google.script.host.close();</script>";
      var userInterface = HtmlService.createHtmlOutput(html)
      .setHeight(10)
      .setWidth(100);
      SlidesApp.getUi().showModalDialog(userInterface, 'Downloading PDF ... ');
    
      //Delete pdf file in the drive
      pdfFile.setTrashed(true);
    }
    
    function convertToPdf() {
    
      //Get current slide
      var presentation = SlidesApp.getActivePresentation();
      var slide = presentation.getSelection().getCurrentPage().asSlide();
      Logger.log(slide.getObjectId());
      
      //Create temporary slide file
      var tmpPresentation = SlidesApp.create("tmpFile");
      //Delete default initial slide
      tmpPresentation.getSlideById("p").remove();
      //Add current slide to the temporary presentation
      tmpPresentation.insertSlide(0,slide);
      tmpPresentation.saveAndClose();
    
      //Create a temporary pdf file from the temporary slide file
      var tmpFile = DriveApp.getFileById(tmpPresentation.getId());
      var pdfFile = DriveApp.createFile(tmpFile.getBlob().setName("slide.pdf"))
      
      //delete temporary slide file
      tmpFile.setTrashed(true);
    
      return pdfFile;
    }
    

    What it does?

    Convert Current Slide to PDF file:

    1. Get the current slide using Page.asSlide().
    2. Create a temporary slide file. Select the initial slide and delete it using Slide.remove().

    Note:

    First slide in the file has an object id = 'q', you can check the object id in the slide url in your browser#slide=id.<object id>

    1. Insert the current slide in Step 1, to our temporary slide file using Presentation.insertSlide(insertionIndex, slide). Save and close
    2. Create a pdf file from our temporary slide file. Get the File object of our temporary slide using DriveApp.getFileById(id), get its blob using File.getBlob(). Set the name of the blob to pdf file using Blob.setName(name).Create a pdf file from the blob using DriveApp.createFile(blob)

    Download the file:

    1. Create a custom menu that will call downloadModal() to download the current slide in your local computer
    2. Call convertToPdf() to get the pdf file's File object. Get the download url using File.getDownloadUrl()
    3. Use custom dialog to display HTML service that will download the file using the download url obtained in Step2.
    4. Delete the pdf file in your Google Drive using File.setTrashed(trashed)