Ryan Capule
Ryan Capule

Reputation: 143

Export PDF Google Script How to fit to Page?

 if(foldNum == 2){var url_ext = '/export?exportFormat=pdf&format=pdf'  

      + (sheetId ? ('&gid=' + sheetId) : ('&id=' + spreadsheetId)) 
      + '&top_margin=0.50' 
      + '&bottom_margin = 0.50'
      + '&left_margin=0.250'             
      + '&right_margin=0.250'
      + 'scale = 3'
      + '&sheetnames=false&printtitle=false&pagenumbers=false'  //hide optional headers and footers
      + '&gridlines=false'  // hide gridlines
      }

Hello, I am using the code above that I have adapted from other sources to my preferences. I want to export a Google Sheet as a PDF with the margins set above but I am not sure how to fit to page. I saw that scale = 3 is supposed to fit to height but it doesn't seem to do anything. My sheet still prints out on 2 pages instead of scaling to 1 page.

Upvotes: 0

Views: 1506

Answers (2)

Oogie2021
Oogie2021

Reputation: 11

got mine finally working with the following - might give you something to work from - it saves also into a shared folder on our workspace folder so you may want to take them out your scripts.

var foldersave=DriveApp.getFolderById('**yourfolder ID**');


  var request = {
  "method": "GET",
  "headers":{"Authorization": "Bearer "+ScriptApp.getOAuthToken()},    
  "muteHttpExceptions": true
   };

   var key='**yourKey**'; 
   var fetch='https://docs.google.com/spreadsheets/d/'+key+'/export? 
   format=pdf&'+'size=A4&'+'portrait=True&'+'scale=4&'+'top_margin=0.70&'+
   'bottom_margin=0.30&'+'left_margin=0.40&'+'right_margin=0.30&'
   var namepdf = newname + ".pdf";
   var pdf = UrlFetchApp.fetch(fetch, request);
   pdf = pdf.getBlob().setName(namepdf);
   var file = foldersave.createFile(pdf)

Upvotes: 1

Ryan Capule
Ryan Capule

Reputation: 143

So I believe that the

+ 'scale=3'

portion of the code does work. The URL is just particular about spacing. In my original question I had spaces between the = and 3. Writing it like above allows for the page to be scaled.

Upvotes: 0

Related Questions