'How to check if browser has pdf viewer or not in javascript
I need to check if the browser has ability to show pdf in iframe.
I was trying to simple check mimeTypes or plugins but now in Firefox on mac, it doesn't seem to be working fine, it shows empty array.
function isPDFSupported(): boolean {
let hasPDFViewer = false;
try {
const pdf =
navigator.mimeTypes && (navigator.mimeTypes as any)['application/pdf']
? (navigator.mimeTypes as any)['application/pdf'].enabledPlugin
: 0;
if (pdf) hasPDFViewer = true;
} catch (e) {
if ((navigator.mimeTypes as any)['application/pdf'] != null) hasPDFViewer = true;
}
return hasPDFViewer;
}
also check the developer.mozilla page for mimeTypes https://developer.mozilla.org/en-US/docs/Web/API/Navigator/mimeTypes and it shows that mimeTypes and plugin is deprecated...
Do you know some other alternatives? or just simply do if IE download pdf and in other cases try to show pdf?
Solution 1:[1]
I recently came across the same challenge and found a way to check for a PDF viewer plugin.
As explained in the Mozilla documentation, the navigator.plugins property returns a PluginArray object, which lists all of the plugins that are installed in the application.
PDF inline viewing plugins will contain "PDF viewer" in the plugin name, e.g. "Chrome PDF Viewer" or "Microsoft Edge PDF Viewer". So you can check if any of the plugins contain this string as follows:
if ('PDF Viewer' in navigator.plugins) {
// your code
}
This worked well for me. However, I need to point out that this method is deprecated so I'm not sure how long it will still be supported.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Zoe |