Download PluginDetect and DummyPDF here...
In order to do generic pdf detection in a browser, you will need 2 things:
1) download the DummyPDF file (just right click and Save As). The DummyPDF file is used by PluginDetect to do detection in browsers that have a built-in PDF viewer.
2)
download the PluginDetect script itself. The download page will allow you to customize the PluginDetect script to include whatever features you desire.
2 Kinds of PDF Readers: Plugin vs Built-in
Many browsers require a plugin in order to display PDF documents. Adobe Reader and Foxit Reader, for example, provide exactly such a plugin. There are some browsers, however, that have a built-in PDF viewer. They are able to display PDF documents without any plugin at all:
1)
Chrome 6+ has a built-in PDF viewer. You have to enable it in the browser. When you do, the "application/pdf" item will be present in the mimetypes array.
2) Firefox 19+ has a built-in viewer.
3) Safari/Mac has a built-in PDF viewer.
4)
Safari/iPad has a built-in PDF viewer.
5) There are other browsers as well that have (or will have) native PDF support.
In cases where the browser has a built-in PDF viewer, but no "application/pdf" mimetype in the navigator.mimeTypes[ ] array,
detection of PDF support requires inserting a small dummy PDF <object> tag into the web page. This detection usually occurs "not on the fly" (NOTF).
OTF vs NOTF
We can say that PDF detection is performed either "on the fly" (OTF) or "not on the fly" (NOTF) in your script. Any single line of Javascript code that is able to both initiate and complete PDF detection is said to be OTF. No Javascript code is executed by the browser in between the initiation and the completion of the pdf detection. All PDF plugins (Such as Adobe Reader, Foxit Reader, etc..) are detected using OTF.
For NOTF, the browser is able to execute Javascript code in between the initiation and the completion of PDF detection. In other words, after you initiate detection the browser will keep executing Javascript commands while you are waiting for the PDF results. NOTF usually only occurs for browsers that have built in PDF capabilities.
A few PluginDetect commands for PDF Reader detection
PluginDetect.getVersion('PDFReader', DummyPDF, detectNonAdobeIE): [Returns null]
Returns null only because PluginDetect will not attempt to detect any plugin version. The reason is that there are multiple vendors for PDF readers, and they use different version numbers. Also, a plugin version is irrelevant when the browser has built-in PDF support and thus requires no plugin.
PluginDetect.isMinVersion('PDFReader', minVersion, DummyPDF, detectNonAdobeIE): [Returns number]
Returns 0 if plugin is installed & enabled. The plugin version is unknown. You can view PDF documents using the browser (with the plugin) and/or the PDF Reader standalone application [depending on the browser settings and the standalone application settings.]
Returns -0.15 (Internet Explorer only) if plugin is installed but not enabled for <object>/<embed> tags. The plugin version is unknown. You can view PDF documents using the browser (with the plugin) and/or the PDF Reader standalone application [depending on the browser settings and the standalone application settings.] When the browser displays a PDF, however, it will not be able to use <object>/<embed> tags to display that PDF.
This result occurs for Internet Explorer when the PDF Reader ActiveX control is disabled in the add-ons menu, or when the <object>/<embed> tag is not security approved to display a PDF (ie. a security popup occurs).
Returns -0.5 if detection has been initiated but is not yet completed. The -0.5 number only occurs for NOTF detection. It is not known yet whether the browser can display PDF documents. When the detection has been completed, the isMinVersion( ) method will return a different number.
Returns -1 if plugin is not installed / not enabled. You cannot view PDF documents within the browser because there is no PDF Reader browser plugin. It is still possible, however, that a PDF Reader standalone application is installed on your computer and can display PDF documents. [But PluginDetect cannot detect standalone applications - it can only detect browser plugins.]
Returns -1.5 (Internet Explorer only) if plugin status is unknown. PluginDetect is unable to determine if a PDF Reader plugin is installed or not because ActiveX is disabled, or ActiveX Filtering is enabled, or detectNonAdobeIE is false/undefined and Adobe Reader was not detected. [Note 1: a PDF Reader plugin can display a PDF document with or without ActiveX in Internet Explorer. Without ActiveX, however, we cannot detect the presence of the plugin and we cannot use <object/embed> tags to display a PDF.] [Note 2: if detectNonAdobeIE is false/undefined and Adobe Reader was not detected, then it is possible that a non-Adobe PDF Reader plugin is installed in the browser. But we have no way of knowing that since no attempt was made to detect any non-Adobe PDF plugins.]
Returns -3 if you supplied a bad input argument to the isMinVersion( ) method.
PluginDetect.onDetectionDone('PDFReader', f, DummyPDF, detectNonAdobeIE): [Returns number]
This method is capable of doing both OTF and NOTF plugin detection. NOTF detection is required for browsers with native PDF support, and possibly for Internet Explorer with non-Adobe PDF Readers. In order for this method to perform NOTF detection, you must specify the DummyPDF input argument.
The method will initiate plugin detection if needed, and will execute the event handler f after the plugin detection results become available. The event handler f automatically receives the PluginDetect object as an input argument such that we have f(PluginDetect){ ... }.
This method is useful as sometimes PDF detection results are available at unpredictable times - meaning that PDF detection is not always possible "on the fly". Hence, the onDetectionDone() method will wait as long as necessary until the PDF detection has completed before executing handler f. You are free to use getVersion() and isMinVersion() inside handler f to see the detection results.
Returns 1 if plugin detection is done (OTF) and handler f has been executed.
Returns 0 if plugin detection is not done yet (NOTF) and so handler f has not been executed yet.
Returns -1 if error (plugin name input argument not specified correctly).
PluginDetect.beforeInstantiate('PDFReader', f):
Executes the event handler f immediately before PluginDetect attempts to instantiate the plugin. [By instantiate, we mean that an instance of the plugin is inserted into your web page. This will cause the plugin to start up and run, assuming it is installed.] The event handler automatically receives the PluginDetect object as an input argument such that we have f(PluginDetect){ ... }.
Sometimes during detection, it may be necessary for PluginDetect to instantiate (or attempt to instantiate) the plugin. Should this attempt be neccessary, the event handler f will run first, and then the plugin will attempt to instantiate. [Note: If the plugin is installed and enabled, then it instantiates. If it is not installed or not enabled, then it will not instantiate. Either way, the handler f will run before the attempt is made.]
In order for the beforeInstantiate() method to work correctly, you must place it BEFORE detection is started for the plugin. In other words, use it before getVersion(pluginName), isMinVersion(pluginName), and onDetectionDone(pluginName). As an example:
var PD = PluginDetect;
var F1 = function($){ ... }; // $ input arg is the PluginDetect object
PD.beforeInstantiate('PDFReader', F1);
var F2 = function($){ var version = $.getVersion('PDFReader'); alert(version); };
PD.onDetectionDone('PDFReader'', F2, 'empty.pdf');
PluginDetect.onWindowLoaded( f ):
Executes the event handler f after the browser window has fully loaded, and after the plugin detection results are available. The event handler f automatically receives the PluginDetect object as an input argument such that we have f(PluginDetect){ ... }. You are free to use getVersion(), isMinVersion(), onDetectionDone(), and beforeInstantiate() inside event handler f.
Event handler f without user input arguments: If the user does not specify any input arguments for event handler f, then the relevant PluginDetect methods are in the format of
PluginDetect.onDetectionDone(pluginName, f, ...)
PluginDetect.onWindowLoaded(f)
PluginDetect.beforeInstantiate(pluginName, f)
When the handler f is executed it automatically receives the PluginDetect object as input such that we have f(PluginDetect){ ... }.
Event handler f with user input arguments: You may specify up to 3 inputs (ie. arg1, arg2, and arg3) for the event handler. The trick is to use an array such as [f, arg1, arg2, arg3]. The relevant PluginDetect methods are in the format of
PluginDetect.onDetectionDone(pluginName, [f, arg1, arg2, arg3], ...)
PluginDetect.onWindowLoaded( [f, arg1, arg2, arg3] )
PluginDetect.beforeInstantiate(pluginName, [f, arg1, arg2, arg3])
When the handler f is executed it automatically receives the PluginDetect object as input such that we have f(PluginDetect, arg1, arg2, arg3){ ... }.
PluginDetect.getInfo('PDFReader', DummyPDF): [object]
Returns an object with several useful properties. The properties are listed below. To simplify matters, we assign this object to a variable:
var INFO = PluginDetect.getInfo('PDFReader', DummyPDF);
INFO.OTF: [number]
Returns 0 if PDF Reader detection has been performed on the fly (OTF).
Returns 1 if PDF Reader detection is being performed not on the fly (NOTF) but is not completed yet.
Returns 2 if PDF Reader detection has been performed not on the fly (NOTF) and is complete.
INFO.DummyPDFused: [Boolean]
Returns true if the DummyPDF file was loaded/used by the PDF Reader plugin during detection. The DummyPDF was temporarily inserted into the web page as a 1 pixel x 1 pixel <object> tag.
Returns false if the DummyPDF was not loaded/used by the PDF Reader plugin during detection. This occurs when the DummyPDF was not needed to accomplish the detection, or when the PDF Reader plugin is not installed/not enabled in the browser.
minVersion: [string or number input argument]
Use a value of '0' here because PluginDetect does not know or care what the PDF reader plugin version is.
DummyPDF: [string input argument, optional but strongly recommended]
This is the path/filename to an empty PDF document that is temporarily inserted into a web page. It is used by PluginDetect to see if a browser has PDF capability. You may download the DummyPDF file here. You are free to rename the DummyPDF file to whatever you wish, as long as the .pdf extension remains the same. We tried to make the file as small as possible.
The path of the DummyPDF can be relative or absolute. If the path is relative, then it is relative to your HTML web page only! If you are using isMinVersion( ) in an external javascript file, the path of DummyPDF is NOT relative to the javascript file - it is still relative to the HTML web page. For example,
DummyPDF = 'empty.pdf' means the file is in the same folder as your web page.
DummyPDF = 'ABC/empty.pdf' means the file is in a subfolder ABC.
Because DummyPDF is usually used during NOTF detection, it is recommended that you use DummyPDF as an input argument to the onDetectionDone( ) method.
detectNonAdobeIE: [input argument, optional]
This input tells PluginDetect whether or not it should try to detect any non-Adobe PDF Readers installed in Internet Explorer.
When the input is 0 or false, then PluginDetect will only try to detect the Adobe PDF Reader for Internet Explorer. The disadvantage to doing this is that any non-Adobe PDF Readers in IE will not be detected. The advantage is that there will/should be no security popups in the browser as a result of this plugin detection.
When the input is 1 or true, then PluginDetect will try to detect both Adobe and non-Adobe PDF Readers installed in Internet Explorer. You need to specify the DummyPDF input argument in this case. The disadvantage here is that the detection of any non-Adobe PDF Readers in IE may produce a security popup in the browser.
You only need to specify this input argument in the very first PluginDetect method( ) that deals with PDFReader detection. (If you do not specify any input value at all, then a default value of false is assumed.) All subsequent PluginDetect methods will remember your initial input.
Limitations of PDF Reader detection
When you install a PDF Reader on your computer, you have 2 ways of viewing PDF documents:
i) You can use the standalone PDF Reader application to view PDF documents.
or
ii) You can use a browser with the PDF Reader plugin to view PDF documents. A PDF document can appear directly inside the browser window, or inside a web page via a <frame> tag/<iframe> tag/<object> tag/<embed> tag.
The problem here is that PluginDetect can only detect the plugin. It cannot detect the standalone application. (But the presence of the plugin implies the presence of the standalone)
For example, a user may adjust the PDF Reader preferences and the browser settings so that the plugin is totally disabled. In this case, PluginDetect will not be able to detect the PDF Reader plugin, yet the user will still be able to view PDF documents in the standalone application.
Example using onDetectionDone( )
In order to be able to detect PDF capability on the widest variety of browsers, you should use the onDetectionDone( ) method. An example of how to use this is as follows:
var $$ = PluginDetect;
var DummyPDF = 'empty.pdf';
var detectNonAdobeIE = 1;
// We assume here that a <div id="pdfresult"></div> is in the <body> tag somewhere, so
// that we have somewhere to place the results from the PDF detection.
var output = 'pdfresult';
function displayPDFresults($$)
{
var status = $$.isMinVersion('PDFReader', 0);
var msg = 'PDF Reader plugin status: ';
if (status >= 0) msg += 'installed & enabled'
else if (status==-0.15) msg += 'installed but not enabled for object/embed tags'
else if (status==-1) msg += 'not installed or not enabled'
else if (status == -1.5) msg += 'unknown'
else if (status==-3) msg+='error...bad input argument to PluginDetect method'
else msg += 'unknown';
var N = document.getElementById(output);
if (N) N.innerHTML += msg;
};
// Display results when PDF detection is completed
$$.onDetectionDone('PDFReader', displayPDFresults, DummyPDF, detectNonAdobeIE);