The Wayback Machine - https://web.archive.org/web/20240626032329/https://www.geeksforgeeks.org/how-to-embed-pdf-file-using-html/
Open In App

How to Embed PDF file using HTML ?

Last Updated : 22 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

We will learn how to embed PDF files in HTML documents, along with knowing their implementation through 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, it is not easy to accomplish the task. 

These are the following methods for doing this:

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.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>PDF in HTML</title>
</head>
<style>
    .pdf {
        width: 100%;
        aspect-ratio: 4 / 3;
    }

    .pdf,
    html,
    body {
        height: 100%;
        margin: 0;
        padding: 0;
    }

    h1,
    h3 {
        text-align: center;
    }

    h1 {
        color: green;
    }
</style>

<body>

    <h1>GeeksforGeeks</h1>
    <h3>Embedding the PDF file Using Object Tag</h3>
    <object class="pdf" 
            data=
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/20210101201653/PDF.pdf"
            width="800"
            height="500">
    </object>
</body>

</html>

Output:

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.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>PDF in HTML</title>
</head>
<style>
    .pdf {
        width: 100%;
        aspect-ratio: 4 / 3;
    }
    .pdf,
    html,
    body {
        height: 100%;
        margin: 0;
        padding: 0;
    }
   h1,
    h3 {
        text-align: center;
    }

    h1 {
        color: green;
    }
</style>

<body>
        <h1>GeeksforGeeks</h1>
        <h3>Embedding the PDF file Using Iframe Tag</h3>
        <iframe class="pdf" 
                src=
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/20210101201653/PDF.pdf"
            width="800" height="500">
        </iframe>
</body>

</html>

Output:

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: This example describes the embedding of a PDF file in HTML using the embed tag.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>PDF in HTML</title>
</head>
<style>
    .pdf {
        width: 100%;
        aspect-ratio: 4 / 3;
    }

    .pdf,
    html,
    body {
        height: 100%;
        margin: 0;
        padding: 0;
    }
   h1,
    h3 {
        text-align: center;
    }

    h1 {
        color: green;
    }
</style>

<body>
        <h1>GeeksforGeeks</h1>
        <h3>Embedding the PDF file Using embed Tag</h3>
        <embed class="pdf" 
               src=
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/20210101201653/PDF.pdf"
            width="800" height="500">
</body>

</html>

Output:



Similar Reads

YouTube Video Embed Code Generator Tool using HTML CSS and JavaScript
In this article, we will see How to create your own YouTube Video embed code generator tool using the basics of HTML, CSS, and JavaScript. With this tool, we can create responsive embed code for responsive video output on all devices. We can make use of this tool and add the generated code in Blogger, WordPress, Wix, Google Sites, custom websites,
4 min read
HTML | DOM Embed Object
The Embed Object in HTML DOM is used to represent the &lt;embed&gt; element. The &lt;embed&gt; element can be accessed by using getElementById() method. Note: This object is new in HTML 5. Property Values: height: It sets or returns the value of the height attribute. src: It sets or returns the value of the src attribute of an Embed Object, type: I
2 min read
HTML | &lt;embed&gt; type Attribute
The HTML embed type Attribute contains the media_type content. It is used to specify the media type of the embedded content. Syntax: &lt;embed type="media_type"&gt; Attribute Values: media_type: It contains the media_type content. It is used to specify the media type of the embedded content. Example: C/C++ Code &amp;lt;!DOCTYPE html&amp;gt; &amp;lt
1 min read
HTML | &lt;embed&gt; src Attribute
The HTML &lt;embed&gt; src Attribute is used to specify the web address of the embedded content. Syntax: &lt;embed src="URL"&gt; Attribute Values: URL: It is used to specify the web address of the embedded content.An absolute URL: It points to another webpage.A relative URL: it points to a file within a website. Example: C/C++ Code &amp;lt;!DOCTYPE
1 min read
HTML | &lt;embed&gt; height Attribute
The HTML &lt;embed&gt; height attribute is used to specify the height of the embedded content. Syntax: &lt;embed height = "pixels"&gt; Attribute Values: pixels: The width value are set in terms of pixels. It is used to specify the height of embedded content. Example: C/C++ Code &amp;lt;!DOCTYPE html&amp;gt; &amp;lt;html&amp;gt; &amp;lt;head&amp;gt;
1 min read
HTML | &lt;embed&gt; width Attribute
The HTML embed width attribute is used to specify the width of the embedded content. Syntax: &lt;embed width="pixels"&gt; Attribute Values: pixels: The width of embed value are set in terms of pixels. It is used to specify the width of embedded content. Example: C/C++ Code &amp;lt;!DOCTYPE html&amp;gt; &amp;lt;html&amp;gt; &amp;lt;head&amp;gt;
1 min read
HTML | DOM Embed src Property
The HTML DOM Embed src Property is used to set or return the value of the src attribute in an embed element. This attribute specifies the address of the external file to embed. Syntax: Return the src property: embedObject.src Set the src property: embedObject.src = URL Property Values: URL: Specify the URL of the external file to embed. Possible Va
1 min read
HTML | DOM embed height property
The embed height property in HTML DOM is used to set or return the value of the height attribute. The height attribute defines the height of the embedded content in terms of pixels. Syntax: Return the height property:embedObject.heightSet the height property:embedObject.height = pixels Property Values: px: Specify the height in pixels of the embedd
2 min read
HTML DOM embed type property
The DOM embed height property in HTML is used to set or return the value of the type attribute in an embed element. The type attribute specifies the MIME type of the embedded content. Syntax: Return the type property:embedObject.typeSet the type property:embedObject.type = MIME_type Property Values: MIME_type: It contains a single value MIME-type w
1 min read
HTML | DOM embed width property
The DOM embed width property in HTML is used to set or return the value of the width attribute. The width attribute defines the width of the embedded content, in pixels. Syntax: Return the width property:embedObject.widthSet the width property:embedObject.width = pixels Property Values: px: Specify the width in pixels of the embedded content. Retur
2 min read
three90RightbarBannerImg