forked from pipwerks/PDFObject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdfobject.js
244 lines (159 loc) · 5.72 KB
/
pdfobject.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
PDFObject v1.2.20111123
https://github.com/pipwerks/PDFObject
Copyright (c) Philip Hutchison
MIT-style license: http://pipwerks.mit-license.org/
*/
/*jslint browser: true, sloppy: true, white: true, plusplus: true */
/*global ActiveXObject, window */
var PDFObject = function (obj){
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,
//declare functions
createAXO,
hasReaderActiveX,
hasReader,
hasGeneric,
pluginFound,
setCssForFullWindowPdf,
buildQueryString,
get,
embed;
/* ----------------------------------------------------
Supporting functions
---------------------------------------------------- */
createAXO = function (type){
var ax;
try {
ax = new ActiveXObject(type);
} catch (e) {
//ensure ax remains null
ax = null;
}
return ax;
};
//Tests specifically for Adobe Reader (aka Acrobat) in Internet Explorer
hasReaderActiveX = function (){
var axObj = null;
if (window.ActiveXObject) {
axObj = createAXO("AcroPDF.PDF");
//If "AcroPDF.PDF" didn't work, try "PDF.PdfCtrl"
if(!axObj){ axObj = createAXO("PDF.PdfCtrl"); }
//If either "AcroPDF.PDF" or "PDF.PdfCtrl" are found, return true
if (axObj !== null) { return true; }
}
//If you got to this point, there's no ActiveXObject for PDFs
return false;
};
//Tests specifically for Adobe Reader (aka Adobe Acrobat) in non-IE browsers
hasReader = function (){
var i,
n = navigator.plugins,
count = n.length,
regx = /Adobe Reader|Adobe PDF|Acrobat/gi;
for(i=0; i<count; i++){
if(regx.test(n[i].name)){
return true;
}
}
return false;
};
//Detects unbranded PDF support
hasGeneric = function (){
var plugin = navigator.mimeTypes["application/pdf"];
return (plugin && plugin.enabledPlugin);
};
//Determines what kind of PDF support is available: Adobe or generic
pluginFound = function (){
var type = null;
if(hasReader() || hasReaderActiveX()){
type = "Adobe";
} else if(hasGeneric()) {
type = "generic";
}
return type;
};
//If setting PDF to fill page, need to handle some CSS first
setCssForFullWindowPdf = function (){
var html = document.getElementsByTagName("html"),
html_style,
body_style;
if(!html){ return false; }
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";
};
//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)) {
string += prop + "=";
if(prop === "search") {
string += encodeURI(pdfParams[prop]);
} else {
string += pdfParams[prop];
}
string += "&";
}
}
//Remove last ampersand
return string.slice(0, string.length - 1);
};
//Simple function for returning values from PDFObject
get = function(prop){
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;
}
return value;
};
/* ----------------------------------------------------
PDF Embedding functions
---------------------------------------------------- */
embed = function(targetID){
if(!pluginTypeFound){ return false; }
var targetNode = null;
if(targetID){
//Allow users to pass an element OR an element's ID
targetNode = (targetID.nodeType && targetID.nodeType === 1) ? targetID : document.getElementById(targetID);
//Ensure target element is found in document before continuing
if(!targetNode){ return false; }
} else {
targetNode = document.body;
setCssForFullWindowPdf();
width = "100%";
height = "100%";
}
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;
return this;
};