2

Good Morning All. I have written a short script which batch-creates [single page] Google Slides based on rows from a spreadsheet. While in the loop for each creation, I would like to create a PNG of the Slide in Google Drive (or download on the user's desktop). These pictures should be the same specs as if a user clicked File>Download>PNG - the heavy small text requires full projector HD - so I don't believe I can use the "Thumbnail" function which appears limited to 1600 pixels.

My code below generates the error "Converting from text/html to image/png is not supported" - so I'm not sure if this is a limitation of the API or a problem with my coding. Thank you in advance.

  var options =
     {
       "contentType" : "image/PNG"
     };
  var url = 'https://docs.google.com/presentation/d/' + presentationCopyId + '/export/PNG';
  var response = UrlFetchApp.fetch(url, options);
  var image = response.getAs(MimeType.PNG);
  image.setName(SlideName);
  DriveApp.createFile(image);  
Rubén
  • 34,714
  • 9
  • 70
  • 166
Joe_Codes
  • 23
  • 1
  • 4

2 Answers2

8

Yes, It is possible.

You can use Google slide API and make a PNG file of every page of Google slide.

Here is the code, BUT first you have to enable API as following

  1. open script editor
  2. click on resources-> Advanced Google Services
  3. Enable Google slide API and Drive API .
  4. click on ok

now copy and paste this code, and write your slide ID in presentationID.

function generateScreenshots() {
  
  var presentationId = "***ADD YOUR Google Slide ID Only***";
  var presentation = SlidesApp.openById(presentationId);
  var baseUrl =
    "https://slides.googleapis.com/v1/presentations/{presentationId}/pages/{pageObjectId}/thumbnail";
  var parameters = {
    method: "GET",
    headers: { Authorization: "Bearer " + ScriptApp.getOAuthToken() },
    contentType: "application/json",
    muteHttpExceptions: true
  };

  // Log URL of the main thumbnail of the deck
  Logger.log(Drive.Files.get(presentationId).thumbnailLink);

  // For storing the screenshot image URLs
  var screenshots = [];

  var slides = presentation.getSlides().forEach(function(slide, index) {
    var url = baseUrl
      .replace("{presentationId}", presentationId)
      .replace("{pageObjectId}", slide.getObjectId());
    var response = JSON.parse(UrlFetchApp.fetch(url, parameters));

    // Upload Googel Slide image to Google Drive
    var blob = UrlFetchApp.fetch(response.contentUrl).getBlob();
    DriveApp.createFile(blob).setName("Image " + (index + 1) + ".png");

    screenshots.push(response.contentUrl);
  });

  return screenshots;
}
Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54
Zenilkumar 99
  • 136
  • 2
  • 2
-1

This answer is outdated, leaving up for documentation purposes but please see other answer.


Answer:

Unfortunately it is not possible to export a Slides as a PNG file using the the Slides nor Drive APIs.

More Information:

According to the documentation, there are only four available MimeTypes for exporting Presentations files:

  • application/vnd.openxmlformats-officedocument.presentationml.presentation
  • application/vnd.oasis.opendocument.presentation
  • application/pdf
  • text/plain

Attempting to export to the image/png MIME Type results in the following error:

Converting from text/html to image/png is not supported

For testing purposes, I tried using the /export/pdf endpoint and making a second conversion to PNG from there like so:

function slidesAsPngAttempt() {
  var presentationCopyId = "1Loa...pQs";  
  var options =
      {
        "contentType" : "application/pdf"
      };
  // for exporting to pdf the /export/pdf needs to be all lower case to avoid 404
  var url = 'https://docs.google.com/presentation/d/' + presentationCopyId + '/export/pdf';  
  var response = UrlFetchApp.fetch(url, options);
  var pdfAsblob = response.getBlob();
 
  var image = pdfAsblob.getAs('image/png');
  
  image.setName(DriveApp.getFileById(presentationCopyId).getName());
  DriveApp.createFile(image);  
}

Unfortunately, a similar error occurs when running var image = pdfAsblob.getAs('image/png'):

Converting from application/pdf to image/png is not supported.

From the same export MIME types reference documentation, the only export types available for PDF files are:

  • text/csv
  • text/tab-separated-values
  • application/zip

References:

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54
  • Appreciate it, I feared this may be the case. A PDF may actually work for my process. However, modifying the code above left me with a PDF of "One account. All of google. Sign in to Continue." Duplicated Slide has its permission set to a view link (shared). Is there another authentication/setting I should be calling? – Joe_Codes Jan 22 '20 at 16:52