1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | (function (root, factory) {
|
11 | if (typeof define === "function" && define.amd) {
|
12 |
|
13 | define([], factory);
|
14 | } else if (typeof module === "object" && module.exports) {
|
15 |
|
16 |
|
17 |
|
18 | module.exports = factory();
|
19 | } else {
|
20 |
|
21 | root.PDFObject = factory();
|
22 | }
|
23 | }(this, function () {
|
24 |
|
25 | "use strict";
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | if( typeof window === "undefined" ||
|
32 | window.navigator === undefined ||
|
33 | window.navigator.userAgent === undefined ||
|
34 | window.navigator.mimeTypes === undefined){
|
35 | return false;
|
36 | }
|
37 |
|
38 | let pdfobjectversion = "2.2.7";
|
39 | let nav = window.navigator;
|
40 | let ua = window.navigator.userAgent;
|
41 |
|
42 |
|
43 |
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | let isIE = ("ActiveXObject" in window);
|
52 |
|
53 | |
54 |
|
55 |
|
56 |
|
57 |
|
58 | let isModernBrowser = (window.Promise !== undefined);
|
59 |
|
60 |
|
61 | let supportsPdfMimeType = (nav.mimeTypes["application/pdf"] !== undefined);
|
62 |
|
63 |
|
64 | let isSafariIOSDesktopMode = ( nav.platform !== undefined &&
|
65 | nav.platform === "MacIntel" &&
|
66 | nav.maxTouchPoints !== undefined &&
|
67 | nav.maxTouchPoints > 1 );
|
68 |
|
69 |
|
70 | let isMobileDevice = (isSafariIOSDesktopMode || /Mobi|Tablet|Android|iPad|iPhone/.test(ua));
|
71 |
|
72 |
|
73 | let isSafariDesktop = ( !isMobileDevice &&
|
74 | nav.vendor !== undefined &&
|
75 | /Apple/.test(nav.vendor) &&
|
76 | /Safari/.test(ua) );
|
77 |
|
78 |
|
79 | let isFirefoxWithPDFJS = (!isMobileDevice && /irefox/.test(ua) && ua.split("rv:").length > 1) ? (parseInt(ua.split("rv:")[1].split(".")[0], 10) > 18) : false;
|
80 |
|
81 |
|
82 | |
83 |
|
84 |
|
85 |
|
86 | let createAXO = function (type){
|
87 | var ax;
|
88 | try {
|
89 | ax = new ActiveXObject(type);
|
90 | } catch (e) {
|
91 | ax = null;
|
92 | }
|
93 | return ax;
|
94 | };
|
95 |
|
96 |
|
97 |
|
98 | let supportsPdfActiveX = function (){ return !!(createAXO("AcroPDF.PDF") || createAXO("PDF.PdfCtrl")); };
|
99 |
|
100 |
|
101 | let supportsPDFs = (
|
102 |
|
103 | !isMobileDevice && (
|
104 |
|
105 | isModernBrowser ||
|
106 |
|
107 | isFirefoxWithPDFJS ||
|
108 |
|
109 | supportsPdfMimeType ||
|
110 |
|
111 | (isIE && supportsPdfActiveX())
|
112 | )
|
113 | );
|
114 |
|
115 |
|
116 | let buildURLFragmentString = function(pdfParams){
|
117 |
|
118 | let string = "";
|
119 | let prop;
|
120 |
|
121 | if(pdfParams){
|
122 |
|
123 | for (prop in pdfParams) {
|
124 | if (pdfParams.hasOwnProperty(prop)) {
|
125 | string += encodeURIComponent(prop) + "=" + encodeURIComponent(pdfParams[prop]) + "&";
|
126 | }
|
127 | }
|
128 |
|
129 |
|
130 | if(string){
|
131 |
|
132 | string = "#" + string;
|
133 |
|
134 |
|
135 | string = string.slice(0, string.length - 1);
|
136 |
|
137 | }
|
138 |
|
139 | }
|
140 |
|
141 | return string;
|
142 |
|
143 | };
|
144 |
|
145 | let embedError = function (msg, suppressConsole){
|
146 | if(!suppressConsole){
|
147 | console.log("[PDFObject] " + msg);
|
148 | }
|
149 | return false;
|
150 | };
|
151 |
|
152 | let emptyNodeContents = function (node){
|
153 | while(node.firstChild){
|
154 | node.removeChild(node.firstChild);
|
155 | }
|
156 | };
|
157 |
|
158 | let getTargetElement = function (targetSelector){
|
159 |
|
160 |
|
161 | let targetNode = document.body;
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | if(typeof targetSelector === "string"){
|
167 |
|
168 |
|
169 | targetNode = document.querySelector(targetSelector);
|
170 |
|
171 | } else if (window.jQuery !== undefined && targetSelector instanceof jQuery && targetSelector.length) {
|
172 |
|
173 |
|
174 | targetNode = targetSelector.get(0);
|
175 |
|
176 | } else if (targetSelector.nodeType !== undefined && targetSelector.nodeType === 1){
|
177 |
|
178 |
|
179 | targetNode = targetSelector;
|
180 |
|
181 | }
|
182 |
|
183 | return targetNode;
|
184 |
|
185 | };
|
186 |
|
187 | let generatePDFJSMarkup = function (targetNode, url, pdfOpenFragment, PDFJS_URL, id, title, omitInlineStyles){
|
188 |
|
189 |
|
190 | emptyNodeContents(targetNode);
|
191 |
|
192 | let fullURL = PDFJS_URL + "?file=" + encodeURIComponent(url) + pdfOpenFragment;
|
193 | let div = document.createElement("div");
|
194 | let iframe = document.createElement("iframe");
|
195 |
|
196 | iframe.src = fullURL;
|
197 | iframe.className = "pdfobject";
|
198 | iframe.type = "application/pdf";
|
199 | iframe.frameborder = "0";
|
200 | iframe.allow = "fullscreen";
|
201 | iframe.title = title;
|
202 |
|
203 | if(id){
|
204 | iframe.id = id;
|
205 | }
|
206 |
|
207 | if(!omitInlineStyles){
|
208 | div.style.cssText = "position: absolute; top: 0; right: 0; bottom: 0; left: 0;";
|
209 | iframe.style.cssText = "border: none; width: 100%; height: 100%;";
|
210 | targetNode.style.position = "relative";
|
211 | targetNode.style.overflow = "auto";
|
212 | }
|
213 |
|
214 | div.appendChild(iframe);
|
215 | targetNode.appendChild(div);
|
216 | targetNode.classList.add("pdfobject-container");
|
217 |
|
218 | return targetNode.getElementsByTagName("iframe")[0];
|
219 |
|
220 | };
|
221 |
|
222 | let generatePDFObjectMarkup = function (embedType, targetNode, targetSelector, url, pdfOpenFragment, width, height, id, title, omitInlineStyles){
|
223 |
|
224 |
|
225 | emptyNodeContents(targetNode);
|
226 |
|
227 | let embed = document.createElement(embedType);
|
228 | embed.src = url + pdfOpenFragment;
|
229 | embed.className = "pdfobject";
|
230 | embed.type = "application/pdf";
|
231 | embed.title = title;
|
232 |
|
233 | if(id){
|
234 | embed.id = id;
|
235 | }
|
236 |
|
237 | if(embedType === "iframe"){
|
238 | embed.allow = "fullscreen";
|
239 | }
|
240 |
|
241 | if(!omitInlineStyles){
|
242 |
|
243 | let style = (embedType === "embed") ? "overflow: auto;" : "border: none;";
|
244 |
|
245 | if(targetSelector && targetSelector !== document.body){
|
246 | style += "width: " + width + "; height: " + height + ";";
|
247 | } else {
|
248 | style += "position: absolute; top: 0; right: 0; bottom: 0; left: 0; width: 100%; height: 100%;";
|
249 | }
|
250 |
|
251 | embed.style.cssText = style;
|
252 |
|
253 | }
|
254 |
|
255 | targetNode.classList.add("pdfobject-container");
|
256 | targetNode.appendChild(embed);
|
257 |
|
258 | return targetNode.getElementsByTagName(embedType)[0];
|
259 |
|
260 | };
|
261 |
|
262 | let embed = function(url, targetSelector, options){
|
263 |
|
264 |
|
265 | let selector = targetSelector || false;
|
266 |
|
267 |
|
268 | let opt = options || {};
|
269 |
|
270 |
|
271 | let id = (typeof opt.id === "string") ? opt.id : "";
|
272 | let page = opt.page || false;
|
273 | let pdfOpenParams = opt.pdfOpenParams || {};
|
274 | let fallbackLink = opt.fallbackLink || true;
|
275 | let width = opt.width || "100%";
|
276 | let height = opt.height || "100%";
|
277 | let title = opt.title || "Embedded PDF";
|
278 | let assumptionMode = (typeof opt.assumptionMode === "boolean") ? opt.assumptionMode : true;
|
279 | let forcePDFJS = (typeof opt.forcePDFJS === "boolean") ? opt.forcePDFJS : false;
|
280 | let supportRedirect = (typeof opt.supportRedirect === "boolean") ? opt.supportRedirect : false;
|
281 | let omitInlineStyles = (typeof opt.omitInlineStyles === "boolean") ? opt.omitInlineStyles : false;
|
282 | let suppressConsole = (typeof opt.suppressConsole === "boolean") ? opt.suppressConsole : false;
|
283 | let forceIframe = (typeof opt.forceIframe === "boolean") ? opt.forceIframe : false;
|
284 | let PDFJS_URL = opt.PDFJS_URL || false;
|
285 | let targetNode = getTargetElement(selector);
|
286 | let fallbackHTML = "";
|
287 | let pdfOpenFragment = "";
|
288 | let fallbackHTML_default = "<p>This browser does not support inline PDFs. Please download the PDF to view it: <a href='[url]'>Download PDF</a></p>";
|
289 |
|
290 |
|
291 | if(typeof url !== "string"){ return embedError("URL is not valid", suppressConsole); }
|
292 |
|
293 |
|
294 | if(!targetNode){ return embedError("Target element cannot be determined", suppressConsole); }
|
295 |
|
296 |
|
297 | if(page){ pdfOpenParams.page = page; }
|
298 |
|
299 |
|
300 | pdfOpenFragment = buildURLFragmentString(pdfOpenParams);
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 | if(forcePDFJS && PDFJS_URL){
|
307 | return generatePDFJSMarkup(targetNode, url, pdfOpenFragment, PDFJS_URL, id, title, omitInlineStyles);
|
308 | }
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 | if(supportsPDFs || (assumptionMode && !isMobileDevice)){
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 | let embedtype = (forceIframe || supportRedirect || isSafariDesktop) ? "iframe" : "embed";
|
322 |
|
323 | return generatePDFObjectMarkup(embedtype, targetNode, targetSelector, url, pdfOpenFragment, width, height, id, title, omitInlineStyles);
|
324 |
|
325 | }
|
326 |
|
327 |
|
328 |
|
329 |
|
330 | if(PDFJS_URL){
|
331 | return generatePDFJSMarkup(targetNode, url, pdfOpenFragment, PDFJS_URL, id, title, omitInlineStyles);
|
332 | }
|
333 |
|
334 |
|
335 |
|
336 |
|
337 | if(fallbackLink){
|
338 |
|
339 | fallbackHTML = (typeof fallbackLink === "string") ? fallbackLink : fallbackHTML_default;
|
340 | targetNode.innerHTML = fallbackHTML.replace(/\[url\]/g, url);
|
341 |
|
342 | }
|
343 |
|
344 | return embedError("This browser does not support embedded PDFs", suppressConsole);
|
345 |
|
346 | };
|
347 |
|
348 | return {
|
349 | embed: function (a,b,c){ return embed(a,b,c); },
|
350 | pdfobjectversion: (function () { return pdfobjectversion; })(),
|
351 | supportsPDFs: (function (){ return supportsPDFs; })()
|
352 | };
|
353 |
|
354 | }));
|