0

I have the basic script working fine. It does exactly what I want it to do. It takes the Google Sheet, turns it into a PDF, and emails me the PDF.

My question is, how can I adjust the margins on the PDF? I need to set the PDF to fit the page. I can't just resize the sheet because it throws the spacing off.

    /* Email Google Spreadsheet as PDF */
function PDF() {
  
  // Send the PDF of the spreadsheet to this email address
  var email = "gmail.com"; 
  
  // Get the currently active spreadsheet URL (link)
  var ss = SpreadsheetApp.openByUrl(
     'https://docs.google.com');

  // Subject of email message
  var subject = "PAR - " + ss.getRange("A6:A6").getValue() +" - "+ ss.getRange("A5:A5").getValue(); 

  // Email Body can  be HTML too 
  var body = "Name -  " + ss.getRange("A6:A6").getValue() +" - "+ ss.getRange("A5:A5").getValue();
  
  var blob = DriveApp.getFileById(ss.getId()).getAs("application/pdf");
  
  blob.setName("Name - " + ss.getRange("A6:A6").getValue() +" - "+ ss.getRange("A5:A5").getValue() + ".pdf");
  
  // If allowed to send emails, send the email with the PDF attachment
  if (MailApp.getRemainingDailyQuota() > 0) 
    GmailApp.sendEmail(email, subject, body, {
      htmlBody: body,
      attachments:[blob]
    });
}

I've seen a script like this but can't figure out how to get it to work on my script.

var url_ext = 'exportFormat=pdf&format=pdf'        // export as pdf / csv / xls / xlsx
  + '&size=letter'                       // paper size legal / letter / A4
  + '&portrait=false'                    // orientation, false for landscape
  + '&fitw=true&source=labnol'           // fit to page width, false for actual size
  + '&sheetnames=false&printtitle=false' // hide optional headers and footers
  + '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
  + '&fzr=false'                         // do not repeat row headers (frozen rows) on each page
  + '&gid=';                             // the sheet's Id
Rubén
  • 34,714
  • 9
  • 70
  • 166
Culper711
  • 41
  • 2
  • 6
  • I dont think Apps Script supports scaling as of yet aside from the default settings provided. You can check [Drive Service](https://developers.google.com/apps-script/reference/drive/) but seems to be no mention of this feature yet. – ReyAnthonyRenacia Aug 01 '17 at 07:52

1 Answers1

1

I've got the same issue, I also want to remove the margins. It is is any help, here is my working script, which includes the part you are mentioning above. But I don't see a parameter where you can adjust the margins...

function CreaPDF() {

  var report = SpreadsheetApp.getActive();            
  var pdfName = "ReportXXX";
  var sheetName = "Sheet1";
  var sourceSheet = report.getSheetByName(sheetName);

  SpreadsheetApp.getActiveSpreadsheet().toast('Creating the PDF');

  // export url
  var url = 'https://docs.google.com/spreadsheets/d/'+report.getId()+'/export?exportFormat=pdf&format=pdf' // export as pdf / csv / xls / xlsx
  + '&size=A4'                           // paper size legal / letter / A4
  + '&portrait=false'                     // orientation, false for landscape
  + '&fitw=true'                        // fit to page width, false for actual size
  + '&sheetnames=false&printtitle=false' // hide optional headers and footers
  + '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
  + '&fzr=false'                         // do not repeat row headers (frozen rows) on each page
  + '&gid='+sourceSheet.getSheetId();    // the sheet's Id

  var token = ScriptApp.getOAuthToken();

  // request export url
  var response = UrlFetchApp.fetch(url, {
    headers: {
      'Authorization': 'Bearer ' +  token
    }
  });

  var theBlob = response.getBlob().setName(pdfName+'.pdf');
  
  //var attach = {fileName:'Monthly Report.pdf',content:pdf, mimeType:'application/pdf'};
  
  var name = report.getRange("H1:H1").getValues();   // Get Name
  var emailTo = report.getRange("H2:H2").getValues();   // Get email
  var period = report.getRange("H3:H3").getValues();   // Get Reporting Period
  var subject = " - TEST Monthly Report - " + period;   // Construct the Subject Line
  var message = "Hi " + name + ", here is your latest report for " + period; // email body text

  // Send the freshly constructed email 
  MailApp.sendEmail(emailTo, subject, message, {attachments:[theBlob]}); 


}
Rubén
  • 34,714
  • 9
  • 70
  • 166
martinval
  • 459
  • 4
  • 19
  • What do you mean by *"It is is any help"*? – Pang Sep 21 '17 at 00:33
  • 4
    These are the parameters that will set the margins to 0: "top_margin=0&left_margin=0&right_margin=0&bottom_margin=0" – kelo Nov 14 '17 at 19:41
  • In addition to the above comment, if you later choose to use generated URL in something like *CreateHTMLOutput*, you will need to replace your ampersands with _&amp_; otherwise substrings _'&top'_ and _'&bottom'_ will get misinterpreted and converted to _'%E2%8A%A4'_ and _'%E2%8A%A5'_ – Art Gertner Apr 27 '18 at 09:27