1、使用npm 下载vue-pdf

npm install --save vue-pdf

2、封装pdf组件

<template>
	<div style="height: 75vh;overflow-y: auto;overflow-x: hidden;">
		<pdf :src="src"></pdf>
	</div>
</template>
import pdf from 'vue-pdf'
export default {
    components: {pdf},
    data(){
        return{
            src: '',
        }
    },
    methods: {
        //预览pdf
        previewPDF(url){
            this.src = pdf.createLoadingTask(url)
        },
        //下载PDF
        downloadPDF(url,fileName){
            this.axios({
                method: 'get',
                responseType: 'arraybuffer',
                url: url
            }).then(function(res){
            		//调用下载方法,把后端传过来的流传给fileDownload方法
                    this.fileDownload(res.data, fileName);
                }.bind(this)
            )
            .catch(
                function(error) {
                    this.$Message.error("网络请求出错")
                    //调试阶段可以看看报的什么错
                    //console.log("error",error)
                }.bind(this)
            );
        },
        fileDownload:function (data, fileName) {
            let blob = new Blob([data], {
				//type类型后端返回来的数据中会有,根据自己实际进行修改
                type: "application/vnd.ms-excel"
            });
            let filename = fileName || "报表.xls";
            if (typeof window.navigator.msSaveBlob !== "undefined") {
                window.navigator.msSaveBlob(blob, filename);
            } else {
                var blobURL = window.URL.createObjectURL(blob);
                // 创建隐藏<a>标签进行下载
                var tempLink = document.createElement("a");
                tempLink.style.display = "none";
                tempLink.href = blobURL;
                tempLink.setAttribute("download", filename);
                if (typeof tempLink.download === "undefined") {
                    tempLink.setAttribute("target", "_blank");
                }
                document.body.appendChild(tempLink);
                tempLink.click();
                document.body.removeChild(tempLink);
                window.URL.revokeObjectURL(blobURL);
            }
        }
    }
}

3、在组件中具体使用封装的pdf组件

<template>
	<div>
		<PDF ref="pdf"></PDF>
		<Button type="primary" size="small" @click="previewWeekly(item.id)">预览</Button>
    	<Button type="primary" size="small" @click="downloadWeekly(item.id,item.name)">下载</Button>
	</div>
</template>
<script>
import PDF from '../../../../../components/UM/MAM/pdfPerviewDownload'
import Vue from 'vue';
export default {
    components: {PDF},
    methods:{
    	//预览
        previewWeekly(id){
        	//调用子组件的预览方法
        	//完整的地址应该是发布到服务器的地址,不应该是localhost:8080这样的
        	//Vue.prototype.ApiUrl 用来读取服务器地址 比如:
        	//http://192.168.8.152:8080/MixShowView-Web/reports/preview/1
            this.$refs.pdf.previewPDF(Vue.prototype.ApiUrl+'/reports/preview/'+id)
        },
        //下载
        downloadWeekly(id,fileName){
        	//调用子组件的下载方法
            this.$refs.pdf.downloadPDF(Vue.prototype.ApiUrl+'/reports/download/'+id,fileName)
        }
    }
};
</script>

3、打包的时候会报错

if (Object({"NODE_ENV":"production","API_ROOT":http://192.168.6.112:8080/MaxTunnel-Web}).VUE_ENV !== 'server') {
      var pdfjsWrapper = __webpack_require__("ytml").default;

  Build failed with errors.

解决方案是在build/webpack.prod.conf.js中加上

new webpack.DefinePlugin({
    'process.env': env,
    'process.env.VUE_ENV': JSON.stringify(process.env.VUE_ENV) //增加此行,将src地址转换成json字符串。
}),
Logo

前往低代码交流专区

更多推荐