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.
This commit is contained in:
Daniel Johnson 2017-02-17 17:15:40 -08:00
parent cfaa621a05
commit 4fc64ceb76
2 changed files with 11 additions and 9 deletions

View File

@ -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;
}
})();

View File

@ -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);
}