UrlFetch from Google sheet exportLink ['application / pdf'] does not return PDF data
I am creating and sending a periodic email as an update from a Google Sheet. For various reasons, the customer is done in three ways, as a reference to the sheet, and as an attachment ( PDF
and XLSX
).
This worked quite recently. The XSLX application is still running, but the PDF is no longer sent as a response to UrlFetch
a URL file.exportLinks('application/pdf')
. No matter the request headers are always returned asContent-Type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Has anything else, undocumented, missing here changed?
function exportAsPDF(spreadsheetId) {
spreadsheetId = spreadsheetId || 'SECRET_ID';
var file = Drive.Files.get(spreadsheetId),
url = file.exportLinks['application/pdf'];
url += '&format=pdf&size=7&fzr=true&portrait=true&fitw=true&gid=0&gridlines=false&printtitle=false&sheetnames=false&pagenum=UNDEFINED&attachment=true'
var token = ScriptApp.getOAuthToken(),
response = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + token
}
});
var headers = response.getAllHeaders(); // revealing content-type returned isn't pdf
var pdfBlob = response.getBlob().getAs('application/pdf');
var pdfString = pdfBlob.getDataAsString(); // this naturally throws an error
return response.getBlob(); // this returns to the send mail script
}
source to share
I can get PDF files using the utility from Convert All Sheets to PDF with Google Apps Script .
That working script changes the spreadsheet edit url to the export url that looks like this:
https://docs.google.com/spreadsheets/d/<%SS-ID%>/export?exportFormat=pdf...
The advanced Drive service provides an export URL, for example:
https://docs.google.com/spreadsheets/export?id=<%SS-ID%>&exportFormat=pdf...
I expect the url provided exportLinks
to be more reliable than a hack in a working script. Apparently this is not the case.
This is caused as Issue 5114 . Star to receive updates.
source to share