Skip to content

Commit

Permalink
PDFObject 2.0!
Browse files Browse the repository at this point in the history
The long awaited updated. Completely rewritten. See details at pdfobject.com
  • Loading branch information
pipwerks committed Apr 11, 2016
1 parent bc025d1 commit 0634428
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 153 deletions.
293 changes: 144 additions & 149 deletions pdfobject.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
/*
PDFObject v1.2.20111123
PDFObject v2.0.20160411
https://github.com/pipwerks/PDFObject
Copyright (c) Philip Hutchison
Copyright (c) 2008-2016 Philip Hutchison
MIT-style license: http://pipwerks.mit-license.org/
*/

/*jslint browser: true, sloppy: true, white: true, plusplus: true */
/*global ActiveXObject, window */
/*global ActiveXObject, window, console, jQuery */
//jshint unused:false, strict: true
var PDFObject = (function (){

var PDFObject = function (obj){
"use strict";
//jshint unused:true

if(!obj || !obj.url){ return false; }

var pdfobjectversion = "1.2",
//Set reasonable defaults
id = obj.id || false,
width = obj.width || "100%",
height = obj.height || "100%",
pdfOpenParams = obj.pdfOpenParams,
url,
pluginTypeFound,
var pdfobjectversion = "2.0.20160402",
supportsPDFs,

//declare functions
createAXO,
hasReaderActiveX,
hasReader,
hasGeneric,
pluginFound,
setCssForFullWindowPdf,
isIE,
supportsPdfMimeType,
supportsPdfActiveX,
isPdfSupported,
buildQueryString,
get,
embed;
log,
embedError,
embed,
getTargetElement,
generatePDFJSiframe,
isIOS = (function (){ return (/iphone|ipad|ipod/i.test(navigator.userAgent.toLowerCase())); })(),
generateEmbedElement;


/* ----------------------------------------------------
Expand All @@ -42,203 +40,200 @@ var PDFObject = function (obj){
try {
ax = new ActiveXObject(type);
} catch (e) {
//ensure ax remains null
ax = null;
ax = null; //ensure ax remains null
}
return ax;
};

//Tests specifically for Adobe Reader (aka Acrobat) in Internet Explorer
hasReaderActiveX = function (){

var axObj = null;
//IE11 still uses ActiveX for Adobe Reader, but IE 11 doesn't expose
//window.ActiveXObject the same way previous versions of IE did
//window.ActiveXObject will evaluate to false in IE 11, but "ActiveXObject" in window evaluates to true
//so check the first one for older IE, and the second for IE11
//FWIW, MS Edge (replacing IE11) does not support ActiveX at all, both will evaluate false
isIE = function (){ return !!(window.ActiveXObject) || !!("ActiveXObject" in window); };

if (window.ActiveXObject) {
//Invoke immediately, this value will be required below.
//If kept as function call, it would be re-evaluated over and over.
supportsPdfMimeType = function () { return (typeof navigator.mimeTypes['application/pdf'] !== "undefined"); };

axObj = createAXO("AcroPDF.PDF");
//If either ActiveX support for "AcroPDF.PDF" or "PDF.PdfCtrl" are found, return true
supportsPdfActiveX = function (){ return !!(createAXO("AcroPDF.PDF") || createAXO("PDF.PdfCtrl")); };

//If "AcroPDF.PDF" didn't work, try "PDF.PdfCtrl"
if(!axObj){ axObj = createAXO("PDF.PdfCtrl"); }
//Determines whether PDF support is available
isPdfSupported = function (){ return supportsPdfMimeType() || (isIE() && supportsPdfActiveX()); };

//If either "AcroPDF.PDF" or "PDF.PdfCtrl" are found, return true
if (axObj !== null) { return true; }

}
//Creating a querystring for using PDF Open parameters when embedding PDF
buildQueryString = function(pdfParams){

//If you got to this point, there's no ActiveXObject for PDFs
return false;
var string = "",
prop;

};
if(pdfParams){

for (prop in pdfParams) {
if (pdfParams.hasOwnProperty(prop)) {
string += prop + "=";
string += (prop === "search") ? encodeURI(pdfParams[prop]) : pdfParams[prop];
string += "&";
}
}

//The string will be empty if no PDF Params found
if(string){

//Tests specifically for Adobe Reader (aka Adobe Acrobat) in non-IE browsers
hasReader = function (){
string = "#" + string;

var i,
n = navigator.plugins,
count = n.length,
regx = /Adobe Reader|Adobe PDF|Acrobat/gi;
//Remove last ampersand
string = string.slice(0, string.length - 1);

for(i=0; i<count; i++){
if(regx.test(n[i].name)){
return true;
}

}

return false;
return string;

};


//Detects unbranded PDF support
hasGeneric = function (){
var plugin = navigator.mimeTypes["application/pdf"];
return (plugin && plugin.enabledPlugin);
log = function (msg){
if(typeof console !== "undefined" && console.log){
console.log("[PDFObject] " + msg);
}
};

embedError = function (msg){
log(msg);
return false;
};

//Determines what kind of PDF support is available: Adobe or generic
pluginFound = function (){

var type = null;

if(hasReader() || hasReaderActiveX()){

type = "Adobe";

} else if(hasGeneric()) {
getTargetElement = function (targetSelector){

type = "generic";
//Default to body for full-browser PDF
var targetNode = document.body;

}
//If a targetSelector is specified, check to see whether
//it's passing a selector, jQuery object, or an HTML element

return type;
if(typeof targetSelector === "string"){

};
//Is CSS selector
targetNode = document.querySelector(targetSelector);

} else if (typeof jQuery !== "undefined" && targetSelector instanceof jQuery && targetSelector.length) {

//If setting PDF to fill page, need to handle some CSS first
setCssForFullWindowPdf = function (){
//Is jQuery element. Extract HTML node
targetNode = targetSelector.get(0);

var html = document.getElementsByTagName("html"),
html_style,
body_style;
} else if (typeof targetSelector.nodeType !== "undefined" && targetSelector.nodeType === 1){

if(!html){ return false; }
//Is HTML element
targetNode = targetSelector;

html_style = html[0].style;
body_style = document.body.style;
}

html_style.height = "100%";
html_style.overflow = "hidden";
body_style.margin = "0";
body_style.padding = "0";
body_style.height = "100%";
body_style.overflow = "hidden";
return targetNode;

};

generatePDFJSiframe = function (targetNode, url, PDFJS_URL, id){

//Creating a querystring for using PDF Open parameters when embedding PDF
buildQueryString = function(pdfParams){

var string = "",
prop;

if(!pdfParams){ return string; }

for (prop in pdfParams) {

if (pdfParams.hasOwnProperty(prop)) {
var querystring = PDFJS_URL + "?file=" + url;
var scrollfix = (isIOS) ? "-webkit-overflow-scrolling: touch; overflow-y: scroll; " : "overflow: hidden; ";
var iframe = "<div style='" + scrollfix + "position: absolute; top: 0; right: 0; bottom: 0; left: 0;'><iframe " + id + " src='" + querystring + "' style='border: none; width: 100%; height: 100%;' frameborder='0'></iframe></div>";
targetNode.className += " pdfobject-container";
targetNode.style.position = "relative";
targetNode.style.overflow = "auto";
targetNode.innerHTML = iframe;
return targetNode.getElementsByTagName("iframe")[0];

string += prop + "=";
};

if(prop === "search") {
generateEmbedElement = function (targetNode, targetSelector, url, width, height, id){

string += encodeURI(pdfParams[prop]);
var style = "";

} else {
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%;";
}

string += pdfParams[prop];
targetNode.className += " pdfobject-container";
targetNode.innerHTML = "<embed " + id + " class='pdfobject' src='" + url + "' type='application/pdf' style='overflow: auto; " + style + "'/>";

}
return targetNode.getElementsByTagName("embed")[0];

string += "&";
};

}
embed = function(url, targetSelector, options){

}
//Ensure URL is available. If not, exit now.
if(typeof url !== "string"){ return embedError("URL is not valid"); }

//Remove last ampersand
return string.slice(0, string.length - 1);
//If targetSelector is not defined, convert to boolean
targetSelector = (typeof targetSelector !== "undefined") ? targetSelector : false;

};
//Ensure options object is not undefined -- enables easier error checking below
options = (typeof options !== "undefined") ? options : {};

//Get passed options, or set reasonable defaults
var id = (options.id && typeof options.id === "string") ? "id='" + options.id + "'" : "",
page = (options.page) ? options.page : false,
pdfOpenParams = (options.pdfOpenParams) ? options.pdfOpenParams : {},
fallbackLink = (typeof options.fallbackLink !== "undefined") ? options.fallbackLink : true,
width = (options.width) ? options.width : "100%",
height = (options.height) ? options.height : "100%",
forcePDFJS = (typeof options.forcePDFJS === "boolean") ? options.forcePDFJS : false,
PDFJS_URL = (options.PDFJS_URL) ? options.PDFJS_URL : false,
targetNode = getTargetElement(targetSelector),
fallbackHTML = "",
fallbackHTML_default = "<p>This browser does not support inline PDFs. Please download the PDF to view it: <a href='[url]'>Download PDF</a></p>";

//Simple function for returning values from PDFObject
get = function(prop){
//If target element is specified but is not valid, exit without doing anything
if(!targetNode){ return embedError("Target element cannot be determined"); }

var value = null;

switch(prop){
case "url" : value = url; break;
case "id" : value = id; break;
case "width" : value = width; break;
case "height" : value = height; break;
case "pdfOpenParams" : value = pdfOpenParams; break;
case "pluginTypeFound" : value = pluginTypeFound; break;
case "pdfobjectversion" : value = pdfobjectversion; break;
//page option overrides pdfOpenParams, if found
if(page){
pdfOpenParams.page = page;
}

return value;
//Append optional Adobe params for opening document
url = encodeURI(url) + buildQueryString(pdfOpenParams);

};
//Do the dance
if(forcePDFJS && PDFJS_URL){

return generatePDFJSiframe(targetNode, url, PDFJS_URL, id);

/* ----------------------------------------------------
PDF Embedding functions
---------------------------------------------------- */

} else if(supportsPDFs){

embed = function(targetID){
return generateEmbedElement(targetNode, targetSelector, url, width, height, id);

if(!pluginTypeFound){ return false; }
} else {

var targetNode = null;
if(PDFJS_URL){

if(targetID){
return generatePDFJSiframe(targetNode, url, PDFJS_URL, id);

//Allow users to pass an element OR an element's ID
targetNode = (targetID.nodeType && targetID.nodeType === 1) ? targetID : document.getElementById(targetID);
} else if(fallbackLink){

//Ensure target element is found in document before continuing
if(!targetNode){ return false; }
fallbackHTML = (typeof fallbackLink === "string") ? fallbackLink : fallbackHTML_default;
targetNode.innerHTML = fallbackHTML.replace(/\[url\]/g, url);

} else {
}

targetNode = document.body;
setCssForFullWindowPdf();
width = "100%";
height = "100%";
return embedError("This browser does not support embedded PDFs");

}

targetNode.innerHTML = '<object data="' +url +'" type="application/pdf" width="' +width +'" height="' +height +'"></object>';

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

};

//The hash (#) prevents odd behavior in Windows
//Append optional Adobe params for opening document
url = encodeURI(obj.url) + "#" + buildQueryString(pdfOpenParams);
pluginTypeFound = pluginFound();

this.get = function(prop){ return get(prop); };
this.embed = function(id){ return embed(id); };
this.pdfobjectversion = pdfobjectversion;
supportsPDFs = isPdfSupported();

return this;
return {
embed: function (a,b,c){ return embed(a,b,c); },
pdfobjectversion: (function () { return pdfobjectversion; })(),
supportsPDFs: (function (){ return supportsPDFs; })()
};

};
})();
Loading

0 comments on commit 0634428

Please sign in to comment.