✨Building a PDF Viewer for Vue.js with PDF.js, vue3-pdf-app and more 🚀
Tags: vue, frontend, javascript, webdev
Published: 2024-03-19 12:30:00

As someone who has worked on several Vue.js projects in the past, I know firsthand the importance of having a great PDF Viewer to enhance the user experience. Whether it's displaying important documents, presenting reports, or sharing content, PDF viewers provide a seamless way to access and interact with PDF files. 

In this article, I'll be sharing with you four different methods that I've personally used for building a PDF Viewer in Vue.js: iFrame, PDF with New Tab, PDF.js, and vue3-pdf-app. Each method offers unique features and benefits, catering to various project requirements.

<img src="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExNWRvbjBibHlpNHVsMHU3cG9kczh1d3pua3E1YzJ1ejE5OWtudDFlayZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/3o7TKUM3IgJBX2as9O/giphy.gif" />

---

## What is so important about PDF?

Rendering PDFs on websites has a wide range of practical use cases that some of you may have come across. Here are a few examples of these use cases:

1. Product Manuals: As a senior developer, you know that product manuals are an essential resource for customers. By rendering product manuals as PDFs, users can access them on any device, view them in high resolution, and even print them out if needed. PDFs allow you to preserve the original layout and formatting while adding interactive elements like hyperlinks and bookmarks to make navigation easy.

2. Legal Contracts: Contracts are an important legal document that requires accuracy and consistency. By rendering legal contracts as PDFs, you can ensure that they cannot be edited or tampered with, and that they can be quickly and easily signed online with digital signatures.

3. E-books and Online Publications: Many websites offer e-books, case studies, and other online publications. By rendering this content as PDFs, readers can enjoy a familiar reading experience, similar to reading a physical book or magazine, with the ability to zoom in, annotate, and bookmark pages.

4. Educational Materials: As a developer, you may find yourself working on e-learning websites or educational platforms. By rendering course materials and study guides as PDFs, you can present the content in an easy-to-read, visually appealing way.

---

