Embed PDF in HTML

We have explored 4 methods to embed PDF in HTML:

  • Method 1: Using Object Tag
  • Method 2: Using iframe
  • Method 3: Using embed tag
  • Method 4: Using PDF.js

When to embed PDF in HTML

For those who want a professional and beautifully designed way to display documents onto their webpages, being able to embed pdf html code onto their website would give them the ability to display interactive documents for users to use. Many companies and individiuals can benefit from being able to embed a pdf inside the html code for their websites, instead of traditional document display. Embedding pdf in the html code allows a more convenient way to display pdf documents which may not have been compatible with browsers before.

It is surely a put-off if you redirect your viewers to a third party application in order to display your pdf documents (such as drive links) and the user is now away from your webpage. The proability of his return is surely reduced if this method is used.

So let's learn how to efficiently embed pdf documents into your webpage and display them and there.

For the tutorial, I'll use an online document, which is my resume.
In case you wish to use a local file, replace the link with the name of the file, with the appropriate path, of course.

Okay, let's go:

Method 1: Using Object Tag

The first method to embed pdf in html is by making use of the object tag in html. The object defines the object that is to be embedded in the page, which in this case is the pdf file to be displayed on the web page, of course.The object tag isn’t just used to embed a pdf html code into a webpage; it can also be used to embed ActiveX, Flash, video, audio, and Java applets. The object embed a pdf html tag can be used to attach interactive documents.

Lets see how the code looks for the same:

<object data="https://iq.opengenus.org/resume/0924.pdf"/>
  • As visible above, the data attribute of the object tag is used to specify the name/address of the file that is to be displayed.
  • Lets create an HTML file named index.html to test this out:
<!DOCTYPE html>
<html>
<head>
	<title>PDF in HTML</title>
</head>
<body>
	<object data="https://iq.opengenus.org/resume/0924.pdf"/>
</body>
</html>
  • Now open the index.html file in your browser and the pdf file should be visible in it.
    Screenshot--14-
  • We observe that the size of the pdf file which we embedded is not appropriate, thefore we have style it accordingly.
  • In this example, I will demonstrate how to display the pdf file in all the space available, by using -webkit-fill-available as the height and width of the object.
  • The code for the object will be as follows for that:
<object data="https://iq.opengenus.org/resume/0924.pdf" style="height: -webkit-fill-available;width: -webkit-fill-available"/>
  • Also, it is always better to specify the file format of the data while using the object tag to eliminate possibilty of inaccurate browser compatibility.
<object data="https://iq.opengenus.org/resume/0924.pdf" type="application/pdf" style="height: -webkit-fill-available;width: -webkit-fill-available"/>

Screenshot--15-

Method 2: Using iframe

The second method to embed a pdf in an HTML web page is the iframe tag.
The iframe tag is widely used by web developers to embed files of various formats and even other webpages inside the concerned web page. Most browsers are compatible with this embed a pdf html code tag which is why this tag is widely used. This tag used to embed a pdf html code can also be used if a browser does not support PDF documents or the object tag.

  • The method to use the iframe tag to display pdf in web pages should be clear from this code snippet:
<iframe src="https://iq.opengenus.org/resume/0924.pdf" type="application/pdf" style="height: -webkit-fill-available;width: -webkit-fill-available"/>
  • As visible in the above snippet, the method to use the iframe and object tags is quite similar.
  • The data tag is replaced by src tag, the abbreviation of source which, although self-explanatory, specifies the source of the file to be embedded.
  • The HTML file should have the following contents:
<!DOCTYPE html>
<html>
<head>
	<title>PDF in HTML</title>
</head>
<body>
	<iframe src="https://iq.opengenus.org/resume/0924.pdf" type="application/pdf" style="height: -webkit-fill-available;width: -webkit-fill-available"/>
	
</body>
</html>
  • Open the file in your browser and it should look like this:
    Screenshot--16-
  • If you notice carefully, while using this method, a line with a shadow appears on the border of the embedded file. The file is rendered in a container, more accurately a frame.

Method 3: Using embed tag

The embed tag isn’t used as often as the previous tags to embed a pdf html code into a website because when it is used to embed a pdf html code into a website if the browser does not support PDF files, the person using the website will see a blank. embed is used to embed a pdf html code when there is no need for a mechanism to be supplied for fallback content in the embed a pdf html code.

Similar to the previous methods, the syntax can be understood by this example:

<embed src="https://iq.opengenus.org/resume/0924.pdf" type="application/pdf" style="height: -webkit-fill-available;width: -webkit-fill-available"/>

Should look like this:
Screenshot--16-

Method 4: Using PDF.js

External libraries can also be used to embed pdf files into html web pages. These are generally used for custom features.
Let us try it out with PDF.js.

PDF.js is a JavaScript library written by Mozilla. Since it implements PDF rendering in vanilla JavaScript, it has cross-browser compatibility and does not require additional plugins to be installed.

With PDF.js, PDFs are downloaded via AJAX and rendered onto a canvas element using native drawing commands. To improve performance, a lot of the processing work happens in a web worker.

To get started, all you need to do is to download a recent copy of PDF.js and you’re good to go.

(For this tutorial, go for the stable version)

Once you have downloaded the file, extract it. You will see two folders, namely
web and build.

The HTML file needs to point to the pdf.js source code and to our custom application code (simple.js). We also create a 'canvas' element, which we want the first page of the PDF to be rendered into:

<html>
<head>
  <meta charset="UTF-8">
  <title>PDF.js Example</title>
  <script src="/pdf.js"></script>
  <script src="/simple.js"></script>
</head>
<body>
  <canvas id="pdf"></canvas>
</body>
</html>

Now all that’s missing is our simple.js code that leverages the PDF.js API to render the first page.
Here, I am assuming that you need to embed a pdf file which is named test.pdf

var loadingTask = PDFJS.getDocument("/test.pdf");
loadingTask.promise.then(
  function(pdf) {
    // Load information from the first page.
    pdf.getPage(1).then(function(page) {
      var scale = 1;
      var viewport = page.getViewport(scale);

      // Apply page dimensions to the <canvas> element.
      var canvas = document.getElementById("pdf");
      var context = canvas.getContext("2d");
      canvas.height = viewport.height;
      canvas.width = viewport.width;

      // Render the page into the <canvas> element.
      var renderContext = {
        canvasContext: context,
        viewport: viewport
      };
      page.render(renderContext).then(function() {
        console.log("Page rendered!");
      });
    });
  },
  function(reason) {
    console.error(reason);
  }
);

We can now work on the integration. To do this, we create a simple HTML file that will include the viewer via an iframe. This allows us to embed the viewer into an existing webpage very easily. The viewer is configured via URL parameters, a list of which can be found here. For our example, we will only configure the source PDF file. For more advanced features (like saving the PDF document to your web server again), you can simply start modifying the viewer.html file provided by PDF.js:

 <html>
<head>
  <meta charset="UTF-8">
  <title>PDF.js Example</title>
</head>
<body>
  <iframe
    src="/web/viewer.html?file=/test.pdf"
    width="800px"
    height="600px"
    style="border: none;" />
</body>
</html>




These are the four methods that I use for embedding a pdf into my html webpages.
Hope you learnt something of value from this article!

Cheers! :)