|
|
|
@ -14,6 +14,7 @@
@@ -14,6 +14,7 @@
|
|
|
|
|
* See the License for the specific language governing permissions and |
|
|
|
|
* limitations under the License. |
|
|
|
|
*/ |
|
|
|
|
/* globals URL*/ |
|
|
|
|
/* globals PDFJS, PDFBug, FirefoxCom, Stats, Cache, PDFFindBar */ |
|
|
|
|
/* globals PDFFindController, ProgressBar, getFileName, CustomStyle */ |
|
|
|
|
/* globals getOutputScale, TextLayerBuilder */ |
|
|
|
@ -914,22 +915,40 @@ var PDFView = {
@@ -914,22 +915,40 @@ var PDFView = {
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
download: function pdfViewDownload() { |
|
|
|
|
function noData() { |
|
|
|
|
FirefoxCom.request('download', { originalUrl: url }); |
|
|
|
|
} |
|
|
|
|
var url = this.url.split('#')[0]; |
|
|
|
|
function getPDFFileNameFromURL(url) { |
|
|
|
|
var reURI = /^(?:([^:]+:)?\/\/[^\/]+)?([^?#]*)(\?[^#]*)?(#.*)?$/; |
|
|
|
|
// SCHEME HOST 1.PATH 2.QUERY 3.REF
|
|
|
|
|
// Pattern to get last matching NAME.pdf
|
|
|
|
|
var reFilename = /[^\/?#=]+\.pdf\b(?!.*\.pdf\b)/i; |
|
|
|
|
var splitURI = reURI.exec(url); |
|
|
|
|
var suggestedFilename = reFilename.exec(splitURI[1]) || |
|
|
|
|
reFilename.exec(splitURI[2]) || |
|
|
|
|
reFilename.exec(splitURI[3]); |
|
|
|
|
if (suggestedFilename) { |
|
|
|
|
suggestedFilename = suggestedFilename[0]; |
|
|
|
|
if (suggestedFilename.indexOf('%') != -1) { |
|
|
|
|
// URL-encoded %2Fpath%2Fto%2Ffile.pdf should be file.pdf
|
|
|
|
|
try { |
|
|
|
|
suggestedFilename = |
|
|
|
|
reFilename.exec(decodeURIComponent(suggestedFilename))[0]; |
|
|
|
|
} catch(e) { // Possible (extremely rare) errors:
|
|
|
|
|
// URIError "Malformed URI", e.g. for "%AA.pdf"
|
|
|
|
|
// TypeError "null has no properties", e.g. for "%2F.pdf"
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return suggestedFilename || 'document.pdf'; |
|
|
|
|
} |
|
|
|
|
//#if !(FIREFOX || MOZCENTRAL)
|
|
|
|
|
|
|
|
|
|
var a = document.createElement('a'); |
|
|
|
|
|
|
|
|
|
// If _parent == self, then opening an identical URL with different
|
|
|
|
|
// location hash will only cause a navigation, not a download.
|
|
|
|
|
if (window.top === window && !('download' in a) && |
|
|
|
|
url === window.location.href.split('#')[0]) { |
|
|
|
|
url += url.indexOf('?') === -1 ? '?' : '&'; |
|
|
|
|
function noData() { |
|
|
|
|
triggerSaveAs(url + '#pdfjs.action=download'); |
|
|
|
|
} |
|
|
|
|
function triggerSaveAs(url, blobUrl) { |
|
|
|
|
// If blobUrl is not specified, fall back to non-blob url.
|
|
|
|
|
if (!blobUrl) blobUrl = url; |
|
|
|
|
|
|
|
|
|
url += '#pdfjs.action=download'; |
|
|
|
|
var a = document.createElement('a'); |
|
|
|
|
if (a.click) { |
|
|
|
|
// Use a.click() if available. Otherwise, Chrome might show
|
|
|
|
|
// "Unsafe JavaScript attempt to initiate a navigation change
|
|
|
|
@ -940,13 +959,12 @@ var PDFView = {
@@ -940,13 +959,12 @@ var PDFView = {
|
|
|
|
|
// - Opera 9 - 12.15
|
|
|
|
|
// - Internet Explorer 6 - 10
|
|
|
|
|
// - Safari 6 (5.1- does not support a.click)
|
|
|
|
|
a.href = url; |
|
|
|
|
a.href = blobUrl; |
|
|
|
|
a.target = '_parent'; |
|
|
|
|
// Use a.download if available. This increases the likelihood that
|
|
|
|
|
// the file is downloaded instead of opened by another PDF plugin.
|
|
|
|
|
if ('download' in a) { |
|
|
|
|
var filename = url.match(/([^\/?#=]+\.pdf)/i); |
|
|
|
|
a.download = filename ? filename[1] : 'file.pdf'; |
|
|
|
|
a.download = getPDFFileNameFromURL(url); |
|
|
|
|
} |
|
|
|
|
// <a> must be in the document for IE and recent Firefox versions.
|
|
|
|
|
// (otherwise .click() is ignored)
|
|
|
|
@ -954,20 +972,29 @@ var PDFView = {
@@ -954,20 +972,29 @@ var PDFView = {
|
|
|
|
|
a.click(); |
|
|
|
|
a.parentNode.removeChild(a); |
|
|
|
|
} else { |
|
|
|
|
window.open(url, '_parent'); |
|
|
|
|
if (window.top === window && |
|
|
|
|
blobUrl.split('#')[0] === window.location.href.split('#')[0]) { |
|
|
|
|
// If _parent == self, then opening an identical URL with different
|
|
|
|
|
// location hash will only cause a navigation, not a download.
|
|
|
|
|
var padCharacter = blobUrl.indexOf('?') === -1 ? '?' : '&'; |
|
|
|
|
blobUrl = blobUrl.replace(/#|$/, padCharacter + '$&'); |
|
|
|
|
} |
|
|
|
|
window.open(blobUrl, '_parent'); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
//#else
|
|
|
|
|
// // Document isn't ready just try to download with the url.
|
|
|
|
|
// if (!this.pdfDocument) {
|
|
|
|
|
// noData();
|
|
|
|
|
// return;
|
|
|
|
|
// function noData() {
|
|
|
|
|
// FirefoxCom.request('download', {
|
|
|
|
|
// originalUrl: url,
|
|
|
|
|
// filename: getPDFFileNameFromURL(url)
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// this.pdfDocument.getData().then(
|
|
|
|
|
// function getDataSuccess(data) {
|
|
|
|
|
// var blob = PDFJS.createBlob(data.buffer, 'application/pdf');
|
|
|
|
|
// var blobUrl = window.URL.createObjectURL(blob);
|
|
|
|
|
//
|
|
|
|
|
// FirefoxCom.request('download', { blobUrl: blobUrl, originalUrl: url },
|
|
|
|
|
// function triggerSaveAs(url, blobUrl) {
|
|
|
|
|
// FirefoxCom.request('download', {
|
|
|
|
|
// blobUrl: blobUrl,
|
|
|
|
|
// originalUrl: url,
|
|
|
|
|
// filename: getPDFFileNameFromURL(url)
|
|
|
|
|
// },
|
|
|
|
|
// function response(err) {
|
|
|
|
|
// if (err) {
|
|
|
|
|
// // This error won't really be helpful because it's likely the
|
|
|
|
@ -977,10 +1004,31 @@ var PDFView = {
@@ -977,10 +1004,31 @@ var PDFView = {
|
|
|
|
|
// window.URL.revokeObjectURL(blobUrl);
|
|
|
|
|
// }
|
|
|
|
|
// );
|
|
|
|
|
// },
|
|
|
|
|
// noData // Error occurred try downloading with just the url.
|
|
|
|
|
// );
|
|
|
|
|
// }
|
|
|
|
|
//#endif
|
|
|
|
|
// If the PDF is not ready yet, or if URL.createObjectURL is not supported,
|
|
|
|
|
// just try to download with the url.
|
|
|
|
|
if (!this.pdfDocument || !URL) { |
|
|
|
|
noData(); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
this.pdfDocument.getData().then( |
|
|
|
|
function getDataSuccess(data) { |
|
|
|
|
var blob = PDFJS.createBlob(data.buffer, 'application/pdf'); |
|
|
|
|
//#if GENERIC
|
|
|
|
|
if (navigator.msSaveBlob) { |
|
|
|
|
// IE10 / IE11
|
|
|
|
|
if (!navigator.msSaveBlob(blob, getPDFFileNameFromURL(url))) { |
|
|
|
|
noData(); |
|
|
|
|
} |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
//#endif
|
|
|
|
|
var blobUrl = URL.createObjectURL(blob); |
|
|
|
|
triggerSaveAs(url, blobUrl); |
|
|
|
|
}, |
|
|
|
|
noData // Error occurred try downloading with just the url.
|
|
|
|
|
).then(null, noData); |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
fallback: function pdfViewFallback() { |
|
|
|
|