I believe your goal as follows.
- You want to export each page of a Google Slides as each PDF file.
- You want to achieve this using Google Apps Script.
In this case, how about the following flow?
- Retrieve all slides from the source Google Slides.
- Create a temporal Google Slides.
- Export each page as a PDF file.
- Remove the temporal Google Slides.
When this flow is reflected to a script, it becomes as follows.
Sample script:
Please copy and paste the following script to the script editor of Google Slides, and run the function myFunction
. By this, the script is run.
function myFunction() {
const folderId = "###"; // Please set the folder ID you want to put the exported PDF files.
// 1. Retrieve all slides from the source Google Slides.
const slide = SlidesApp.getActivePresentation();
const srcSlides = slide.getSlides();
// 2. Create a temporal Google Slides.
let temp = SlidesApp.create("temp");
const id = temp.getId();
const file = DriveApp.getFileById(id);
const folder = DriveApp.getFolderById(folderId);
// 3. Export each page as a PDF file.
srcSlides.forEach((s, i) => {
temp.appendSlide(s);
temp.getSlides()[0].remove();
temp.saveAndClose();
folder.createFile(file.getBlob().setName(`page_${i + 1}.pdf`));
temp = SlidesApp.openById(id);
});
// 4. Remove the temporal Google Slides.
file.setTrashed(true);
}
- In this case, the filenames of each PDF file are like
page_1.pdf
, page_2.pdf
.
Note:
From your title, if you want to export each slide as each JPEG file instead of PDF file, you can also use the following sample script. In this case, Slides API is used. So, before you run the script, please enable Slides API at Advanced Google services.
function myFunction() {
const folderId = "###"; // Please set the folder ID you want to put the exported PDF files.
// 1. Retrieve all slides from the source Google Slides.
const slide = SlidesApp.getActivePresentation();
const id = slide.getId();
const srcSlides = slide.getSlides();
// 2. Export each page as a JPEG file.
const folder = DriveApp.getFolderById(folderId);
srcSlides.forEach((s, i) => {
const url = Slides.Presentations.Pages.getThumbnail(id, s.getObjectId(), {"thumbnailProperties.mimeType": "PNG"}).contentUrl;
const blob = UrlFetchApp.fetch(url).getAs(MimeType.JPEG);
folder.createFile(blob.setName(`page_${i + 1}.jpg`));
});
}
- In this case, the filenames of each JPEG file are like
page_1.jpg
, page_2.jpg
.
References:
Added:
From the following OP's replying,
My Slide file has a different page setup than the default, so when applying your code the PDF files generated end up with the default size, giving distorted files. How can I address that?
Actually, it's my bad. I mean that my original Google Slide file has a different page setup (let's say 14 x 18 cm). Your code works, but the PDF files generated have the default page setup of a slide.
From above replying, it seems that the page size is not default. In this case, I would like to propose to use the page size of the original Google Slides. The sample script is as follows.
Sample script:
function myFunction2() {
const folderId = "###"; // Please set the folder ID you want to put the exported PDF files.
// 1. Retrieve all slides from the source Google Slides.
const slide = SlidesApp.getActivePresentation();
const srcId = slide.getId();
const srcSlides = slide.getSlides();
// 2. Create a temporal Google Slides.
const file = DriveApp.getFileById(srcId).makeCopy("temp");
const id = file.getId();
let temp = SlidesApp.openById(id);
temp.getSlides().forEach((e, i) => {
if (i != 0) e.remove();
});
const folder = DriveApp.getFolderById(folderId);
// 3. Export each page as a PDF file.
srcSlides.forEach((s, i) => {
temp.appendSlide(s);
temp.getSlides()[0].remove();
temp.saveAndClose();
folder.createFile(file.getBlob().setName(`page_${i + 1}.pdf`));
temp = SlidesApp.openById(id);
});
// 4. Remove the temporal Google Slides.
file.setTrashed(true);
}