Embed PDF in mobile browsers

天大地大妈咪最大 提交于 2020-01-13 13:57:14

问题


I have the code below which is a perfect solution to what I need, which would essentially be embedding any of JPG, GIF, PNG or PDF files in my webpages. It works perfectly in PC browsers, but a critical requirement for the pages is to have them compatible in mobile browsers due to its target users.

<iframe src="uploads/test1.pdf" width="auto" height="auto"> </iframe>

Although image files work fine, PDF files are opened separately in the mobile browser and not embedded inline in the web page. What would be an alternative solution or implementation to this?


回答1:


You can use PDFJs library. Using just JS you can render pdf files. Please , check here : https://mozilla.github.io/pdf.js/




回答2:


One simple option is that the the object element provides a fallback, so you can do:

<object data='some.pdf'>
    <p>Oops! You don't support PDFs!</p>
    <p><a href="some.pdf">Download Instead</a>
</object>

Then, when the mobile browser can't get the item, it'll just show this and you'll be all set, kinda.




回答3:


The only way I have found, which allows you to embed pdf is using Google drive, then select the menu button once you have opened your file, and select get embed code, you can only use a Google drive or Google docs reference. And you must also turn public sharing on otherwise others won't be able to view it without permission.




回答4:


Use an object tag with a iframe tag inside your object tags.

The object data can be a pdf or png file by changing the type and can use any link you want stored wherever, however the I frame is the one which will be loaded for mobiles which has to be a link from Google drive or Google docs you also need to allow the files to be shared public otherwise others won't be able to view them.

I would share the code but this site has some rubbish rules about code and won't let me share them so I'll leave you to Google how object and iframe tags work by viewing better sites that actually wants the help from developers.

Have fun guys!




回答5:


I ran into the same issue. As a new developer, I wasn't aware that mobile browsers have issues embedding PDF files in iframes. I am now... lol

I racked my resources trying to get this to work when it dawned on me that mobile browsers do not have an issue displaying PNG files in a new window. So, in my infinite wisdom, I opened the PDF files in Gimp 2.0 then exported them as PNG files. And then I created buttons for the user to click and now it opens the graphic instead of trying to embed the PDF. Looks like this:

<input class="AG" id="UnityBtn" type="button" value="Unity" onclick="location.href='../Meeting_Info/Unity.png'"  />

I don't know if this is possible for you, but it worked beautifully for me.



来源:https://stackoverflow.com/questions/36382249/embed-pdf-in-mobile-browsers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!