簡體   English   中英

從 Google Apps Script and Drive 下載的空白 PDF API

[英]Blank PDF downloaded from Google Apps Script and Drive API

我正在嘗試使用 GAS 在 web 應用程序中從 Drive 下載 PDF 文件。 下載了一個PDF的文件,但是頁面是空白的。 我可以在瀏覽器或 Adobe Acrobat 中打開文件。

在 GAS 中,我使用驅動器 API 獲取文件。

function downloadPDF(){
  let url = "https://www.googleapis.com/drive/v3/files/<file id>?supportsAllDrives=true&alt=media";

  let response = getGoogleAPI(url); // A function I created call the Drive API
  Logger.log("Response: %s", response); // This shows binary data

  return Utilities.base64Encode(response.getContent());
  
}

在前端我有以下代碼:

function getPDF(){

    google.script.run.withSuccessHandler(function(response){
      $("#overlay").hide(); 
      
      let decoded = atob(response);
      console.log(decoded); // This shows the same binary data as from Apps Script
      
      let blob = new Blob(decoded, {type: "application/pdf"});

      const objectUrl = URL.createObjectURL(blob);

      const link = document.createElement('a')

      link.setAttribute('href', objectUrl)
      link.setAttribute('download', "test.pdf")
      link.style.display = 'none'

      document.body.appendChild(link)
    
      link.click()
    
      document.body.removeChild(link)
      
    }).downloadPDF();
}
  

有誰知道問題出在哪里?

提前致謝!

我在閱讀@Tanaike 的回復時看到了這篇文章。

我更改了這行代碼:

link.setAttribute('href', objectUrl)

對此:

link.setAttribute('href', 'data:text/html;base64,' + response)

它奏效了!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM