uniapp使用pdf.js在线查看附件

效果图

 

pdf.js下载地址:官网下载地址

下载之后

第一步
将文件放在static文件下,之前我按其他的教程放在pages同级下打开是我的页面首页

 第二步

我用的iframe

<iframe style="width: 100%;height: 100%;" scrolling="no" frameborder="0" id="dtdt-map" class="dtdt-map" :src="pdfpath"></iframe>

//我的path是后端返回的文件地址
var pdfpath = path.substring(0, index + 1) + "pdf";

//如果带参数,带数字还是用js解码一下,/static/hybrid/html/web/viewer.html为存放的pdf.js路径
pdfpath = `/static/hybrid/html/web/viewer.html?file=` + encodeURIComponent(pdfpath)

 第三步
修改源代码

1 pdf.js是不支持跨域文件加载的 ,会报 “file origin doesnot match viewer”错误。改一下源码。把下面图片上代码注释掉。

 我封装成了一个组件,完整代码

<file-view>组件部分

<template name="modal">
	<view class="">
		<!-- 页面附件展示 -->
		<view class="file">
			<view class=" " v-for="(item,index) in fjArra" :key="index" style="margin-top: 10upx;">
				<view style="min-width: 205upx;">
					<view class="" style="color: #3F8EF7;" @click="handlePictureCardPreview(item.url,item.name)">
						{{index+1}}. {{item.name}}
					</view>
				</view>
			</view>
		</view>

		<!-- pdf查看弹框 -->
		<view class="modalInputBg" v-show="modalData.showModal" @tap="modalData.showModal = false">
			<view class="modalInput">
				<view class="modalDiv">
					<view class="title">
						<iframe style="width: 100%;height: 100%;" scrolling="no" frameborder="0" id="dtdt-map"
							class="dtdt-map" :src="modalData.url"></iframe>
					</view>
					<view class="bottom">
						<view class="bottomCancel" @tap.stop="modalData.showModal = false">关闭</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		props: {
			// 接收附件数据,格式为[{name:'xxx',url:'xxx'}]
			fjArra: {
				type: Array,
				default: () => []
			}
		},
		data() {
			return {
				modalData: {
					showModal: false,
					title: '标题名称',
					url: '',
					pic: '',
				},
				originalUrl:''
			}
		},

		methods: {
			onModalShow() {
				this.modalData.showModal = true
			},
			downFile() {
				window.location.href = this.originalUrl
			},
			handlePictureCardPreview(path) {
				this.modalData.pic = ''
				this.modalData.url = ''
				this.originalUrl= ''
				var index = path.lastIndexOf('.')
				var type = path.substring(index + 1, path.length).toUpperCase()
				console.log(type,'type');
				if (type == 'PNG' || type == 'JPG' || type == 'JPEG' || type == 'BMP' || type == 'GIF' ||
					type == 'WEBP' || type == 'PSD' || type == 'SVG' || type == 'TIFF') {
					this.modalData.pic = path
					// 如果是图片类型就直接查看
					uni.previewImage({
						current: this.modalData.pic,
						urls: [this.modalData.pic]
					})
				} else if (type == 'DOCX' || type == 'DOC' || type == 'TXT' || type == 'XLS' || type == 'XLSX' ||
					type == 'PPT' || type == 'PPTX' || type == 'PDF') {
					var pdfpath = path.substring(0, index + 1) + "pdf";
					pdfpath = `/static/hybrid/html/web/viewer.html?file=` + encodeURIComponent(pdfpath)
					this.modalData.url = pdfpath
					this.onModalShow()
				} else {
			         // 如果是其他类型文件就直接下载				
				    this.originalUrl = path.replace("/file-server/file/download", "/file-server/file/getFile");
					this.downFile()
				}
			},
			onDefine() {
				this.modalData.showModal = false
			},
		}
	}
