Reputation: 381
I'm trying to turn Google Slides into PDF or IMAGE format, and I'm having a lot of trouble finding a solution that is not 10 years old and still working.
Upvotes: 0
Views: 63
Reputation: 381
Here is how I did :
Doesn't matter the way you're going, you NEED to add the services "Google Slides API" !!
If the script is linked to the presentation, you can use the following :
FOR PDF
function toPDF() {
const folderId = "1-rCYyDgTzOV0FKt2gdh8dtaB1hSi1JN-";
const presentation = SlidesApp.getActivePresentation();
const slides = presentation.getSlides();
let temp = SlidesApp.create("Temporary");
const id = temp.getId();
const file = DriveApp.getFileById(id);
const folder = DriveApp.getFolderById(folderId);
slides.forEach((slide, i) => {
temp.appendSlide(slide);
temp.getSlides()[0].remove();
temp.saveAndClose();
folder.createFile(file.getBlob().setName(i + 1));
temp = SlidesApp.openById(id);
});
file.setTrashed(true);
}
FOR IMAGE
function toPNG() {
const folderId = "1G6E_BZeilLJ1_f36xpwFqlthXblGOI3h";
const presentation = SlidesApp.getActivePresentation();
const id = presentation.getId();
const slides = presentation.getSlides();
const folder = DriveApp.getFolderById(folderId);
const options = {"thumbnailProperties.mimeType": "PNG" };
slides.forEach((slide, i) => {
const url = Slides.Presentations.Pages.getThumbnail(
id,
slide.getObjectId(),
options
).contentUrl;
const blob = UrlFetchApp.fetch(url).getAs(MimeType.PNG);
folder.createFile(blob.setName(i + 1));
});
}
If you want to use the code from a bigger script (outside of the script linked to the google slide document), then you can use this :
FOR IMAGE :
/**
* Converts all slides of a Google Slides presentation (specified by its ID) to PNG images
* and saves them to a specified folder.
*
* @param {string} slideFileId - The ID of the Google Slides presentation to convert.
* @param {string} folderId - The ID of the folder where the PNG images should be saved.
*/
function convertSlideToPNG(slideFileId, folderId) {
// Get the specified presentation by its ID
const presentation = SlidesApp.openById(slideFileId);
// Get all slides in the presentation
const slides = presentation.getSlides();
// Get the target folder by its ID
const folder = DriveApp.getFolderById(folderId);
// Define the options to get the slide as PNG
const options = { "thumbnailProperties.mimeType": "PNG" };
// Loop through each slide and generate its thumbnail
slides.forEach((slide, i) => {
const url = Slides.Presentations.Pages.getThumbnail(
slideFileId,
slide.getObjectId(),
options
).contentUrl; // Get the URL for the PNG thumbnail
const blob = UrlFetchApp.fetch(url).getAs(MimeType.PNG); // Fetch the image as PNG
folder.createFile(blob.setName('Slide_' + (i + 1) + '.png')); // Save the PNG in the folder with a slide-based name
});
}
Upvotes: 0