针对android系统不支持pdf文档在线预览,可通过引入pdf.js插件实现,其具体实现步骤如下

一、引入插件

方式一:npm install --save pdfjs-dist,安装完成后在vue项目的node_modules出现如下依赖

vue实现pdf文档在线预览功能

方式二:只引入pdf.js的核心文件pdf.js和pdf.work.js,其他无关的文件全部删除,如图

vue实现pdf文档在线预览功能

方式三:将插件直接放在static文件夹下,如图

vue实现pdf文档在线预览功能

二、前端页面代码

方式一和方式二:特点精简

<template>
 <div>
 <canvas v-for="page in pages" :id="'the-canvas'+page" :key="page"></canvas>
 </div>
</template>
 
<script>
// 方式一
import PDFJS from 'pdfjs-dist'
// 方式二
import * as PDFJS from '../../../static/pdf/build/pdf'
 
export default {
 // 返回数据
 data () {
 return {
 pdfDoc: null,
 pages: 0
 }
 },
 created () {
 },
 mounted () {
 this.showPdf()
 },
 methods: {
 showPdf: function () {
 // 请求本地文件
 let url = '/static/pdf/web/compressed.tracemonkey-pldi-09.pdf'
 // 跨域请求文件,需要走后台代理,后台需要将文件流返回前端才可在页面显示
 // let url = '/pdf/showPdf"color: #800000">方式三:功能强大,但是引入过多无用文件,此种方式的filePath如为本地文件不进行编码也可发送请求,如为跨域文件不进行编码无法发送请求,因此建议统一进行编码。

<template>
 <div >
 <iframe :src="/UploadFiles/2021-04-02/url">
@Controller
public class ShowPdfController {
 @RequestMapping(name = "/showPdf")
 public String showPdf(HttpServletRequest request, HttpServletResponse response, String pdfUrl) {
 try {
  pdfUrl = pdfUrl.trim();
  URL url = new URL(pdfUrl);
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  conn.setConnectTimeout(5*1000);
  InputStream inputStream = conn.getInputStream();
  response.setHeader("Content-Disposition", "attachment;fileName=show.pdf");
  response.setContentType("multipart/form-data");
  OutputStream outputStream = response.getOutputStream();
  IOUtils.write(IOUtils.toByteArray(inputStream), outputStream);
 } catch (Exception e) {
  e.printStackTrace();
 }
 return null;
 }
}

具体采用哪种方式实现pdf文档的在线预览,可根据项目实际情况选择,如业务简单建议使用方式一和方式二(精简),如业务复杂建议使用方式三(功能强大)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
vue,pdf文档在线预览,vue,pdf在线预览,vue文档在线预览

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
评论“vue实现pdf文档在线预览功能”
暂无“vue实现pdf文档在线预览功能”评论...