[an error occurred while processing this directive]

Previous | Home | Next

Aug 28, 2007

Using the HTML embed Tag to Display a PDF on Your Web Page

Most web developers create links to PDF files so that Adobe Acrobat or the Adobe Reader takes over the entire browser window. Few people leverage the fact that you can display a PDF file in a web page just like any other graphic by using the embed tag in your HTML file. You can also use PDF Open Parameters in the src property to make the document look better on that page but those don’t work the same way in all browsers.

Careful attention to crafting the HTML page, setting the embed tag parameters properly and setting properties in your PDF correctly can make a PDF embedded in an HTML page work just as seamlessly as a SWF file.

Below is an example of simply adding the PDF file name to the src property of the embed tag; it doesn’t look that great.

<embed src="FullScreenEmbed.pdf" width="500" height="375">


In the example below, I’ve added parameters to the URL that suppress the toolbar, navigation panes, and scrollbars of the Acrobat or Reader user interface. This looks much better. You can navigate the PDF by clicking on it (to activate the embedded PDF) and then use the arrow keys on your keyboard to scroll through the document. The other advantage of this method is that you don’t need to change the PDF to provide a better experience; you simply adjust the way it looks by adding parameters to the URL.

<embed src="FullScreenEmbed.pdf#toolbar=0&navpanes=0&scrollbar=0" width="500" height="375">


The example below demonstrates the solution that, in my opinion works the best. Here, I used Acrobat to set the PDF to “Open in Full Screen mode” in the “Document Properties” dialog box.

<embed src="FullScreenEmbed.pdf" width="500" height="375"/>


Setting the document to open in full screen mode will cause the PDF to fill the entire area specified in the width and height properties of the embed tag. The PDF page size is 10 inches by 7.5 inches so I use a width of 500 pixels and a height of 375 pixels so that the aspect ratio of the embed object window and the PDF match. The PDF will not stretch to fill the area in the same way that an image file would. Instead, the PDF will display in the center of the area and fill the rest with the “Full Screen Appearance Background Color” which is an Acrobat user preference but their color choice may not fit with the style of your web page so it’s best to keep the aspect ration the same. Of course, your document can change the background color setting through JavaScript by adding, for example, “app.fs.backgroundColor = color.white;” as a document level JavaScript. You can read about how to use the built-in color arrays and create your own on page 193 of the JavaScript™ for Acrobat® API Reference.

Setting the document to open in full screen mode will also cause the document to “play” in the browser same way that it would if it were in full screen mode in Acrobat or Reader. All of your animations and page transitions will display properly.


permalink.gif Comments (7) Bookmark and Share


Comments

Nice Work

What if I want to embed the pdf so that "pdf is in find mode".

[Joel's response]
You can add #search="foo" to the URL and Acrobat/Reader will open in find mode and highlight the word(s) in your search string. Here's an example...

http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters_v9.pdf#search="parameters"

I have embedded the pdf and it works perfectly in safari but in firefox it doesn't work, why is that?

Joel's reply...

I'm guessing that you're on OSX. Acrobat/Reader actually doesn't support viewing PDF in Firefox on OSX, just Safari. You can see the ful system requirements for Reader at the URL below.

http://www.adobe.com/products/reader/systemreqs/

Umm... so how DO I embed a PDF into a page so that it does render in Firefox on a Mac OS X? I put together a site, and that's the one bug I've got. Half the people that looked at it complained that they couldn't view the pages in question. All it does is show the little Lego icon. Click on the icon to find the appropriate plug-in and it says "Can't find a suitable plug-in."

So what should I do?

[Marv: I'm running Firefox 3.5.3 and the OSX Snow Leopard Preview.app is displaying the PDF just fine when either the tag or the tag is being used to. I'm not sure what the problem is on your side.]

Exactly what I was looking for. Thanks!

My doc looks the same, good, in "Firefox ver 3.5.3", "IE ver 8", "Safari (windows)ver 4.03"

I know that when HTML tags are enabled that it's very hard to quote HTML properly - it gets unquoted every time I preview the comment. It's even harder to quote quoted HTML properly, but just to clarify, all those lone ampersands in the example HTML source snippets should of course be the full HTML entity i.e.
<embed src="FullScreenEmbed.pdf#toolbar=0&amp;navpanes=0&amp;scrollbar=0" width="500" height="375">

[Neil - Good point. Thanks for the comment. That's what I get for just copying and pasting from the IE location bar.]

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)