Skip to content

Commit

Permalink
Add option supportRedirect which uses an iFrame rather than embed f…
Browse files Browse the repository at this point in the history
…or Safari.
  • Loading branch information
John Hunter committed Apr 28, 2020
1 parent 8a41e67 commit 6e57bc0
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions pdfobject.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
embed,
getTargetElement,
generatePDFJSiframe,
generateEmbedElement;
generateEmbedElement,
generateIframeElement;


/* ----------------------------------------------------
Expand All @@ -86,6 +87,11 @@
//Constructed as a method (not a prop) to avoid unneccesarry overhead -- will only be evaluated if needed
isIE = function (){ return !!(window.ActiveXObject || "ActiveXObject" in window); };

// Detect desktop Safari
// Relying on the Apple pushNotifications api, implemented in 10.9
// See https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/NotificationProgrammingGuideForWebsites/PushNotifications/PushNotifications.html
isSafariOsx = 'safari' in window;

//If either ActiveX support for "AcroPDF.PDF" or "PDF.PdfCtrl" are found, return true
//Constructed as a method (not a prop) to avoid unneccesarry overhead -- will only be evaluated if needed
supportsPdfActiveX = function (){ return !!(createAXO("AcroPDF.PDF") || createAXO("PDF.PdfCtrl")); };
Expand All @@ -97,7 +103,7 @@
//Therefore if iOS, we shall assume that PDF support is not available
!isIOS && (
//Modern versions of Firefox come bundled with PDFJS
isFirefoxWithPDFJS ||
isFirefoxWithPDFJS ||
//Browsers that still support the original MIME type check
supportsPdfMimeType || (
//Pity the poor souls still using IE
Expand Down Expand Up @@ -206,6 +212,23 @@

};

generateIframeElement = function (targetNode, targetSelector, url, pdfOpenFragment, width, height, id){

var style = "";

if(targetSelector && targetSelector !== document.body){
style = "width: " + width + "; height: " + height + ";";
} else {
style = "position: absolute; top: 0; right: 0; bottom: 0; left: 0; width: 100%; height: 100%;";
}

targetNode.className += " pdfobject-container";
targetNode.innerHTML = "<iframe sandbox='' " + id + " class='pdfobject' src='" + url + pdfOpenFragment + "' type='application/pdf' style='border: none; " + style + "'/>";

return targetNode.getElementsByTagName("iframe")[0];

};

embed = function(url, targetSelector, options){

//Ensure URL is available. If not, exit now.
Expand All @@ -226,6 +249,7 @@
height = (options.height) ? options.height : "100%",
assumptionMode = (typeof options.assumptionMode === "boolean") ? options.assumptionMode : true,
forcePDFJS = (typeof options.forcePDFJS === "boolean") ? options.forcePDFJS : false,
supportRedirect = (typeof options.supportRedirect === "boolean") ? options.supportRedirect : false,
PDFJS_URL = (options.PDFJS_URL) ? options.PDFJS_URL : false,
targetNode = getTargetElement(targetSelector),
fallbackHTML = "",
Expand Down Expand Up @@ -254,6 +278,11 @@
//If traditional support is provided, or if this is a modern browser and not iOS (see comment for supportsPDFs declaration)
} else if(supportsPDFs || (assumptionMode && isModernBrowser && !isIOS)){

// Safari will not honour redirect responses on embed src.
if (supportRedirect && isSafariOsx) {
return generateIframeElement(targetNode, targetSelector, url, pdfOpenFragment, width, height, id);
}

return generateEmbedElement(targetNode, targetSelector, url, pdfOpenFragment, width, height, id);

//If everything else has failed and a PDFJS fallback is provided, try to use it
Expand Down

0 comments on commit 6e57bc0

Please sign in to comment.