Home > Back-end >  Saving a PDF copy of all pages of a Slide Presentation
Saving a PDF copy of all pages of a Slide Presentation

Time:03-29

I have a slide presentation with charts linked to a spreadsheet. I want to create a PDF copy of all pages of this slide in one PDF file for distribution purposes. I found a script from @Tanaike which will work except for some additions.

  1. All slide pages must be copied in one PDF file.
  2. Must be able to select pages that needs to be copied as PDF.
  3. If possible please include a script to automatically email the PDF copy. Thank you very much in advance for any assistance.

Current Script:

function myFunction2() {
  const folderId = "13Un85DDcMiW_ACHC1Tncrgaqt_DP5cg3"; // 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);
}

CodePudding user response:

I believe your goal is as follows.

  • You want to convert a Google Slides to a PDF file by selecting the specific slides in the Google Slides.
  • You want to send the created PDF file as an attachment file of Email.
  • You want to achieve this by modifying your showing script.

In this case, how about the following modification? From your showing script, I thought that you might have used this sample script. In this script, each slide is created as each PDF file. By this, this sample script is not directly used.

When this script is modified for achieving your goal, how about the following modification?

Modified script:

Before you use this script, please set the folder ID and the selected page numbers and the email address you want to send. In this script, the specificl slides are exported using the selected page numbers.

function myFunction3() {
  const folderId = "root"; // Please set the folder ID you want to put the exported PDF files.
  const selectedPages = [1, 2, 3, 5]; // Please set the selected page numbers.
  const emailAddress = "###"; // Please set the email address.

  // 1. Retrieve all slides from the source Google Slides.
  const slide = SlidesApp.getActivePresentation();
  const srcId = slide.getId();

  // 2. Create a temporal Google Slides.
  const file = DriveApp.getFileById(srcId).makeCopy("temp");
  const id = file.getId();
  let temp = SlidesApp.openById(id);
  const tempSlides = temp.getSlides();
  for (let i = tempSlides.length - 1; i >= 0; i--) {
    if (!selectedPages.includes(i   1)) {
      tempSlides[i].remove();
    }
  }

  // 3. Export each page as a PDF file.
  temp.saveAndClose();
  const blob = file.getBlob().setName(`${file.getName()}.pdf`);
  DriveApp.getFolderById(folderId).createFile(blob);

  // 4. Remove the temporal Google Slides.
  file.setTrashed(true);

  // 5. Send email.
  MailApp.sendEmail({ to: emailAddress, subject: "sample subject", body: "sample text body", attachments: [blob] });
}
  • When this script is run, the specific slides are selected and convert them as a PDF data, and create it as a PDF file, and then, it is sent as an attachment file of email.

  • In this sample, const selectedPages = [1, 2, 3, 5] is used. In this case, the slides of 1, 2, 3, 5 pages are exported.

Note:

  • If you don't want to create a file, please remove DriveApp.getFolderById(folderId).createFile(blob);.

  • Unfortunately, I couldn't understand the method for selecting the specific slides from Must be able to select pages that needs to be copied as PDF.. So in this sample, I used an array including the page numbers. If this was not the direction, please modify it.

References:

  • Related