Vue implements online preview of PDF files (using pdf.js/iframe/embed)

Vue implements online preview of PDF files (using pdf.js/iframe/embed)

Preface

I am currently working on a high-quality course that requires online preview of the courseware PPT. Our idea is to convert the PPT into PDF for online preview, so the question is how to achieve online preview of PDF.

During the implementation process, in order to better display the effect, I used a variety of different methods and finally chose pdf.js which had the best effect.

Implementation method:

1: iframe

Embed PDF into web page with iframe to achieve preview effect. The idea is good and the implementation is simple, but the display is cruel...

Although one line of code is concise and clear, and the effect is OK when opening Google Chrome, the shortcomings are also very obvious! ! ! !

<iframe src="http......" width="100%"></iframe>

shortcoming:

(1) Incompatible with IE, because iframe is not standardized and IE no longer uses it

(2) Download pop-up window! ! ! Every time you open the preview, a download pop-up window pops up, which is a very bad user experience

2: embed

Embed and iframe feel similar things. They are equally simple and clear to use, and the effect is also good when opening IE, but the interference of download pop-up windows cannot be avoided.

 <embed src="http......" width="100%" height="400" />

3: pdf.js (the effect is the best)

Implementation steps:

(1) Download the pdf.js file

Because there are many pdf.js files, we only need to use the core files, so extract the core files (cross-domain errors have been handled). Click to download the core file

(2) Import core files into static

(3) Use

<template>
	<iframe :src="pathUrl" width="100%"></iframe>
</template>

<script>
export default {
	data () {
	   return {
	     pathUrl:''
	   }
	 },
	 mounted () {
      this.pathUrl = '../../../../../static/pdf/web/viewer.html?file=' + encodeURIComponent('https://lidn02.o%BA.pdf') // Find the viewe.html in the pdf file introduced before and + pdf address},
 }
</script>

(4) The effect is compatible with all major browsers.

(5) Receive PDF as a stream

Although the above effect has been achieved, there is still an error line when opening the console:

In order to solve this problem, or when encountering cross-domain, the PDF file is received in the form of a stream and then displayed:

mounted(){
	this.getData(your pdf address)
}

//Receive file stream, convert address and then display getData(url){
  axios.get(url,{
    responseType:'blob',
  })
    .then(res => {
      let blob = new Blob([res.data], {type: "application/vnd.ms-excel"})
      let objectUrl = URL.createObjectURL(blob)
      this.pathUrl = '../../../../../static/pdf/web/viewer.html?file=' + objectUrl
    })
    .catch(err => {
      console.log(err)
    })
}

Summarize

This is the end of this article about Vue's online preview of PDF files. For more relevant Vue online preview of PDF files, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue implements online preview of PDF files and downloading (pdf.js)
  • Vue implements online preview function of PDF document
  • Vue plugin development: How to use pdf.js to realize online preview of PDF documents on mobile phones
  • Vue sample code for online preview of office files
  • vue Sample code for using vue-pdf to implement PDF online preview
  • Vue-pdf implements online preview of PDF files
  • vue-pdf realizes online file preview
  • Online preview of three common file types in Vue projects (pdf/word/excel tables)

<<:  Mysql: The user specified as a definer ('xxx@'%') does not exist solution

>>:  How the Linux kernel breaks into the process address space and modifies the process memory

Recommend

Tutorial on migrating mysql from phpstudy to Linux

Project Purpose Migrate the data in MySQL 5.5.53 ...

Detailed explanation of vuex persistence in practical application of vue

Table of contents vuex persistence Summarize vuex...

Three ways to configure Nginx virtual hosts (based on domain names)

Nginx supports three ways to configure virtual ho...

MySQL Series 7 MySQL Storage Engine

1. MyISAM storage engine shortcoming: No support ...

Details of 7 kinds of component communication in Vue3

Table of contents 1. Vue3 component communication...

How to implement responsive layout in vue-cli

When we are doing front-end development, we will ...

Solution to ElementUI's this.$notify.close() call not working

Table of contents Requirement Description Problem...

Introduction to the method attribute of the Form form in HTML

1 method is a property that specifies how data is ...

Solve the compatibility issue between MySQL 8.0 driver and Alibaba Druid version

This article mainly introduces the solution to th...

CentOS 6-7 yum installation method of PHP (recommended)

1. Check the currently installed PHP packages yum...

Steps and pitfalls of upgrading linux mysql5.5 to mysql5.7

Table of contents Linux MySQL 5.5 upgraded to MyS...

View the command to modify the MySQL table structure

Brief description The editor often encounters som...