Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save Google Slide as PDF using Google Apps Script

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.

like image 759
Roberto B Avatar asked Sep 02 '25 16:09

Roberto B


2 Answers

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 ###"));
like image 78
Tanaike Avatar answered Sep 05 '25 09:09

Tanaike


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())
}
like image 34
rmb87 Avatar answered Sep 05 '25 09:09

rmb87