title: Download PDF from google.script.run in web app
body: <p>What I'm trying to achieve is to download sheet as .PDF file from google script web app, but I have a problem: output file has (probably) wrong encoding (UTF-8 also doesn't work).</p>
  
  <p>Example:</p>
  
  <p>Good file:
  downloaded from link with "export?exportFormat=pdf..."</p>
  
  <pre><code>%PDF-1.4
  % âăĎÓ
  4
  0
  obj
  &lt;&lt;
  /Type
  /Catalog
  /Names
  &lt;&lt;
  /JavaScript
  3
  
  ...
  </code></pre>
  
  <p>Corrupted file:</p>
  
  <pre><code>%PDF-1.4
  % ????
  4
  0
  obj
  &lt;&lt;
  /Type
  /Catalog
  /Names
  &lt;&lt;
  /JavaScript
  3
  
  ...
  </code></pre>
  
  <p>Did anyone experienced similar problem? How can I solve it? Thank you in advance.</p>
  
  <p>Google script:</p>
  
  <pre><code>function doGet(e) {
      return HtmlService.createTemplateFromFile('index').evaluate();
  }
  
  function test() {
    var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(),
        sheet = spreadsheet.getActiveSheet();
  
    var url = spreadsheet.getUrl().replace(/edit$/, '');
    var url_ext = 'export?exportFormat=pdf&amp;format=pdf&amp;gid=' + 
                   sheet.getSheetId();
  
    var options = {
      headers: {
        'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken(),
      }
    };
  
    var response = UrlFetchApp.fetch(url + url_ext, options);
    var blob = response.getBlob();
  
    var charset = 'ISO-8859-1';
    var str = blob.getDataAsString(charset);
  
    return Utilities.base64Encode(str);
  }
  </code></pre>
  
  <p>HTML JavaScript (index.html):</p>
  
  <pre><code>...
  
  function downloadURI(uri, name) {
    var link = document.createElement('a');
    link.download = name;
    link.href = uri;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    delete link;
  }
  
  google.script.run.withSuccessHandler((data) =&gt; {
  
    var uri = 'data:application/pdf;charset=ISO-8859-1;base64,' + encodeURIComponent(data);
    downloadURI(uri, 'test.pdf');
  
  }).withFailureHandler((err) =&gt; {
    console.log(err);
  }).test();
  </code></pre>
  
  <p><a href="https://docs.google.com/spreadsheets/d/1kCcxRbucbvKRSCMC4fNj9NJbjTJHTTBFXP7TLGmDNtQ/" rel="nofollow noreferrer">https://docs.google.com/spreadsheets/d/1kCcxRbucbvKRSCMC4fNj9NJbjTJHTTBFXP7TLGmDNtQ/</a></p>