How to Load & View PDF Files in a Vue Application
Quick Start Guide | |
---|---|
Tutorial Concept |
Vue PDF Viewer - Add the ability to view PDF files in Vue applications. The Documents Solutions PDF Viewer makes it possible to add this functionality and other document management options to web applications. |
What You Will Need |
NPM @mescius/dspdfviewer |
Controls Referenced |
Document Solutions PDF Viewer - Vue PDF Viewer Control |
The Document Solutions PDF Viewer (DsPdfViewer) is a Vue PDF Viewer that enables users to view and edit PDF files directly in the browser— including interactive PDF forms. Compatible with all modern browsers, it also allows users to save their modified PDFs on the client side. In this article, we’ll show you how to integrate the Document Solutions PDF Viewer into a Vue application, giving users a seamless way to interact with PDF content in-browser.
Download a finished Vue3 sample app to follow along with the blog!
Steps for Loading & Viewing PDFs in Vue3 Apps: Viewer Integration and Loading Methods
- Integrate the Vue PDF Viewer Control into the Vue Project
- Load or Open a Default PDF Template
- Load or Open a PDF from a URL
- Load or Open a PDF from Another Domain URL using a CORS Proxy
Work on PDFs in Vue on your own by downloading a FREE trial of Document Solutions for PDF!
Integrate the Vue PDF Viewer Control into the Vue Project
Using the following command create a simple Vue project. This command will run create-vue, the Vue scaffolding tool, with optional prompts for features like TypeScript and testing support.
Note, you will be given several optional features such as TypeScript and Testing Support:
After creating the project, use the following commands to navigate to the project directory and install the Document Solutions PDF Viewer NPM package. Optionally, you can additionally install bootstrap for styling.
After the installation is complete, all of the required files are installed in the node_modules → @mescius → dspdfviewer folder, as depicted below:
Update the App.vue file by replacing the template with the following code to create a host element for the Vue PDF Viewer.
Next, initialize the PDF Viewer inside the onMounted lifecycle hook by creating a new DsPdfViewer instance and passing in the ID of the host element. Then, call the addDefaultPanels method to display the viewer’s built-in toolbar and navigation panels.
Lastly, set the CSS styling for the PDF Viewer’s host element in the main.css class.
Run the Vue3 project and notice the PDF viewer control shows in the browser with the default side panels:
Check out the online Vue PDF Viewer demo and documentation to learn more about this developer friendly PDF viewer control.
Load or Open a Default PDF Template
With the application configured and the Document Solutions PDF Viewer successfully rendered in the browser, we can now load and display a PDF file within the viewer. In this example, we'll load a local PDF file stored in the Vue project directory at public/pdfs/social_media_schedule.pdf.
To render the PDF, use the open method provided by DsPdfViewer, passing in the relative file path as an argument.
Notice, the specified PDF will now appear on load in the Vue PDF Viewer.
The PDF Viewer API also provides an openLocalFile method, which allows the user to choose the PDF that they want to load into the DsPdfViewer using the file open dialog.
Watch it in action: Here's a short video showing how to load a default PDF template into the JavaScript PDF Viewer control in a Vue application.
Load or Open a PDF from a URL
In addition to loading local files, you can also open PDFs hosted on the same domain as your Vue application. To do this, use the open method and pass the URL of the PDF as an argument:
This method works for any PDF file that is accessible within your app’s domain. Once called, the viewer will fetch and render the specified PDF directly in the browser.
Test the above code here in the online PDF Viewer demo sample.
Load or Open a PDF from Another Domain URL using a CORS Proxy
To load a PDF from another domain, use the SupportApi configuration to avoid CORS issues. SupportApi is a lightweight ASP.NET Core service included with Document Solutions for PDF that acts as a proxy for loading and processing remote PDFs.
The supportApi object in the viewer config enables this, and you’ll also need to create a CorsProxyController on the server to download and stream remote files.
Start by adding the following controller to your SupportApi ASP.NET Core project:
Make sure to register the HTTP client in Program.cs:
In your Vue component, set up the viewer with the supportApi config pointing to your backend, and call the open method provided by DsPdfViewer, passing in the proxy endpoint:
Once configured, the Vue app will be able to open PDFs from external domains without hitting CORS issues, using an ASP.NET Core app as a secure proxy.
Conclusion
This completes the basics of configuring the Document Solutions PDF Viewer in a Vue application. Download the Vue3 sample that we built throughout the article to check it out. Make sure to install the DsPdfViewer NPM package to get the sample working.
You can also refer to this online demo to observe the sample in action and go through the implementation details.
Work on PDFs in Vue on your own by downloading a FREE trial of Document Solutions for PDF!
Vue PDF Viewer Control
This article just scratches the surface of the full capabilities of the Vue PDF Viewer, Document Solutions PDF Viewer. Check out the online demo explorer and documentation to learn more about this powerful control.