</script>
<style>
	.modalInputBg {
		position: fixed;
		z-index: 999;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, .2);
	}

	.modalInput {
		width: 100%;
		height: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.modalDiv {
		width: 90%;
		height: auto;
		background-color: #FFFFFF;
		border-radius: 10rpx;
	}

	.modalInput .modalDiv .title {
		text-align: center;
		/* padding: 48rpx 0 0 0; */
		letter-spacing: 2rpx;
		font-size: 44rpx;
		height: 80vh;
	}

	.modalInput .modalDiv .body {
		width: 86%;
		height: 80rpx;
		line-height: 80rpx;
		margin: 72rpx auto;
		border: 2rpx solid #DCDEE0;
		box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.08);
		padding: 0 3%;
		box-sizing: border-box;
		border-radius: 10rpx;
	}

	.modalInput .modalDiv .body input {
		height: 100%;
	}

	.modalInput .modalDiv .bottom {
		display: flex;
		align-items: center;
		height: 88rpx;
		border-top: 2rpx solid #DCDEE0;
		box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.08);
	}

	.modalInput .modalDiv .bottom view {
		flex: 1;
		text-align: center;
		font-size: 30rpx;
		letter-spacing: 2rpx;
		height: 100%;
		line-height: 88rpx;
	}

	.modalInput .modalDiv .bottom .bottomCancel {
		border-right: 2rpx solid #DCDEE0;
	}

	.modalInput .modalDiv .bottom .bottomDefine {
		color: #3F83F7;
	}

	.file {
		margin: 0upx 40upx 30upx;
		border-bottom: 2upx solid #eee;
	}
</style>

使用:

<u--text slot="title" type="info" text="附件:"></u--text>
<file-view :fjArra='fjArra'></file-view>


    this.fjArra = []
	if (this.notication.files) {
		this.notication.files.split(',').forEach((item) => {
			if (item.trim().length > 0) {
				this.fjArra.push({
					name: decodeURIComponent(item.substring(item.lastIndexOf(
							'=') +
						     1)),
						url: item
					})
				}
			})
	 	 } else {
	   this.fjArra = []
     }

UniApp是一个基于Vue.js开发的跨平台应用框架,可以同时开发iOS、Android和Web应用。要在UniApp使用pdf.js,可以按照以下步骤进行操作: 1. 首先,需要在UniApp项目中安装pdf.js库。你可以使用npm来安装pdf.js,具体命令如下: ``` npm install pdfjs-dist ``` 2. 安装完成后,在你的Vue组件中引入pdf.js库: ```javascript import pdfjsLib from 'pdfjs-dist' ``` 3. 接下来,你可以使用pdf.js提供的API来加载和显示PDF文件。以下是一个简单的示例: ```javascript export default { data() { return { pdf: null, currentPage: 1, totalPages: 0 } }, mounted() { // 加载PDF文件 this.loadPDF() }, methods: { async loadPDF() { const pdfUrl = 'path/to/your/pdf/file.pdf' // 使用pdf.js加载PDF文件 const loadingTask = pdfjsLib.getDocument(pdfUrl) const pdf = await loadingTask.promise this.pdf = pdf this.totalPages = pdf.numPages // 显示第一页 this.showPage(this.currentPage) }, async showPage(pageNumber) { if (!this.pdf) return // 获取指定页的渲染对象 const page = await this.pdf.getPage(pageNumber) // 创建一个canvas元素用于渲染PDF内容 const canvas = document.createElement('canvas') const context = canvas.getContext('2d') // 设置canvas的大小 const viewport = page.getViewport({ scale: 1 }) canvas.width = viewport.width canvas.height = viewport.height // 渲染PDF内容到canvas const renderContext = { canvasContext: context, viewport: viewport } await page.render(renderContext).promise // 将canvas添加到页面中显示 document.body.appendChild(canvas) } } } ``` 上述示例代码中,`loadPDF()`方法用于加载PDF文件,`showPage(pageNumber)`方法用于显示指定页的内容。你可以根据自己的需求进行修改和扩展。 这样,你就可以在UniApp使用pdf.js来加载和显示PDF文件了。记得确保你已经在项目中添加了对应的PDF文件,并使用正确的路径来引用它。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值