
[英]Attach a pdf from google drive to an email using google apps script
[英]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.