uniapp 和react native pdf预览

uniapp 本地pdf预览

需要安装pdfh5插件

<view class="pdf">
		<view id="demo"></view>
	</view>
import Pdfh5 from "pdfh5"
import "pdfh5/css/pdfh5.css";
pdfh5: null
this.pdfh5 = new Pdfh5('#demo', {
	pdfurl: "/h5/static/sc.pdf"
	// responseType: "arraybuffer" // blob arraybuffer
});
.pdf {
	height: 90vh;
}

uniapp 线上pdf预览

uni.navigateTo({
	url: './webview?data=' + item3.FILE_ID + '&title=' + item3.DIS_NAME
})
<template>
	<uni-nav-bar left-icon="left" :fixed='true' :title='title' @clickLeft="leftClick" />
	<view class="pdf">
		<web-view :src="path" style="margin-top: 100rpx;"></web-view>
	</view>
</template>

<script>
	import {
		getKnowledgeFile
	} from '@/common/api.js'
	import config from '@/common/config.js'
	export default {
		data() {
			return {
				title: '',
				data: '',
				fileID: '',
				pdfUrl: '',
				path: '',
			}
		},
		mounted() {
			
		},
		onLoad(e) {
			this.title = e.title;
			this.fileID = e.data

			uni.setNavigationBarTitle({
				title: e.title,
			})
			this.getFile()
		},
		onReady() {
			// #ifdef APP-PLUS
			var currentWebview = this.$scope
				.$getAppWebview(); //此对象相当于html5plus里的plus.webview.currentWebview()。在uni-app里vue页面直接使用plus.webview.currentWebview()无效,非v3编译模式使用this.$mp.page.$getAppWebview()
			setTimeout(function() {
				wv = currentWebview.children()[0];
				wv.setStyle({
					top: 35,
				});
			}, 100); //如果是页面初始化调用时,需要延时一下
			// #endif
		},
		methods: {
			leftClick() {
				uni.navigateBack()
			},
			getFile() {
				var key = '?jwtToken=' + uni.getStorageSync('PCM_TOKEN') + '&fileId=' + this.fileID
				this.pdfUrl = config.baseUrl + '/kl/MOBILE_INTERFACE_SERVICES.downloadFile.do' + key				
				this.path = '/hybrid/html/web/viewer.html?file=' + encodeURIComponent(this.pdfUrl)
			},
		}
	}
</script>

<style lang="scss" scoped>
	.pdf {
		height: 90vh;
	}
</style>

react native pdf预览

需要下载react-native-pdf插件,页面写了翻页和页面跳转功能

 this.props.navigation.navigate('pdfViewer', {
      data: {
        data: item3,
        title: item3.DIS_NAME
      }
    })
 import React, {Component} from 'react';
import {
  View,
  Text,
  Dimensions,
  TouchableOpacity,
  StyleSheet, TextInput,
} from 'react-native';
import {default as config} from "../../../config/base";
import NavBarC from '../../../components/common/NavBarC';
import Pdf from 'react-native-pdf';
const {width, height} = Dimensions.get('window');
export default class pdfViewer extends Component {
  static navigationOptions = {
    header: null
  };
  
  constructor (props) {
    super(props);
    this.state = {
      pdfUrl: '',
      currentPage: 1,
      totalPage: 1,
    };
    this.data = this.props.navigation.state.params.data ? this.props.navigation.state.params.data : {};
  }
  
  componentDidMount () {
    this.data = this.props.navigation.state.params.data ? this.props.navigation.state.params.data : {};
    if (this.data.data.FILE_ID) {
      var pdfUrl = config.applicationIpAndPortPcm + 'MOBILE_INTERFACE_SERVICES.downloadFile.do' + '?jwtToken=' + global.pcmToken + '&fileId=' + this.data.data.FILE_ID;
      this.setState({pdfUrl: pdfUrl});
    } else {
      var pdfUrlB = config.applicationIpAndPort + 'jdjhapi/sdc/engineeringStandardDocuments/preview/' + this.data.data.fileId + '?Authorization=' + global.Authorization;
      this.setState({pdfUrl: pdfUrlB});
    }
  }
  
  componentWillUnmount () {
  }
  
  render () {
    const {pdfUrl, currentPage, totalPage} = this.state;
    const source = {uri: pdfUrl, cache: true};
    return (
      <View style={styles.container}>
        <NavBarC
          goBack={() => {
            this.props.navigation.goBack()
          }}
          midView={<Text style={{color: '#0F2646', fontSize: 15}}
                         numberOfLines={1}
                         ellipsizeMode={'tail'}>{this.data.title ? this.data.title : this.data.data.fileName}</Text>}
        />
        <View style={styles.pdf}>
          {
            pdfUrl !== '' ?
              <Pdf
                ref={(pdf) => {
                  this.pdf = pdf;
                }}
                page={currentPage}
                source={source}
                onLoadComplete={(numberOfPages, filePath) => {
                  this.setState({totalPage: numberOfPages})
                }}
                onPageChanged={(page, numberOfPages) => {
                  this.setState({currentPage: page})
                }}
                onError={(error) => {
                  // console.log('s', error);
                }}
                enablePaging={true}
                style={styles.pdfs}
              />
              : null
          }
        </View>
        <View style={styles.navigate}>
          <TouchableOpacity
            onPress={() => {
              let currentPage = this.state.currentPage - 1
              this.setState({currentPage: currentPage})
            }}>
            <Text style={styles.naviText}>上一页</Text>
          </TouchableOpacity>
          <View>
            <Text style={styles.cenText}>{currentPage} / {totalPage}</Text>
          </View>
          <View style={styles.center}>
            <Text style={styles.naviText}>前往</Text>
            <View style={styles.input}>
              <TextInput
                placeholder='输入'
                placeholderTextColor='#333333'
                underlineColorAndroid='transparent'
                keyboardType={'numeric'}
                onChangeText={(text) => {
                  const number = parseInt(text, 10)
                  this.setState({currentPage: number})
                }}
                value={currentPage}
                style={{fontSize: 15, color: '#333333', textAlign: 'center'}}/>
            </View>
            <Text style={styles.naviText}></Text>
          </View>
          <TouchableOpacity
            onPress={() => {
              let currentPage = this.state.currentPage + 1
              this.setState({currentPage: currentPage})
            }}>
            <Text style={styles.naviText}>下一页</Text>
          </TouchableOpacity>
        </View>
      </View>
    );
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#EFEFF4',
  },
  pdf: {
    flex: 1,
    width: width,
    height: height,
    alignItems: 'center',
    justifyContent: 'center'
  },
  pdfs: {
    width: '100%',
    height: '100%',
    alignItems: 'center',
    justifyContent: 'center'
  },
  navigate: {
    width: width - 30,
    marginLeft: 15,
    borderRadius: 20,
    height: 50,
    backgroundColor: 'gray',
    position: 'absolute',
    zIndex: 2,
    bottom: 10,
    left: 0,
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-around'
  },
  center: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-around',
    width: 90,
  },
  naviText: {
    color: "#fff",
    textAlign: 'center',
    fontSize: 13,
  },
  cenText: {
    color: "#fff",
    textAlign: 'center',
    fontSize: 15,
  },
  input: {
    width: 40,
    height: 40,
    backgroundColor: '#fff',
    borderRadius: 10,
  }
});


在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值