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?
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.
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;
}
Convert Current Slide to PDF file:
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>
Download the file:
downloadModal()
to download the current slide in your local computerconvertToPdf()
to get the pdf file's File object. Get the download url using File.getDownloadUrl()