3
votes

Is there a way to save a Google Slides as PDF file by using Google Apps Script?

I could only find solutions for saving Google Docs and Sheets.

3

3 Answers

9
votes

How about this sample script? When Google Docs (Spreadsheet, Document and Slide) is saved and/or downloaded using DriveApp.createFile(), the file format automatically becomes PDF. So we can use the following script.

Sample script :

var blob = DriveApp.getFileById("### Slide file ID ###").getBlob();
DriveApp.createFile(blob);

Note :

In this case, the filename of created PDF file is the same to the slide.

Edit :

If you want to copy the slide file, please use a following script.

var file = DriveApp.getFileById("### Slide file ID ###");
file.makeCopy(DriveApp.getFolderById("### Destination folder ID ###"));
1
votes

This could work by replacing DocumentApp with SlideApp

Beware it also deletes a previously created pdf of the same name if it exists in the drive folder.

function onOpen() {
  var ui = DocumentApp.getUi();
  ui.createMenu('Save PDF copy')
  .addItem('Save PDF', 'SavePDF')
  .addToUi();
}

function SavePDF() {
  var theDoc = DocumentApp.getActiveDocument();
  var id = DocumentApp.getActiveDocument().getId()
  var ui = DocumentApp.getUi();
  var folderString = DriveApp.getFileById(id).getParents().next().getName();
  var folder = DriveApp.getFileById(id).getParents().next();

  var theBlob = theDoc.getAs('application/pdf')
  var thePDFName = theBlob.getName()

  var theFiles = DriveApp.getFilesByName(theBlob.getName())
  while (theFiles.hasNext()) {
    var theFile = theFiles.next()
    if (theFile.getParents().next().getName() == folderString) {
      theFile.setTrashed(true)
    }
   }

  var newFile = folder.createFile(theBlob);
  ui.alert('Saved to ' + folderString + " " + theBlob.getName())
}
0
votes
let pdf =  DriveApp.getFileById(copySlide.getId())
              .getBlob()
              .getAs("application/pdf");
pdf.setName("filename.pdf");
let file = DriveApp.getFolderById(FOLDER_ID).createFile(pdf);