Skip to content
Advertisement

How can I convert each google slide into a PDF and JPG?

Google Apps Script

  • I want to create PDF files per each slide of a Google slide file (a normal slide with words and graphs).
  • I reviewed past questions and I found a solution that generates PDF files but taking the slides as images (I need to be able to search for words in those pdf).
  • Another solution was applied to convert the spreadsheet -> PDF but also it didn’t work for my case.

This was my reference

function autoFlyerGenerator() {

  const name_file = "FILE_NAME";
  
  // Template file in Google slide
  const ppt_file = DriveApp.getFileById(##SLIDE_ID##);
  // Temporal folder to store a temporal slide file
  const temp_folder = DriveApp.getFolderById(##TEMP_FOLDER_ID##);
  // PDF folder
  const pdf_folder = DriveApp.getFolderById(##PDF_FOLDER_ID##);

  // My current spreadsheet
  const ss = SpreadsheetApp.getActive();
  const values_ss = ss.getDataRange().getValues();

  const temp_ppt = ppt_file.makeCopy(temp_folder).setName(name_file);
  const open_temp_ppt = SlidesApp.openById(temp_ppt.getId());
  const slides = open_temp_ppt.getSlides();

  // First for-loop iterate over all rows (without the header)
  // Second for-loop iterate over all columns
  for (var index = 0; index < ss.getLastRow()-1; index++){
    for (var col = 0; col < ss.getLastColumn(); col++){
      slides[index].replaceAllText(values_ss[0][col], values_ss[index+1][col])
    }
  }

  open_temp_ppt.saveAndClose();

  // This code block create a single PDF of the slide
  const pdfContentBlob = temp_ppt.getBlob();
  pdf_folder.createFile(pdfContentBlob).setName(name_file);
}

Update

  • I forgot to mention that the Google slide file has a different page setup size than the default.
  • The first answer to this question solved the problem to generate the pdf files, but it doesn’t consider the size of the slide.

Advertisement

Answer

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?

  1. Retrieve all slides from the source Google Slides.
  2. Create a temporal Google Slides.
  3. Export each page as a PDF file.
  4. 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);
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement