Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed PDF in mobile browsers

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?

like image 325
Lane Avatar asked Apr 03 '16 06:04

Lane


People also ask

Do mobile browsers support PDF Embedding?

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. Although image files work fine, PDF files are opened separately in the mobile browser and not embedded inline in the web page.

How can I view PDF in mobile browser?

No browsers can support native rendering of PDF files without a plugin (except Google Chrome as claimed here). But you can you Google Docs Viewer to display PDF files inside a webpage like explained here. Show activity on this post. If embedding isn't important to you you could just link to the PDF on a seperate page.

Can you iframe a PDF?

The simplest one-line solution The best way I found to display a PDF was to use an iframe. iframe stands for inline frame, and it allows you to embed another HTML document within the current one. You can read more about it here.


1 Answers

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.

like image 154
mlissner Avatar answered Sep 23 '22 11:09

mlissner