From 4fc64ceb76162d01d9ec38a869a7a96f9b13a4f2 Mon Sep 17 00:00:00 2001 From: Daniel Johnson Date: Fri, 17 Feb 2017 17:15:40 -0800 Subject: [PATCH] iOS Chrome: Fix broken download button The download button in pdf.js doesn't work in iOS Chrome. - It appears to be an issue with URLs from URL.createObjectURL. The URL is correct, but iOS Chrome won't even load the URL when `a.click()` is called in `download_manager.js`. Even if you manually visit the URL, you get a blank page. - Fix this by detecting iOS Chrome and disabling createObjectURL. The `download_manager.js` `download` method wasn't checking `PDFJS.disableCreateObjectURL`, so check it there, too. - Move the navigator.msSaveBlob check earlier, so that this doesn't change IE10 / IE11 behavior. - Remove the !URL check since pdf.js has a URL polyfill now. --- web/compatibility.js | 8 +++++--- web/download_manager.js | 12 ++++++------ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/web/compatibility.js b/web/compatibility.js index 2b4fe85f4..4bed22d79 100644 --- a/web/compatibility.js +++ b/web/compatibility.js @@ -26,6 +26,7 @@ var isAndroidPre3 = /Android\s[0-2][^\d]/.test(userAgent); var isAndroidPre5 = /Android\s[0-4][^\d]/.test(userAgent); var isChrome = userAgent.indexOf('Chrom') >= 0; var isChromeWithRangeBug = /Chrome\/(39|40)\./.test(userAgent); +var isIOSChrome = userAgent.indexOf('CriOS') >= 0; var isIE = userAgent.indexOf('Trident') >= 0; var isIOS = /\b(iPad|iPhone|iPod)(?=;)/.test(userAgent); var isOpera = userAgent.indexOf('Opera') >= 0; @@ -456,10 +457,11 @@ if (typeof PDFJS === 'undefined') { })(); // Checks if possible to use URL.createObjectURL() -// Support: IE +// Support: IE, Chrome on iOS (function checkOnBlobSupport() { - // sometimes IE loosing the data created with createObjectURL(), see #3977 - if (isIE) { + // sometimes IE and Chrome on iOS loosing the data created with + // createObjectURL(), see #3977 and #8081 + if (isIE || isIOSChrome) { PDFJS.disableCreateObjectURL = true; } })(); diff --git a/web/download_manager.js b/web/download_manager.js index 674dfb622..0be8a011b 100644 --- a/web/download_manager.js +++ b/web/download_manager.js @@ -86,12 +86,6 @@ if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC || CHROME')) { }, download: function DownloadManager_download(blob, url, filename) { - if (!URL) { - // URL.createObjectURL is not supported - this.downloadUrl(url, filename); - return; - } - if (navigator.msSaveBlob) { // IE10 / IE11 if (!navigator.msSaveBlob(blob, filename)) { @@ -100,6 +94,12 @@ if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC || CHROME')) { return; } + if (pdfjsLib.PDFJS.disableCreateObjectURL) { + // URL.createObjectURL is not supported + this.downloadUrl(url, filename); + return; + } + var blobUrl = URL.createObjectURL(blob); download(blobUrl, filename); }