Learn how to convert Google Slides to high-resolution PNG images using Google Apps Script. Choose between Google Slides API and Google Drive API based on your needs.
Docs Studio can convert Google Slides to high-resolution PNG images. This can be useful if you want to create multiple variations of the same slide in bulk – create a single template in Google Slides and then use Docs Studio to generate PNG images with different text or images pulled from Google Sheets or Google Forms.
Internally, the application uses Google APIs to generate high-resolution thumbnail images of slides and upload individual slides to the current user’s Google Drive.
In this tutorial, we’ll explore two ways to achieve Slide-to-PNG conversion using Google Apps Script.
Approach #1 – Use the Google Slides API
You can use the Google Slides API to get thumbnail images of slides, fetch a blob of the image, and then upload the image 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/presentations/${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('image.png');
};
boundaries
The previous approach has some limitations.
First, you need to enable the Google Slides API in the console of your Google Cloud project associated with the Google Apps Script project. Second, thumbnail images have a fixed width of 1600px/800px/200px and you cannot resize the image.
Also, you need to make two API calls here. The first is to get a thumbnail link of a presentation. A further API call will fetch the thumbnail image from the URL.
Approach #2 – Use the Google Drive API
The recommended approach is to use the Google Drive API to export slides as PNG images. The big advantage here is that the generated image is of the same resolution as the original slide. So if you have set the size of your presentation page as 600×800 pixels, the generated PNG image will also be of the same size.
And since the Drive API can directly export the slide as an image, there’s one less API call.
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 = {
method: 'GET',
headers: { Authorization: `Bearer ${ScriptApp.getOAuthToken()}` },
contentType: 'application/json',
};
const request = UrlFetchApp.fetch(apiUrl, parameters);
const blob = request.getBlob();
DriveApp.createFile(blob).setName('image.png');
};
See also: Convert Google Docs and Sheets