title: Google Sheet Script (Email to PDF) suddenly sending corrupt PDFs
body: <p>I have a Google Sheet Script that sends the page to an email as a PDF which has been working perfectly until yesterday. Suddenly it started sending corrupted PDF's that can not be opened.</p>
  <p>The Script runs just fine, if just can not open up the PDF file as it says &quot;Can Not Display - Invalid format&quot;.</p>
  <p>Any ideas on why it may have stopped working?</p>
  <pre><code>function sendSheetToPdfwithA1MailAdress(){ // this is the function to call
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sh = ss.getSheets()[0]; // it will send sheet 0 which is the first sheet in the spreadsheet.
    // if you change the number, change it also in the parameters below
    var shName = sh.getName()
      
    // This function uses a cell in the spreadsheet that names the file that is being saved as getfilename().  using this function will pull from a certain Cell (G4 in this case)
  function getFilename() {
  
      var spreadsheet = SpreadsheetApp.getActive();
  
      var sheet = spreadsheet.getSheetByName('N1944E'); // Edit the sheet name as necessary
  
      var cell = sheet.getRange('C8'); //Cell to pull file name from.
      var filename = cell.getValue();
  
      return filename;
  } 
  
    sendSpreadsheetToPdf(0, shName, sh.getRange('C6').getValue(),&quot;Air Attack Daily Fire Sheet &quot; + getFilename() );
  }
  function sendSpreadsheetToPdf(sheetNumber, pdfName, email,subject, htmlbody) {
    var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
    var spreadsheetId = spreadsheet.getId()  
    var sheetId = sheetNumber ? spreadsheet.getSheets()[sheetNumber].getSheetId() : null;  
   var url_base = spreadsheet.getUrl().replace(/edit$/,'');
  
  
  
    var url_ext = 'export?exportFormat=pdf&amp;format=pdf'   //export as pdf
  
        + (sheetId ? ('&amp;gid=' + sheetId) : ('&amp;id=' + spreadsheetId)) 
        // following parameters are optional...
        + '&amp;size=A4'      // paper size
        + '&amp;portrait=true'    // orientation, false for landscape
        + '&amp;fitw=true'        // fit to width, false for actual size
        + '&amp;sheetnames=true&amp;printtitle=false&amp;pagenumbers=true'  //hide optional headers and footers
        + '&amp;gridlines=false'  // hide gridlines
        + '&amp;fzr=false';       // do not repeat row headers (frozen rows) on each page
  
    var options = {
      headers: {
        'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken(),
      }
    }
  
    var response = UrlFetchApp.fetch(url_base + url_ext, options);
    var blob = response.getBlob().setName(pdfName + '.pdf');
    if (email) {
      var mailOptions = {
        attachments:blob, htmlBody:htmlbody
      }
  MailApp.sendEmail(
        email, 
        subject+&quot; (&quot; + pdfName +&quot;)&quot;, 
        &quot;html content only&quot;, 
        mailOptions);
  
  MailApp.sendEmail(
        Session.getActiveUser().getEmail(), 
        &quot; &quot;+subject+&quot; (&quot; + pdfName +&quot;)&quot;, 
        &quot;html content only&quot;, 
        mailOptions);
    }
  }
  </code></pre>