Discover ways to convert Google Slides into high-resolution PNG photographs utilizing Google Apps Script. Select between the Google Slides API and the Google Drive API based mostly in your necessities.
Doc Studio can convert Google Slides into high-resolution PNG photographs. This may be helpful if you wish to create a number of variations of the identical slide in bulk – create a single template in Google Slides after which use Doc Studio to generate PNG images with completely different textual content or photographs, pulled from a Google Sheet or Google Varieties.
Internally, the app makes use of the Google APIs to generate high-resolution thumbnail photographs of the slides and uploads the person slides to the Google Drive of the present person.
On this tutorial, we’ll discover two strategies to realize the slide-to-png conversion utilizing Google Apps Script.
Method #1 – Use the Google Slides API
You should use the Google Slides API to get the thumbnail photographs of the slides, fetch the blob of the picture, after which add the picture to Google Drive.
const generateSlideScreenshot = () => {
const presentation = SlidesApp.getActivePresentation();
const presentationId = presentation.getId();
const pageObjectId = presentation.getSlides()[0].getObjectId();
const apiUrl = `https://slides.googleapis.com/v1/shows/${presentationId}/pages/${pageObjectId}/thumbnail`;
const apiUrlWithToken = `${apiUrl}?access_token=${ScriptApp.getOAuthToken()}`;
const request = UrlFetchApp.fetch(apiUrlWithToken);
const { contentUrl } = JSON.parse(request.getContentText());
const blob = UrlFetchApp.fetch(contentUrl).getBlob();
DriveApp.createFile(blob).setName('picture.png');
};
Limitations
There are a couple of limitations with the earlier method.
First, you would wish to allow Google Slides API within the console of your Google Cloud mission related to the Google Apps Script mission. Second,
the thumbnail photographs has a hard and fast width of 1600px/800px/200px and you can’t change the scale of the picture.
Additionally, it’s essential make two API calls right here. The primary one is to get the thumbnail hyperlink of the presentation. The extra API name will fetch the thumbnail picture from the URL.
Method #2 – Use the Google Drive API
The really useful method is to make use of the Google Drive API to export the slides as PNG photographs. The massive benefit right here is that the generated picture is of the identical decision as the unique slide. So you probably have set your presentation web page measurement as 600×800 pixels, the generated PNG picture may also be of the identical measurement.
And there’s one much less API name to make because the Drive API can straight export the slide as a picture.
const generateSlideScreenshotWithDrive = () => {
const presentation = SlidesApp.getActivePresentation();
const id = presentation.getId();
const pageid = presentation.getSlides()[0].getObjectId();
const apiUrl = `https://docs.google.com/presentation/d/${id}/export/png?id=${id}&pageid=${pageid}`;
const parameters = {
methodology: 'GET',
headers: { Authorization: `Bearer ${ScriptApp.getOAuthToken()}` },
contentType: 'utility/json',
};
const request = UrlFetchApp.fetch(apiUrl, parameters);
const blob = request.getBlob();
DriveApp.createFile(blob).setName('picture.png');
};
Additionally see: Convert Google Docs and Sheets