From 0634428640543d6bcb0b99fdad4fc8068e3f4026 Mon Sep 17 00:00:00 2001 From: Philip Hutchison Date: Mon, 11 Apr 2016 01:03:18 -0700 Subject: [PATCH] PDFObject 2.0! The long awaited updated. Completely rewritten. See details at pdfobject.com --- pdfobject.js | 293 ++++++++++++++++++++-------------------- pdfobject.min.js | 6 +- readme.txt => readme.md | 15 +- 3 files changed, 161 insertions(+), 153 deletions(-) rename readme.txt => readme.md (74%) diff --git a/pdfobject.js b/pdfobject.js index 75ce173..8a02575 100644 --- a/pdfobject.js +++ b/pdfobject.js @@ -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; /* ---------------------------------------------------- @@ -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"; + 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 = ""; - } + 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 = "

This browser does not support inline PDFs. Please download the PDF to view it: Download PDF

"; - //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 = ''; - - 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; })() + }; -}; \ No newline at end of file +})(); \ No newline at end of file diff --git a/pdfobject.min.js b/pdfobject.min.js index fa70f67..e6b5437 100644 --- a/pdfobject.min.js +++ b/pdfobject.min.js @@ -1,7 +1,7 @@ /* - 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/ */ -var PDFObject=function(h){if(!h||!h.url){return false}var e="1.2",c=h.id||false,d=h.width||"100%",p=h.height||"100%",g=h.pdfOpenParams,a,m,l,b,j,i,n,o,q,f,k;l=function(r){var s;try{s=new ActiveXObject(r)}catch(t){s=null}return s};b=function(){var r=null;if(window.ActiveXObject){r=l("AcroPDF.PDF");if(!r){r=l("PDF.PdfCtrl")}if(r!==null){return true}}return false};j=function(){var r,u=navigator.plugins,s=u.length,t=/Adobe Reader|Adobe PDF|Acrobat/gi;for(r=0;r';return s.getElementsByTagName("object")[0]};a=encodeURI(h.url)+"#"+q(g);m=n();this.get=function(r){return f(r)};this.embed=function(r){return k(r)};this.pdfobjectversion=e;return this}; \ No newline at end of file +var PDFObject=function(){"use strict";var pdfobjectversion="2.0.20160402",supportsPDFs,createAXO,isIE,supportsPdfMimeType,supportsPdfActiveX,isPdfSupported,buildQueryString,log,embedError,embed,getTargetElement,generatePDFJSiframe,isIOS=function(){return/iphone|ipad|ipod/i.test(navigator.userAgent.toLowerCase())}(),generateEmbedElement;createAXO=function(type){var ax;try{ax=new ActiveXObject(type)}catch(e){ax=null}return ax};isIE=function(){return!!window.ActiveXObject||!!("ActiveXObject"in window)};supportsPdfMimeType=function(){return typeof navigator.mimeTypes["application/pdf"]!=="undefined"};supportsPdfActiveX=function(){return!!(createAXO("AcroPDF.PDF")||createAXO("PDF.PdfCtrl"))};isPdfSupported=function(){return supportsPdfMimeType()||isIE()&&supportsPdfActiveX()};buildQueryString=function(pdfParams){var string="",prop;if(pdfParams){for(prop in pdfParams){if(pdfParams.hasOwnProperty(prop)){string+=prop+"=";string+=prop==="search"?encodeURI(pdfParams[prop]):pdfParams[prop];string+="&"}}if(string){string="#"+string;string=string.slice(0,string.length-1)}}return string};log=function(msg){if(typeof console!=="undefined"&&console.log){console.log("[PDFObject] "+msg)}};embedError=function(msg){log(msg);return false};getTargetElement=function(targetSelector){var targetNode=document.body;if(typeof targetSelector==="string"){targetNode=document.querySelector(targetSelector)}else if(typeof jQuery!=="undefined"&&targetSelector instanceof jQuery&&targetSelector.length){targetNode=targetSelector.get(0)}else if(typeof targetSelector.nodeType!=="undefined"&&targetSelector.nodeType===1){targetNode=targetSelector}return targetNode};generatePDFJSiframe=function(targetNode,url,PDFJS_URL,id){var querystring=PDFJS_URL+"?file="+url;var scrollfix=isIOS?"-webkit-overflow-scrolling: touch; overflow-y: scroll; ":"overflow: hidden; ";var iframe="
";targetNode.className+=" pdfobject-container";targetNode.style.position="relative";targetNode.style.overflow="auto";targetNode.innerHTML=iframe;return targetNode.getElementsByTagName("iframe")[0]};generateEmbedElement=function(targetNode,targetSelector,url,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="";return targetNode.getElementsByTagName("embed")[0]};embed=function(url,targetSelector,options){if(typeof url!=="string"){return embedError("URL is not valid")}targetSelector=typeof targetSelector!=="undefined"?targetSelector:false;options=typeof options!=="undefined"?options:{};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="

This browser does not support inline PDFs. Please download the PDF to view it: Download PDF

";if(!targetNode){return embedError("Target element cannot be determined")}if(page){pdfOpenParams.page=page}url=encodeURI(url)+buildQueryString(pdfOpenParams);if(forcePDFJS&&PDFJS_URL){return generatePDFJSiframe(targetNode,url,PDFJS_URL,id)}else if(supportsPDFs){return generateEmbedElement(targetNode,targetSelector,url,width,height,id)}else{if(PDFJS_URL){return generatePDFJSiframe(targetNode,url,PDFJS_URL,id)}else if(fallbackLink){fallbackHTML=typeof fallbackLink==="string"?fallbackLink:fallbackHTML_default;targetNode.innerHTML=fallbackHTML.replace(/\[url\]/g,url)}return embedError("This browser does not support embedded PDFs")}};supportsPDFs=isPdfSupported();return{embed:function(a,b,c){return embed(a,b,c)},pdfobjectversion:function(){return pdfobjectversion}(),supportsPDFs:function(){return supportsPDFs}()}}(); \ No newline at end of file diff --git a/readme.txt b/readme.md similarity index 74% rename from readme.txt rename to readme.md index 697daa3..976fdfd 100644 --- a/readme.txt +++ b/readme.md @@ -1,5 +1,18 @@ +Project Update: April 2016 +I'm pleased to announce PDFObject 2.0 has arrived! + +Completely rewritten for the HTML5 era, PDFObject 2.0 has BREAKING CHANGES and is not backwards-compatible. + +For examples, instructions, and a general good time, check out the recently updated http://pdfobject.com + +I have completed initial testing in IE9-11, MS Edge, Safari, Firefox, Chrome, and iOS Safari. PDF embedding is not supported by Safari in iOS -- it is not a PDFObject shortcoming! + +If you find any issues, please report them here. + + + Project update: March 2016 -Hello. I've finally written a 2.0 update, which I hope to release by the end of the month. (I do this in my spare time, which is in short supply these days.) +Hello. I've finally written a 2.0 update, which I hope to release by the end of the month. (I do this in my spare time, which is in short supply these days.) The new 2.0 version supports IE, supports PDF.js in Firefox, and (IMHO) includes some much-needed modernization. The changes are _breaking_ changes -- new, cleaner syntax -- but are worth it. I hope you agree.