How to Embed PDF file using HTML ?
In this article, we will learn how to embed PDF files in HTML documents, along with knowing their implementation through the examples. Sometimes, you may want to insert a PDF file into an HTML document or code, to make the content more interactive. Because the formats are so different, which is not easy to accomplish the task.
To learn how to add PDF files to HTML code, you need to know how to convert files using PDF software, as a prerequisite knowledge. PDF’s static layout makes it incompatible with HTML’s responsive design. To insert a PDF into HTML, we must convert the file first. An alternative is to embed the PDF HTML code into a webpage to make it interactive. The following embedding methods can be used to add PDF to an HTML file:
- Using Object Tag
- Using an iframe
- Using embed tag
We will explore & understand these methods in detail through their implementations.
Method 1: Using Object Tag
HTML’s object tag is the first way to embed PDF files. In the below example, the pdf file will be displayed on a web page, which is an object.
In addition to embedding a pdf file into a webpage, the object tag can embed ActiveX, Flash, video, audio, and Java applets. Interactive documents can be attached to an object embedded with an HTML tag. It can be displayed according to your need on the screen by using the object’s height and width attributes.
Example 1: This example describes the embedding of a PDF file in HTML using the Object Tag.
Method 2: Using an iframe
Using an iframe tag is the second way to embed a pdf file in an HTML web page. In web development, web developers use the iframe tag to embed files in various formats and even other websites within a web page. Due to its wide compatibility, the iframe tag is widely used for embedding pdf. A browser that does not support PDF documents or the object tag can also use this tag to embed a pdf HTML code.
Example 2: This example describes the embedding of a PDF file in HTML using an iframe.
Method 3: Using embed tag
When embedding a pdf HTML code into a website, the embed tag isn’t used as often as the previous tags because if the user’s browser can’t handle PDF files, the display will be blank. The embed a pdf HTML code method is used when no fallback content needs to be provided.
Example 3: This example describes the embedding of a PDF file in HTML using the embed tag.