Embed PDF in mobile browsers

2020-07-13 09:42发布

问题:

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! Your browser doesn't support PDFs!</p>
    <p><a href="some.pdf">Download Instead</a></p>
</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:

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.



回答5:

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!