3

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.

Kos
  • 4,890
  • 9
  • 38
  • 42
Roberto B
  • 111
  • 2
  • 10

3 Answers3

10

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 ###"));
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thank you! What should I change so that the original format Google Slide is retained? – Roberto B Oct 18 '17 at 22:35
  • @Roberto B Welcome. Thank you, too. In my experiences, when I have tried to convert some slides using this script, the slide format has been kept and converted to PDF. – Tanaike Oct 18 '17 at 22:40
  • Excuse me, you're right. The format remains. I meant something else. If I want to save a copy of the presentation with original file-format (GoogleSlide), not as a PDF. What do I need to change in your code? – Roberto B Oct 18 '17 at 22:49
  • @Roberto B I'm sorry. I have misunderstand your question. I updated my answer. Please confirm it. You can copy the slide file without converting using [``makeCopy()``](https://developers.google.com/apps-script/reference/drive/file#makecopydestination). – Tanaike Oct 18 '17 at 22:55
  • @Roberto B Yes. Copying original file and converting to PDF from Google Docs are easy to do. But in the case of converting to other format, it becomes a bit complicated. – Tanaike Oct 18 '17 at 23:05
1

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())
}
rmb87
  • 11
  • 2
0
let pdf =  DriveApp.getFileById(copySlide.getId())
              .getBlob()
              .getAs("application/pdf");
pdf.setName("filename.pdf");
let file = DriveApp.getFolderById(FOLDER_ID).createFile(pdf);
hp-01
  • 11
  • 3