I am trying to make a href-link (dataUri) work in my google sheet on our Company Terminals' Chrome Browser. The below code works fine on my private PC Chrome Browser, but not in the Company Browser.
I used the code from User TheMaster seen at the following link:
How to download single sheet as PDF (not export to Google Drive)
Chrome Browser Version in Company: Version 83.0.4103.61 (Official Build) (64-Bit)
The problem is, that the automatic download is not initialised. The clickable link does not work on left button press, but one can save the PDF with right click "save under".
Is there any setting that needs to be changed in order to make the automatic download work?
Thank you very much.
function downloadPdfToDesktop() {
var ss = SpreadsheetApp.getActive(),
id = ss.getId(),
sht = ss.getActiveSheet(),
shtId = sht.getSheetId(),
url =
'https://docs.google.com/spreadsheets/d/' +
id +
'/export' +
'?format=pdf&gid=' +
shtId;
var val = 'PDFNAME';//custom pdf name here
val += '.pdf';
//can't download with a different filename directly from server
//download and remove content-disposition header and serve as a dataURI
//Use anchor tag's download attribute to provide a custom filename
var res = UrlFetchApp.fetch(url, {
headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() },
});
SpreadsheetApp.getUi().showModelessDialog(
HtmlService.createHtmlOutput(
'<a target ="_blank" download="' +
val +
'" href = "data:application/pdf;base64,' +
Utilities.base64Encode(res.getContent()) +
'">Click here</a> to download, if download did not start automatically' +
'<script> \
var a = document.querySelector("a"); \
a.addEventListener("click",()=>{setTimeout(google.script.host.close,10)}); \
a.click(); \
</script>'
).setHeight(50),
'Downloading PDF..'
);
}