191

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?

2
  • 23
    And now with <embed> in HTML5? What is the best way?
    – VinnyG
    Apr 21, 2011 at 2:20
  • @VinnyG <embed> is now officially a standard tag with HTML5, but you should anticipate at least some compatibility issues with older browser versions.
    – arkon
    Apr 7, 2013 at 6:25

6 Answers 6

199

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.

11
  • 51
    <embed> is actually part of HTML5 dev.w3.org/html5/spec/Overview.html#the-embed-element Jun 1, 2010 at 8:22
  • 8
    Even though <embed> is part of HTML5 standard, it seems to me that <object> is the better choice because of compatibility with older browsers and the ability to display alternate content. Thoughts?
    – Raphael
    Sep 11, 2012 at 16:02
  • 5
    @raphaelcm Allow me to play devil's advocate. If maintaining compatibility with outdated browsers was that important, HTML would never evolve. What's important here is market share, specifically regarding browser versions.
    – arkon
    Apr 7, 2013 at 6:32
  • 7
    Why is this accepted? I thought <embed> was the HTML5 standard tag.
    – fabspro
    Feb 27, 2014 at 6:57
  • 2
    I would think <embed> would be the better choice going forward. It's in the HTML5 standard while Object had many of it's features(attributes) deprecated to separate it's functionality from the embed tag. w3schools.com/tags/tag_object.asp It appears to me that the object tag is almost a 'Swiss army knife' tag while embed is purpose built for embedding content into a page.
    – cmaynard
    Apr 21, 2015 at 12:40
13

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

1
  • 2
    There are other uses for both than plugins. SVG (which can also use <svg> or <img> with subtle differences...) and PDF are two common ones. Jan 20, 2022 at 17:29
6

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>
4

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>
4

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

2

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.

2
  • 13
    Embed seems to be quite standard to me - at least in HTML5.
    – kapa
    Jul 7, 2012 at 2:08
  • 7
    @bažmegakapa It is certainly standard now with HTML5, but that article he is referring to was written back in 2008 and his answer is from 2009, which predates HTML5.
    – arkon
    Apr 7, 2013 at 6:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.