浏览器中展示PDF(兼容IE)

比特星辰客
• 阅读 4730
之前接到个需求:需要在chrome浏览器、IE10和IE11中直接查看PDF,最好是不需要安装插件或设置浏览器选项。
先是百度了一下,找到一个插件react-pdf 当时搜到博客说可以兼容IE10。

一、react-pdf 踩坑(着急找答案的可跳过)

代码如下:
/*

* 展示PDF的组件

* */

import React, { PureComponent } from 'react';

import PropTypes from 'prop-types';

import LoadingOrEmptyComponent from 'components/LoadingOrEmptyComponent';

import { Document, Page } from 'react-pdf';

import style from './PDFComponent.less'

class PDFComponent extends PureComponent {

 constructor(props) {

 super(props);

 this.state = {

     numPages: null,

     pageNumber: 1,

  }

 }

 static propTypes = {

    textDataSet:PropTypes.object

 };

 static defaultProps = {

    textDataSet:{}

 };

 onDocumentLoadSuccess = ({ numPages }) => {

    this.setState({ numPages });

 }

 render() {

 let { textDataSet } = this.props;

 let { pageNumber,numPages } = this.state;

 let src = textDataSet.EMR_FILE_PATH;

 if(src){
     return(

         <iframe src={src} width="100%" height={"800px"}>

             <div align="center">

                 <Document

                 file={src}

                 onLoadSuccess={this.onDocumentLoadSuccess}>

                 <Page pageNumber={pageNumber} />

                 </Document>

                <p>Page {pageNumber} of {numPages}</p>

             </div>

         </iframe>

     )

     }else{

         return(

            <LoadingOrEmptyComponent isLoadingOrEmpty={true}/>

         )

     }

  }

}

export default PDFComponent;

试了下,并不能兼容IE10,我太难了,当时前方一直催进度,差点急死~
浏览器中展示PDF(兼容IE)
接着各种找,后来发现另一个:pdf.js

二、pdf.js

核心部分是pdf.js和pdf.worker.js,一个负责API解析,一个负责核心解析

2.1使用方法如下:

下载 pdf.js(点此链接)
然后将pdfjs放到前端代码的public下。
封装一个pdf组件,代码如下:

/*

* 展示PDF的组件

* */

import React, { PureComponent } from 'react';

import PropTypes from 'prop-types';

import LoadingOrEmptyComponent from 'components/LoadingOrEmptyComponent';

class PDFComponent extends PureComponent {

 constructor(props) {

 super(props);

     this.state = {
     }

 }

 static propTypes = {

    textDataSet:PropTypes.object

 };

 static defaultProps = {

    textDataSet:{}

 };

 componentDidMount(){

     let { subTabKey,textDataSet} = this.props;

     let type = textDataSet.EMR_FILE_TYPE;

     let file_path = textDataSet.EMR_FILE_PATH;

     let basePath = "/uranus/UI/detail/fileProxy?fileType="+ encodeURIComponent(type) +'&url='+encodeURIComponent(file_path);

     document.getElementById("pdfIframe" + subTabKey).src = "pdfjs/web/viewer.html?file="+encodeURIComponent(basePath);

 }

 render() {

     let { textDataSet,subTabKey} = this.props;

     let src = textDataSet.EMR_FILE_PATH;

     if(src){

         return(

             <iframe name="pdfIframe" id={"pdfIframe" + subTabKey} width="100%" height="100%">

             </iframe>

         )

     }else{

         return(

            <LoadingOrEmptyComponent isLoadingOrEmpty={true}/>

         )

     }

  }

}

export default PDFComponent;
注意:采用的版本是2.0,最新的版本不支持IE10。
写得比较糙,中间还有很多踩坑过程就省略了,也没能花时间仔细研究react-pdf兼容IE的办法,如有问题欢迎指正,欢迎多交流沟通~
点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
美凌格栋栋酱 美凌格栋栋酱
6个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
想天浏览器 想天浏览器
2年前
想天浏览器3.2正式版推送更新
快来看更新!经过我们一个多月的努力开发,想天浏览器3.2正式版推送更新啦。    图片下方是本次更新的主要内容↓↓浅色模式3.2改进的深色模式新增内容1.兼容插件机制插件的crx形式安装插件的解压包形式安装插件的工具栏展示与否设置插件的启用禁用chrome商店安装到想天浏览器,同时支持下载
Peter20 Peter20
4年前
mysql中like用法
like的通配符有两种%(百分号):代表零个、一个或者多个字符。\(下划线):代表一个数字或者字符。1\.name以"李"开头wherenamelike'李%'2\.name中包含"云",“云”可以在任何位置wherenamelike'%云%'3\.第二个和第三个字符是0的值wheresalarylike'\00%'4\
Wesley13 Wesley13
3年前
FLV文件格式
1.        FLV文件对齐方式FLV文件以大端对齐方式存放多字节整型。如存放数字无符号16位的数字300(0x012C),那么在FLV文件中存放的顺序是:|0x01|0x2C|。如果是无符号32位数字300(0x0000012C),那么在FLV文件中的存放顺序是:|0x00|0x00|0x00|0x01|0x2C。2.  
Stella981 Stella981
3年前
SpringBoot整合Redis乱码原因及解决方案
问题描述:springboot使用springdataredis存储数据时乱码rediskey/value出现\\xAC\\xED\\x00\\x05t\\x00\\x05问题分析:查看RedisTemplate类!(https://oscimg.oschina.net/oscnet/0a85565fa
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n
Easter79 Easter79
3年前
SpringBoot整合Redis乱码原因及解决方案
问题描述:springboot使用springdataredis存储数据时乱码rediskey/value出现\\xAC\\xED\\x00\\x05t\\x00\\x05问题分析:查看RedisTemplate类!(https://oscimg.oschina.net/oscnet/0a85565fa
Wesley13 Wesley13
3年前
Java日期时间API系列36
  十二时辰,古代劳动人民把一昼夜划分成十二个时段,每一个时段叫一个时辰。二十四小时和十二时辰对照表:时辰时间24时制子时深夜11:00凌晨01:0023:0001:00丑时上午01:00上午03:0001:0003:00寅时上午03:00上午0
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable