Blog Post

5 easy ways to embed a PDF viewer in HTML

Illustration: 5 easy ways to embed a PDF viewer in HTML
Information

This article was first published in October 2021 and was updated in August 2024.

The PDF file format, created by Adobe, is widely used in various industries such as healthcare, education, and government. It’s common to find PDF files embedded in or attached to websites. However, embedding a PDF viewer in a website can be challenging, as HTML and PDF formats are very different.

If you’re looking for a fast and simple way to embed PDF documents in your website, this post will provide you with five methods to do so. These methods utilize HTML tags and can be implemented on any website. The default PDF viewer includes features like pagination, zoom, scroll, and download functionality.

You can watch our step-by-step video tutorial guide.

1 — Embedding using the embed tag

The <embed> element is used to load external content to a website. The content is provided by an external application, multimedia, or interactive content such as a browser plugin.

To use this element, you need to add the following attributes to the <embed> tag. Notice the URL to the PDF file in the src attribute:

<embed
	src="document.pdf"
	type="application/pdf"
	width="100%"
	height="100%"
	title="Embedded PDF Viewer"
/>
  • The src attribute specifies the location of the PDF file. In the case above, the document is located in the same directory as the HTML file.

  • The type attribute specifies the type of the embedded file. For PDF documents, the value of this attribute should be set to application/pdf.

  • The width and height attributes specify the width and height of the PDF file viewer (the embed element). Here, they’re set to take the whole width and height of the parent container, so you have to make sure the parent container has an actual width and height, otherwise the content won’t be visible.

Support for the <embed> element is currently available in all major browsers.

2 — Embedding using the object tag

The <object> tag is one of the primary ways to embed PDF files into HTML. This method allows you to embed various types of content, including PDFs, multimedia elements, and interactive documents.

The HTML <object> element can be used in a native browser for PDF viewing, and it even allows you to provide a fallback if the PDF format isn’t supported:

<object
	data="https://web-preview.pspdfkit.com/showcases/8.pdf"
	type="application/pdf"
	width="100%"
	height="100%"
	aria-labelledby="PDF document"
	title="Embedded PDF Viewer"
>
	<p>
		Your browser does not support PDFs.
		<a href="https://web-preview.pspdfkit.com/showcases/8.pdf">
			Download the PDF
		</a>
	</p>
</object>
  • The data attribute specifies the URL location of the PDF file.

Similar to the <embed> element, you can add the type, width, height, and title attributes to the <object> tag.

In the example above, if the browser doesn’t support displaying PDFs, the <object> tag will display a message with a link to download the PDF.

Embedding Using the Object Tag

3 — Embedding using the inline frame tag

An <iframe>, short for “inline frame,” is a container for embedding information from other web pages, either as a full page or as a widget. It has some attributes that allow you to control the appearance of the content:

  • src specifies the URL location of the data source.

  • width and height control the frame’s width and height.

  • title provides an accessible title for the frame.

  • Under style, border:none removes the border around the frame.

This is the code for the <iframe> element:

<iframe
	src="document.pdf"
	width="100%"
	height="100%"
	style="border:none"
	title="Embedded PDF Viewer"
></iframe>

Here, the document.pdf file is in the same directory as the HTML file.

It’s also possible to prevent the PDF document from being downloaded by the user by adding #toolbar=0 after the URL of your PDF document:

<iframe
	src="document.pdf#toolbar=0"
	width="100%"
	height="100%"
	style="border:none"
	title="Embedded PDF Viewer, non-downloadable PDF"
></iframe>

The <iframe> method is highly recommended for embedding PDFs due to its flexibility and ease of customization. You can adjust attributes such as width, height, and style, and use URL parameters like #toolbar=0 to control viewer options.

4 — Combining object and inline frame tags

This method combines <object> with the <iframe> HTML element. To do this, the fallback area of the <object> is used to host an <iframe>. Similar to the <object> element, the content of an <iframe> — the <p> tag with its content in this case — is only shown when the browser doesn’t support PDFs via the <iframe> element:

<object
	data="document.pdf"
	type="application/pdf"
	width="100%"
	height="100%"
	title="Embedded PDF Viewer"
>
	<iframe
		src="document.pdf"
		width="100%"
		height="100%"
		style="border: none"
		title="Embedded PDF Viewer, iframe fallback"
	>
		<p>
			Your browser does not support PDFs.
			<a href="document.pdf">Download the PDF</a>
		</p>
	</iframe>
</object>

Combining Object and Inline Frame Tags

5 — Embedding using the anchor tag

A hyperlink or anchor element is a piece of text that can be clicked, and when the user clicks it, they’ll be taken to another location on the same page or to a completely different website. In this section, you’ll use the <a> tag to link to the PDF file.

It has a simple format; just add the href attribute to the <a> tag:

<p>
	Open a PDF file
	<a href="document.pdf">example</a>
</p>

The href stands for hypertext reference and is used to designate the website or file that the link points to. Substitute document.pdf with the name of your PDF file. If you click the example link, the browser will open the PDF file.

Try the example below:

Open a PDF file example.

Comparison of PDF embedding methods

This section outlines the pros and cons of different PDF embedding methods. Understanding these can help you decide which method aligns best with your requirements.

Method Pros Cons
<embed> - Simple to implement
- Supported across most major browsers
- Limited control over viewer features
- Some browsers may not display PDFs if plugins are disabled
<object> - Provides fallback content
- Better compatibility with older browsers
- Fallback content can affect user experience
- Limited control over viewer features
<iframe> - Highly customizable
- Control over appearance with URL parameters
- Flexibility with width and height
- #toolbar=0 may not be supported by all viewers
- Users might still find ways to download PDFs
<a> - Extremely simple
- Ensures users can access the PDF
- Does not embed the PDF
- Users need to download or open PDF in a new window
<object> + <iframe> - Provides a fallback for unsupported browsers
- Combines benefits of both <object> and <iframe>
- More complex to implement
- May result in duplicate content if both methods are supported

Detailed comparison of embedding methods

For a more in-depth analysis, this section compares each method based on various criteria such as browser support, customization options, advanced features, and user experience.

Method Browser support Customization Advanced Features User experience
<embed> Widely supported Basic Limited Good for straightforward embedding
<object> Good compatibility Basic Limited Better fallback but less control
<iframe> Highly supported Extensive High with URL parameters Flexible and customizable, but some limitations on controls
<a> Excellent None None Provides direct access, not embedding
<object> + <iframe> Comprehensive High High with fallback Ensures broad compatibility but can be complex

Problems with these PDF embed methods

All that said, there are some shortcomings of the methods outlined above, such as:

  • There’s only a very limited set of API methods, and some of the attributes are deprecated.

  • A browser could use whichever PDF reader is installed on a system, and there’s no API that would allow you to customize the reader’s look and feel. In other words, the user interface (UI) used when loading a PDF via the aforementioned tags is beyond your control.

  • Keep in mind that most browsers no longer support browser plugins such as Adobe Flash. So, relying on <embed> or <object> tags may no longer be a good idea, as browser vendors may deprecate or limit support for them in the future.

  • There are some security issues with iframes. For example, clickjacking is a common iframe attack that fools users into thinking they’re clicking a visible UI element when they’re actually clicking a hidden iframe. You can learn about the mitigation of this attack by reading this article.

FAQ

Here are a few frequently asked questions about about how to embed PDF in HTML.

How to embed a PDF viewer in HTML?

To incorporate a PDF viewer in HTML, use the <embed>, <object>, <iframe>, and <a> HTML5 elements. Each method offers different functionalities and is supported in most browsers. For more advanced features, consider a commercial PDF viewer library like Nutrient.

How to embed a PDF viewer into your website without the ability to download?

To prevent users from downloading a PDF, use the <iframe> element and append #toolbar=0 to the URL of your PDF document. This hides toolbar options and restricts downloading.

What is the best method to embed a PDF using an iframe?

The <iframe> method is recommended for its flexibility. You can customize it with attributes like width, height, and style, and control functionality using parameters such as #toolbar=0.

How can I embed a PDF in HTML with advanced features?

For advanced features such as annotations and interactive forms, consider using a commercial PDF viewer library like Nutrient Web SDK. It offers extensive functionality and customization beyond basic HTML embedding.

Conclusion

This post taught you how to embed a PDF viewer into your website. With <embed>, <object>, <iframe>, and <a> HTML5 elements, it’s possible to show a PDF in your web app with ease. This is supported in most browsers and requires no JavaScript at all, making it a perfect approach for adding PDF support if no advanced features are necessary.

However, for use cases that require support for every browser, customization, or more advanced PDF features — such as PDF annotations, interactive PDF forms, and digital signatures — we recommend you look into commercial alternatives.

At Nutrient, we offer a commercial, feature-rich, and completely customizable HTML5 PDF viewer library that’s easy to integrate and comes with well-documented APIs to handle complex use cases. Check out our PDF library using our free trial, and play around with our demo to see what’s possible.

Author
Hulya Masharipov Technical Writer

Hulya is a frontend web developer and technical writer at Nutrient who enjoys creating responsive, scalable, and maintainable web experiences. She’s passionate about open source, web accessibility, cybersecurity privacy, and blockchain.

Related products
Share post
Free trial Ready to get started?
Free trial