vue-pdf插件的使用,并通过Java后端文件流的方式解决vue跨域问题

vue-pdf插件的使用,并通过Java后端文件流的方式解决vue跨域问题

安装

npm install --save vue-pdf

简单用法

直接上代码-前端写法

<template>
  <div>
    <pdf :src="pdfUrl">
    </pdf>
  </div>
</template>
<script>
  import pdf from 'vue-pdf'
  import { getPdf } from "../../api/test/test";

  export default {
    name: 'Pdf',
    components: {
      pdf
    },
    data() {
      return {
        pdfUrl: null,
        numPages: 1
      }
    },
    created() {
      this.getpdf()
    },
    methods: {
      getpdf() {
        const that = this;
        let formData = new FormData();
        formData.append("pdfUrl", "https://dakaname.oss-cn-hangzhou.aliyuncs.com/file/2018-12-28/1546003237411.pdf");
        getPdf(formData).then(data => {
          that.pdfUrl = that.getObjectURL(data);
          console.log("pdfUrl", that.pdfUrl);
        });
      },
      // 将返回的流数据转换为url
      getObjectURL(file) {
        let url = null;
        if (window.createObjectURL != undefined) { // basic
          url = window.createObjectURL(file);
        } else if (window.webkitURL != undefined) { // webkit or chrome
          try {
            url = window.webkitURL.createObjectURL(file);
          } catch (error) {
            console.log(error)
          }
        } else if (window.URL != undefined) { // mozilla(firefox)
          try {
            url = window.URL.createObjectURL(file);
          } catch (error) {
            console.log(error)
          }
        }
        return url;
      },
    }
  }
</script>

前端请求方式

import request from '@/utils/request'


export function getPdf(data) {
  return request({
    url: '/test/getPdf',
    method: 'post',
    responseType: 'blob', //非常关键
    data: data
  })
}

Java后端文件流的方式解决vue跨域问题

package com.ruoyi.web.controller.test;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author lanren
 * @Description
 * @date 2020/12/11
 */
@RestController
@RequestMapping("/test")
public class TestApiController {

    @RequestMapping(value = "/getPdf", method = {RequestMethod.GET, RequestMethod.POST})
    public void getPdf(String pdfUrl, HttpServletRequest req, HttpServletResponse resp) {
        if (pdfUrl == null) {
            pdfUrl = "";
        }
        resp.setContentType("application/pdf");
        OutputStream sos = null;
        BufferedInputStream bis = null;
        try {
            sos = resp.getOutputStream();
            String destUrl = pdfUrl;
            URL url = new URL(destUrl);
            HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();
            //连接指定的网络资源
            httpUrl.connect();
            //获取网络输入流
            bis = new BufferedInputStream(httpUrl.getInputStream());
            int b;
            while ((b = bis.read()) != -1) {
                sos.write(b);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                sos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

导入

插件GitHub

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值