1

I'm a bit new to this, so I apologize in advance for any newb related annoyances.

What I'm trying to do is create a google presentation with various images, and then download each slide as a separate pdf. I'm trouble with the downloading as a pdf part. The presentation is being constructed correctly. I've tried a couple different things, but haven't found a working solution yet. The simplest one I tried was:

var newFolder=rootFolder.createFolder(sourceSpreadsheet.getName() + ' - Functionals').setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);  
var deck = SlidesApp.create(NAME);  // name determined separately
var presentationID = deck.getId();

...

var blob = DriveApp.getFileById(presentationID).getBlob();
newFolder.createFile(blob);

This did create a pdf, but it looks like it was just one blank page. I'm unsure if maybe it needs to run on each slide individually rather than the presentation as a whole. I couldn't find anything to indicate that to be the case though.

The second thing I tried was based on a similar solution I found for a spreadsheet. I don't really understand how changing the URL downloads it as a pdf, and maybe that's related to the issue with it, which is this is resulting in a 404.

var presentation = SlidesApp.openById(presentationID);
var url = presentation.getUrl();
url = url.replace(/edit$/,'');
var url_ext = 'export?exportFormat=pdf&format=pdf' + '&muteHttpExceptions=true' //export as pdf
var response = UrlFetchApp.fetch(url + url_ext, {
                                   headers: {
                                   'Authorization': 'Bearer ' +  token
                                   }
                                   });

2 Answers2

0

I've used the second method to create pdfs of Google Sheets.

The following function could be adapted to create pdfs of your slides. This uses the REST API so that's why you need to construct the URL with parameters according to how you want to format the pdf.

Your formated url will need to look something like this: https://docs.google.com/presentation/d/****presentationId****/export?exportFormat=pdf&format=pdf

You can find other optional parameters for formating the pdf in this function.

function exportPDF(fileId) {
  var ss = SpreadsheetApp.openById(fileId);  
  // Base URL
  var url = "https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", ss.getId());
  /* Specify PDF export parameters
  From: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579
        https://stackoverflow.com/questions/46088042/margins-parameters-for-spreadsheet-export
  */

  var url_ext = 'exportFormat=pdf&format=pdf'        // export as pdf / csv / xls / xlsx
  + '&size=letter'                       // paper size legal / letter / A4
  + '&portrait=true'                    // orientation, false for landscape
  + '&fitw=true&source=labnol'                          // fit to page width, false for actual size
  + '&top_margin=0.25'              //All four margins must be set!
  +'&bottom_margin=0.25'           //All four margins must be set!
  +'&left_margin=0.25'             //All four margins must be set!
  +'&right_margin=0.25'            //All four margins must be set!
  + '&sheetnames=false&printtitle=false' // hide optional headers and footers
  + '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
  + '&fzr=true'                         // do not repeat row headers (frozen rows) on each page
  + '&gid=';                             // the sheet's Id

  var token = ScriptApp.getOAuthToken();
  var sheet = ss.getSheets()[0]; //get first sheet

  // Converts to PDF
  var response = UrlFetchApp.fetch(url + url_ext + sheet.getSheetId(), {
      headers: {
        'Authorization': 'Bearer ' +  token
      }
    });

  //convert the response to a blob and store in our array
  var blob = response.getBlob().setName(sheet.getName() + '.pdf');
  var folderId = '**********your folder id here*******************';
  var folder = DriveApp.getFolderById(folderId);
  return folder.createFile(blob).getId();
}
jembe
  • 61
  • 7
  • Thanks for the help! Unfortunately, even with the URL updates, I'm still running into a 404 error. I'm not sure why. I included all of the optional parameters except the &fzr=true since a presentation doesn't have row headers. I log the url, and when I just paste into a browser, it gives me a page not found error, but if I remove all the extra parameters it opens the slideshow as I'd expect. I'm not sure if the browser would accept the parameters or not, so maybe that's expected there? – eaten_by_cthulhu Oct 17 '19 at 22:46
  • If it's helpful, this is what it specifically logs as the url + url_ext + id: https://docs.google.com/presentation/d/1MJB__ogPgu_StPWD-pDYdcwQU0__B3qXVMzLjEVsK7U/export?exportFormat=pdf&format=pdf&muteHttpExceptions=true&size=letter&portrait=false&fitw=true&source=labnol&top_margin=0.25&bottom_margin=0.25&left_margin=0.25&right_margin=0.25&sheetnames=false&printtitle=false&pagenumbers=false&gridlines=false&gid=1MJB__ogPgu_StPWD-pDYdcwQU0__B3qXVMzLjEVsK7U – eaten_by_cthulhu Oct 17 '19 at 22:55
