java+前端实现在线预览PDF功能

后台读取本地文件转成流

 @PostMapping("/getPdf")
    public   static void  outPutLocal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            File file = new File("C:/Users/molly.li/Desktop/变更流程.pdf");
            FileInputStream fileInputStream = new FileInputStream(file);
            response.setHeader("Content-Type", "application/pdf");
            OutputStream outputStream = response.getOutputStream();
            IOUtils.write(IOUtils.toByteArray(fileInputStream), outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

前台引入

npm install --save vue-pdf

前台请求后台接口时需要明确类型responseType(主要)

如果不明确responseType的类型时,前台接收转换出的PDF文档为空白文档
在这里插入图片描述

import request from '@/plugins/request/index';
// 查询变更详细信息
export function outPutLocal (data) {
    return request({
        url: '/workbench/FileConversion/getPdf',
        method: 'post',
        data: data,
        responseType: 'blob'
    })
}

引入

// 调用的后台接口路径
import {outPutLocal} from "@api/epdmWorkbench/FileConversionApi";
// 引入的pdf
import PDF from 'vue-pdf'
<p-d-f
                        v-for="i in numPages"
                        :key="i"
                        :src="src"
                        :page="i"
                        class="pdf-set"
                    ></p-d-f>
  components: {PDF},

在这里插入图片描述

实现方法

 // 查询pdf文件转换
        previewPdfFile(){
            let that = this
            outPutLocal().then(res=>{
                const blob = new Blob([res], { type: 'application/pdf' }) // res--后台返回的文件流
                // 转换pdf的预览路径
                this.src = that.getObjectURL(blob);
                // 不分页查看PDF
                this.loadPdf(this.src)
            })
        },
        // 将返回的流数据转换为url
        getObjectURL(file) {
            let url = null;
            if (typeof window.createObjectURL != 'undefined') { // basic
                url = window.createObjectURL(file);
            } else if (typeof window.webkitURL != 'undefined') { // webkit or chrome
                try {
                    url = window.webkitURL.createObjectURL(file);
                } catch (error) {
                }
            } else if (typeof window.URL != 'undefined') { // mozilla(firefox)
                try {
                    url = window.URL.createObjectURL(file);
                } catch (error) {
                }
            }
            return url;
        },

获取PDM的文件的页数

 // 获取PDM的文件的页数
        loadPdf (url) {
            this.src = PDF.createLoadingTask(url)
            this.src.promise.then(pdf => {
                this.numPages = pdf.numPages // 这里拿到当前pdf总页数
            })
        },
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值