## Vue-PDF-Viewer: Flexible and Powerful Vue.js PDF Component
Just a quick background about what I’m working on. [Vue-PDF-Viewer](https://www.vue-pdf-viewer.dev/?utm_source=dev.to&utm_content=building-a-pdf-viewer-for-vuejs) renders the PDF viewer on your Vue or Nuxt websites so that your users can interact with your PDF document without leaving your sites. The component has over 20 features including theme customization, built-in localization, web responsive and more.

![Vue PDF Viewer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/caxyyicw35mz9exhdc1c.png)

I would be grateful if you could check [Vue-PDF-Viewer](https://www.vue-pdf-viewer.dev/?utm_source=dev.to&utm_content=building-a-pdf-viewer-for-vuejs) out. It will encourage me to make even more contents ❤️‍🔥

---


## Method 1: iFrame
iFrame provides a straightforward approach to embed PDF files and leverage the browser's built-in PDF viewing capabilities. 
Here's how you can implement it in Vue.js:

1. Install Vue.js and create a new Vue project using the Vite.
2. Add an iFrame element to your Vue component template.
3. Set the src attribute of the iFrame to the URL or path of the PDF file.

Here is the [link](https://codesandbox.io/p/devbox/pdf-vg3g97?file=%2Fsrc%2Fcomponents%2FIframe.vue) to the code sample.


![iFrame demo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fhbc6rlye414j1boy6od.png)

Using iFrame has its pros and cons:

**Pros**
- Simple implementation
- Relies on the browser's native PDF viewer
- Display PDF file as part of a website

**Cons**
- Cannot customize because it's using the browser's native pdf function
- Potential compatibility issues with different browsers and versions

**When to use**
Looking for a quick set up without a need for PDF viewer to have UI consistency.

---

## Method 2: PDF with New Tab
The second method involves opening the PDF in a new browser tab. Here's an overview of implementing this method in Vue.js:

1. Create a link or button element in your Vue component template.
2. Add an onclick event handler to open a new browser tab with the PDF URL.

Using a new tab approach offers the following pros and cons:

**Pros**
- Simple implementation
- Direct interaction with the PDF content

**Cons**
- User experience cannot be controlled
- Potential compatibility issues with different browsers and versions

**When to use**
Similar to method 1 and when you want the viewing experience of PDF file to be separated from the main website. For example, you do not want to add a PDF iFrame.

---

## Method 3: PDF.js
PDF.js is a powerful JavaScript library maintained by Mozilla that allows rendering and manipulation of PDF files directly in web browsers. It provides extensive features and capabilities for displaying PDFs in Vue.js applications. To utilize PDF.js in Vue.js, follow these steps:

1. Install the PDF.js library using a script tag.
2. Create a Vue component to display the PDF viewer
3. incorporate the PDF.js into the component to render PDF files.

I have added a simple PDF Viewer using PDF.js in a Vue website. You may find the codes [here](https://codesandbox.io/p/devbox/pdf-vg3g97?file=%2Fsrc%2Fcomponents%2FPDFjs.vue).


![PDF.js demo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5o25yttxnqftdhxymcny.png)

Let's discuss the advantages and disadvantages of using PDF.js:

**Pros**
- Advanced PDF rendering functionality
- Cross-browser compatibility
- Extensive customization options
- Well maintained
- Big community

**Cons**
- May require time and effort to learn the syntax and best practices of the library 
- Not user-friendly technical document
- Not easy to customize with JavaScript such as Angular, React and Vue
- Increased complexity when handling more complex PDF interactions

**When to use**
If you want to customize the PDF viewer UI and integrate it as part of a Vue website.


---

## Method 4: vue3-pdf-app
vue-pdf is a convenient package specifically designed for Vue.js developers to build PDF viewers. It acts as a wrapper around PDF.js, simplifying the integration process. Here's how you can get started with vue3-pdf-app in Vue.js:

1. Install the vue3-pdf-app package using npm or yarn
2. Import the vue3-pdf-app component to display PDF

Please find the demo [here](https://codesandbox.io/p/sandbox/vue3-pdf-app-forked-p2zygn?file=%2Fsrc%2FApp.vue%3A5%2C8).


![vue3-pdf-app demo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ojacamdthb869n1ia59i.png)

Let's explore the advantages and disadvantages of using vue-pdf:

**Pros**
- Easy integration with Vue.js projects
- Simplified API for PDF rendering
- Customizable viewer options

**Cons**
- Support only Vue 3.2 with vue-cli which is not no longer recommended by Vue
- Cannot run on Vite
- Not maintained
- Small community

**When to use**
Actually, I don't recommend using this library because of it only support Vue 3.2.

---

## Comparison Summary
There are multiple methods available to build a PDF viewer for Vue.js. The iFrame approach is simple and relies on the browser's built-in capabilities. Displaying PDFs on the browser provides more control but requires user installation of a PDF viewer plugin. PDF.js offers advanced rendering features and cross-browser compatibility. For Vue.js developers, the vue-pdf package provides a convenient wrapper for PDF.js, simplifying the implementation process. Choose the method that best suits your project requirements and delivers the desired user experience

<img src="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExMGo1N29zbjd2aG5paHM1ZWE5dnF3M2pkeTE1YW0yNHB1N285aDY1cCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/eIqj0vevEQ6lLFUeTZ/giphy.gif" />

---

## Is there a Better Solution?

For customization purposes, using iFrame or Open with New Tab may not be suitable. Although PDF.js is a possible option, using it effectively can take time and effort that Vue.js developers may not want to commit. It's because of all these reasons that the company I'm working for is building a new library, [Vue-PDF-Viewer](https://www.vue-pdf-viewer.dev/?utm_source=dev.to&utm_content=building-a-pdf-viewer-for-vuejs).

![Video demo of Vue PDF Viewer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/18z1p9r0kbo3ekw4s4z5.gif)

1. **Made for Vue.js:** [Vue-PDF-Viewer](https://www.vue-pdf-viewer.dev/?utm_source=dev.to&utm_content=building-a-pdf-viewer-for-vuejs) library is crafted to seamlessly integrate with Vue.js. With just a few lines of code. What's more, the library offers extensive customization options, enabling developers to easily match the PDF viewer's theme to their website's design.
2. **Compatible with Vite:** For Vue.js developers who leverage Vite as their development tool, the library brings additional advantages. Developers can take advantage of unmatched performance and build speed.
3. **Extensive Feature Set:** Come equipped with an extensive set of over 20 features, catering to a wide range of user requirements. From essential functionalities like text highlighting and bookmarking, to advanced options such as search functionality, web responsive and  localization.
4. **User-Friendly Technical Doc:** Get started quickly with our starter toolkit. The documentation includes clear instructions, code examples, and extensive API references.
5. **Reliable Support:** We understand the importance of reliable support during the integration process. From a responsive customer support team to regular updates addressing bug fixes and feature enhancements.

If you find this library helpful, please check out please check out [Vue-PDF-Viewer](https://www.vue-pdf-viewer.dev/?utm_source=dev.to&utm_content=building-a-pdf-viewer-for-vuejs). It would encourage me to continue creating even more contents. Thank you in advance! 🙏
 
<img src="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExdzUxaWZzM2IwcDRtcm9mYmRzeWZ1c2hpc29tN2RucmQ1OXo3cmoyMSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/3oz8xIsloV7zOmt81G/giphy.gif" />