How to know if browser has PDF viewer or not?

前端 未结 4 2237
再見小時候
再見小時候 2021-02-13 04:38

I am viewing PDF into iframe. It works fine. But some of the client don\'t able to see it in IE. They get it as download option.

How can I identify if browser has pdf v

4条回答
  •  被撕碎了的回忆
    2021-02-13 04:50

    PDF format is a licensed adobe format, from my understanding it's probably not always going to be reliable detection if you need to account for all non-licensed PDF readers also.

    However, there is this resource: http://www.pinlady.net/PluginDetect/PDFReader/

    The basic idea is:

    • Detect all plugins running in the browser
    • Search for "application/pdf" in the browser's navigator.mimeTypes array
    • If the navigator.mimeTypes array test fails, a second test is run in the background (i.e. nothing shown to the user on the frontend) which sends the browser a lightweight "empty" pdf file to see if it can be interpreted

    Of course the idea of running a test pdf in the background might be a costly solution (i.e. mobile users on 3G etc.) but it's probably going to be a pretty reliable method, and the good part is you only have to test your user once when they reach your domain and then set a cookie or localStorage item with their pdf config saved so you don't have to keep testing their pdf capability.

提交回复
热议问题