vue 集成 pdf.js 文件流方式预览 pdf

1.后端输出字节流

    public void getFileStream(Long ossFileId, HttpServletResponse response) {
        InputStream inputStream = null;
        try {
            OssFile vo = fileService.getById(ossFileId);
            String fileUrl = minioTemplate.getObjectUrl(vo.getBucketName(), vo.getFilename());

            //如果是pdf直接输出流预览,如果是excel转换成pdf预览,如果是其他文件,输出“改文件格式不支持预览!”
            if (vo.getFileSuffix().equals("xls") || vo.getFileSuffix().equals("xlsx")) {
                InputStream inputStream2 = new FileInputStream(AsposeUtils.excel2pdfBySuffix(fileUrl, vo.getFileSuffix()));
                response.getOutputStream().write(readInputStream(inputStream2));
                inputStream2.close();
            } else if (vo.getFileSuffix().equalsIgnoreCase("pdf")) {
                inputStream = new URL(fileUrl).openStream();
                response.getOutputStream().write(readInputStream(inputStream));
            } else if (vo.getContentType().toLowerCase().indexOf("image") > -1) {
                InputStream inputStream2 = new FileInputStream(AsposeUtils.image2pdfBySuffix(fileUrl, vo.getFileSuffix()));
                response.getOutputStream().write(readInputStream(inputStream2));
                inputStream2.close();
            } else if (vo.getFileSuffix().equalsIgnoreCase("doc") || vo.getFileSuffix().equalsIgnoreCase("docx")) {
                InputStream inputStream2 = new FileInputStream(AsposeUtils.word2pdfBySuffix(fileUrl, vo.getFileSuffix()));
                response.getOutputStream().write(readInputStream(inputStream2));
                inputStream2.close();
            }else if (vo.getFileSuffix().equalsIgnoreCase("dwg")) {
                InputStream inputStream2 = new FileInputStream(AsposeUtils.dwg2pdfBySuffix(fileUrl, vo.getFileSuffix()));
                response.getOutputStream().write(readInputStream(inputStream2));
                inputStream2.close();
            } else {
                response.getOutputStream().write(readInputStream(AsposeUtils.getPreTitle()));
            }
            inputStream.close();
        } catch (Exception e) {
            try {
                response.getOutputStream().write(readInputStream(AsposeUtils.getPreTitle()));
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception ee) {

            }

        }
    }

    /**
     * 从输入流中获取字节数组
     *
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[inputStream.available()];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while ((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }

2.前端get请求方式


/**
 * 根据文件id查询文件流以pdf方式预览
 * @param id
 * @returns {*}
 */
export function getFileStream(id) {
  return `${api.darwing}` + '/file/pre/' + id
}
//调用:
window.open('/pdf/web/viewer.html?file=' + encodeURIComponent(getFileStream(record.id)) + '#page=1');

3.前端携带token请求方式

/**
 * 根据文件id查询文件流以pdf方式预览
 * @param id
 * @returns {*}
 */
export function getFileStream(id) {
  return axios({
    url: `${api.darwing}`  + '/file/pre/' + id,
    method: 'get',
    responseType: 'blob'
  })
}

// 后台返回流的形式
getFileStream(record.id).then((res)=>{
  let blob = new Blob([res.data],{type:"application/octet-stream"});
  let url = window.URL.createObjectURL(blob)
  this.pdfUrl = '/pdf/web/viewer.html?file=' + encodeURIComponent(url) + '#page=1' 
})
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值