<embed> vs. <object>

HtmlPdfEmbed Tag

Html Problem Overview


Which is the right/best tag to use in my HTML file when I want to display the Adobe PDF viewer?

Right now I'm using the code below, but there are weird side effects (e.g. it seems to steal the starting focus that I've set to another <input> text box; it doesn't seem to play real well with the jQueryUI Resizeable class; etc.)

<embed src="abc.pdf" type="application/pdf" />

Could I even do the same thing with the <object> tag? Are there advantages/disadvantages to using one tag vs. the other?

Html Solutions


Solution 1 - Html

OBJECT vs. EMBED - why not always use embed?

> Bottom line: OBJECT is Good, EMBED is Old. Beside's IE's PARAM tags, any content between OBJECT tags will get rendered if the browser doesn't support OBJECT's referred plugin, and apparently, the content gets http requested regardless if it gets rendered or not.

object is the current standard tag to embed something on a page. embed was included by Netscape (along img) before anything like object were on the w3c mind.

This is how you include a PDF with object:

<object data="data/test.pdf" type="application/pdf" width="300" height="200">
  alt : <a href="data/test.pdf">test.pdf</a>
</object>

If you really need the inline PDF to show in almost every browser, as older browsers understand embed but not object, you'll need to do this:

<object data="abc.pdf" type="application/pdf">
    <embed src="abc.pdf" type="application/pdf" />
</object>

This version does not validate.

Solution 2 - Html

Answer updated for 2020:

Both <object> and <embed> are included in the WHAT-WG HTML Living Standard (Sept 2020).


<object>

> The object element can represent an external resource, which, > depending on the type of the resource, will either be treated as an > image, as a child browsing context, or as an external resource to be > processed by a plugin.


<embed>

> The embed element provides an integration point for an external (typically non-HTML) application or interactive content.


> Are there advantages/disadvantages to using one tag vs. the other?

The opinion of Mozilla Developer Network (MDN) appears (albeit fairly subtly) to very marginally favour <object> over <embed> but, overwhelmingly, MDN wants to recommend that wherever you can, you avoid embedding external content entirely.

> [...] you are unlikely to use these elements very much — Applets > haven't been used for years, Flash is no longer very popular, due to a > number of reasons (see The case against plugins, below), PDFs tend to > be better linked to than embedded, and other content such as images > and video have much better, easier elements to handle those. Plugins > and these embedding methods are really a legacy technology, and we are > mainly mentioning them in case you come across them in certain > circumstances like intranets, or enterprise projects. > > Once upon a time, plugins were indispensable on the Web. Remember the > days when you had to install Adobe Flash Player just to watch a movie > online? And then you constantly got annoying alerts about updating > Flash Player and your Java Runtime Environment. Web technologies have > since grown much more robust, and those days are over. For virtually > all applications, it's time to stop delivering content that depends on > plugins and start taking advantage of Web technologies instead. > > Source: https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Other_embedding_technologies#The_%3Cembed%3E_and_%3Cobject%3E_elements

Solution 3 - Html

Some other options:

<object type="application/pdf" data="filename.pdf" width="100%" height="100%">
</object>

<object type="application/pdf" data="#request.localhost#_includes/filename.pdf" 
        width="100%" height="100%">
  <param name="src" value="#request.localhost#_includes/filename.pdf">
</object>

Solution 4 - Html

You could also use the iframe method, although this is not cross browser compatible (eg. not working in chromium or android and probably others -> instead prompts to download). It works with dataURL's and normal URLS, not sure if the other examples work with dataURLS (please let me know if the other examples work with dataURLS?)

 <iframe class="page-icon preview-pane" frameborder="0" height="352" width="396" src="data:application/pdf;base64, ..DATAURLHERE!... "></iframe>

Solution 5 - Html

Probably the best cross browser solution for pdf display on web pages is to use the Mozilla PDF.js project code, it can be run as a node.js service and used as follows

<iframe style="width:100%;height:500px" src="http://www.mysite.co.uk/libs/pdfjs/web/viewer.html?file="http://www.mysite.co.uk/mypdf.pdf"></iframe>

A tutorial on how to use pdf.js can be found at this ejectamenta blog article

Solution 6 - Html

Embed is not a standard tag, though object is. Here's an article that looks like it will help you, since it seems the situation is not so simple. An example for PDF is included.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJayhawksFan93View Question on Stackoverflow
Solution 1 - HtmlEsteban KüberView Answer on Stackoverflow
Solution 2 - HtmlRounin - Standing with UkraineView Answer on Stackoverflow
Solution 3 - HtmlaguzView Answer on Stackoverflow
Solution 4 - HtmlejectamentaView Answer on Stackoverflow
Solution 5 - HtmlejectamentaView Answer on Stackoverflow
Solution 6 - HtmlaehlkeView Answer on Stackoverflow