How to preview pdf file using pdfjs in vue

How to preview pdf file using pdfjs in vue

Preface

In the process of writing a project, PDF files are occasionally used. When we want to view PDF files, do you download the PDF file first and then view the PDF file through the software installed on the computer specifically for opening PDFs? What if there is a requirement not to let users install software? After all, there are still many users who don’t know how to install software or what is required to open it. Ok, if there is such a demand, then we have to realize it. After all, we can’t refute this reason.

think

Now that it has been brought up, let’s think about how to use it. Since you need to use pdf. Is there any library file that can be operated by js to help me? Just search Baidu and you will find our protagonist today: pdfjs.

Library directory parsing and downloading

Now that we have a plan, let's study it. First, we need to download the file of this library. Download address: click here to download. There will be two versions of this page, one is compatible with the old version, you can download it according to your actual situation.

The downloaded code structure is as follows:

There are two folders in here:

  • build This is the PDF core file
  • Web Example

How to use

There are two ways to use it. One is to implement PDF preview by yourself through the core library file, and the other is to preview PDF files through the given examples. Here we will use the example given to preview the PDF file. If you are interested, you can study how to use the core file to implement it.

File Location

First, we need to put the downloaded files in the static directory of the Vue project, as shown below:

Actual call

Use the following method to call it where needed:

var url = encodeURIComponent(window.location.origin+'/other/202101/dc88623a-74c4-49c4-bc95-7e34d9cf6163.pdf')
window.open(window.location.origin + window.location.pathname+'static/pdfjs/web/viewer.html?file='+url)

The pdf address above is the path you put locally. The path here can be relative or absolute. The above is an absolute path.

At this time, you can see the following effect:

question

If the address of this PDF file and the address of the currently downloaded library file are in the same domain, there should not be any big problems. If they are in different domains, you need to solve the cross-domain problem. There is one thing that needs to be solved here, which is the library file we downloaded. Find the following content comment in the viewer.js file:

Annotate the contents in the red box in the above picture. If the server provides a download address, you can use iframe nesting to solve it.

Summarize

In fact, you can see that cross-domain knowledge is sometimes very useful. It is used above. When I first started working on it, I couldn’t get it out. Later I found that I couldn’t download it across domains. There are currently more usages and questions, and everyone is welcome to raise them.

The above is the details of how to use pdfjs in vue to preview pdf files. For more information about using pdfjs in vue to preview pdf files, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • How to generate high-definition PDF files from HTML pages in Vue
  • How to use pdf.js to preview pdf files in Vue
  • Vue implements online preview of PDF files (using pdf.js/iframe/embed)
  • Examples of mobile phone number and email regular verification in Vue and sending verification code in 60s
  • Vue implements the sample code of sending PDF files to mailbox

<<:  How to create a virtual environment using virtualenv under Windows (two ways)

>>:  Virtual Box tutorial diagram of duplicating virtual machines

Recommend

Graphical steps of zabbix monitoring vmware exsi host

1. Enter the virtualization vcenter, log in with ...

jQuery plugin to achieve code rain effect

This article shares the specific code of the jQue...

Management of xinetd-based services installed with RPM packages in Linux

Table of contents Preface 1. Startup management b...

Summary of Docker common commands and tips

Installation Script Ubuntu / CentOS There seems t...

webpack -v error solution

background I want to check the webpack version, b...

Briefly describe the four transaction isolation levels of MySql

Isolation Level: Isolation is more complicated th...

A brief discussion on mysql backup and restore for a single table

A. Installation of MySQL backup tool xtrabackup 1...

How to import txt into mysql in Linux

Preface When I was writing a small project yester...

How does Zabbix monitor and obtain network device data through ssh?

Scenario simulation: The operation and maintenanc...

Docker port mapping and external inaccessibility issues

The Docker container provides services and listen...

Ubuntu installation Matlab2020b detailed tutorial and resources

Table of contents 1. Resource files 2. Installati...

Analyze the duration of TIME_WAIT from the Linux source code

Table of contents 1. Introduction 2. First, let&#...

Vue recursively implements three-level menu

This article example shares the specific code of ...