0

Converting all of the slides in a presentation to individual pdfs

function convertingSlideImagesToPDF() {
  var fldr=DriveApp.getFolderById("FolderID");
  var ss=SlidesApp.openById("PresentationID");
  var slds=ss.getSlides();
  var n=0;
  for(var i=0;i<slds.length;i++) {
    var sldImgA=slds[i].getImages();
    if(sldImgA) { 
      for(var j=0;j<sldImgA.length;j++) {
        var imgName=sldImgA[j].getTitle();
        var base64=Utilities.base64Encode(sldImgA[j].getBlob().getBytes());
        var html='<img src="data:image/jpg;base64,'+base64+'">';
        var blob=null;
        blob=Utilities.newBlob(html, MimeType.HTML).setName('Image' + n++ + ".pdf");
        blob=blob.getAs(MimeType.PDF);
        var file=fldr.createFile(blob);  
      }
    }
  }
}

Helpful Reference

Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Thanks so much. I tried this out, and oddly, I got a page could not be found error on the line var sldImgA=slds[i].getImages() I'm not totally sure what page it's even referring to. I adjusted the actual slides right before this bit, so I don't think it could be the slides its referring to, but if not that, what could it be? – eaten_by_cthulhu Oct 17 '19 at 23:24
  • I added a slide that didn't have any image and it didn't cause any problems. Are all of your images jpg? – Cooper Oct 18 '19 at 01:01
  • Try it again. I checked to see if sldImgA is defined before using it now. – Cooper Oct 18 '19 at 01:20
  • Still no luck. It looks like the error is actually occurring above the new if statement where the variable is actually being declared. It's a "page not found" error, which seems like an odd error to get for referencing a slide within a slideshow, right? – eaten_by_cthulhu Oct 18 '19 at 16:02
  • If you can setup a sample spreadsheet and load the script in it. I'll debug it for you. Unfortunately, you will also have to share the slide presentation or at least one that exhibits the same problem. – Cooper Oct 18 '19 at 16:51
  • Thanks so much for the help! I think this should do it: https://docs.google.com/spreadsheets/d/1snYzHd-EgZf-G-zofLquVSR7VStp-zcpu-NOSp-UJsY/edit?usp=sharing I'm still seeing the problem here. This will actually create slideshow as well. Let me know if you have any permissions issues. I'm not sure if you'll run into an issue with the folder it tries to get images from. – eaten_by_cthulhu Oct 19 '19 at 15:43
  • It's in view only mode so I can't see or add scripts. I basically need full access. What I normally do is create a separate public folder and put all of the necessary files in that public folder and provide full access to everyone with the link and hope that some clown doesn't come in and mess everything up. You can always remove the link by editing your original question or deleting a comment. – Cooper Oct 19 '19 at 16:54
  • I've gone through several presentation and this extracts the images reliably. – Cooper Oct 19 '19 at 22:12
  • Weird. Do you think something is happening upstream? I put everything in [this folder](https://drive.google.com/open?id=1S58wnT97WxuNA9knmkJ3tgEkVyBF9r84), and that should have editing turned on, so hopefully you're able to see it now. All the images are jpgs. – eaten_by_cthulhu Oct 21 '19 at 15:25
  • Can you put the slide presentation in that folder? And by the way I could not get to the script in the spreadsheet. – Cooper Oct 21 '19 at 17:10
  • The slide presentation should be constructed in your drive folder once you run the script. Here's a direct link to the script: https://script.google.com/d/1-WdQXHBJhRBNn3Ip6TkZ2_rxINm1stghryIRtF2Wz2NKip4CVm6Dn_OY/edit?usp=sharing For some reason, it won't let me turn editing on for the script. It keeps erroring out. – eaten_by_cthulhu Oct 21 '19 at 17:49
  • That totally makes sense. Thanks for the link, and I'll see if I can get it down to a much smaller example. I really do appreciate the help, and I'm sorry if sending the whole script came off as off putting. – eaten_by_cthulhu Oct 21 '19 at 20:10