React移動端項目之pdf預覽問題
React移動端項目之pdf預覽
因為項目需要,需要在在項目中實現(xiàn)pdf文件遇見功能,眾所周知,安卓老大哥貌似不怎么支持,所以采用的react-pdf插件實現(xiàn)方式,實現(xiàn)方式很簡單:
引入react-pdf包
yarn add react-pdf 或者npm install react-pdf --save
封裝pdf組件:(官網demo)
import?React,?{?Component?}?from?'react';
import?{?Document,?Page?}?from?'react-pdf';
//如果報錯?
Uncaught SyntaxError: Unexpected token <index.js:1452
Error: Setting up fake worker failed: "Cannot read property 'WorkerMessageHandler' of undefined". at pdf.js:10999
//就增加這兩句
import?{?pdfjs?}?from?'react-pdf';
pdfjs.GlobalWorkerOptions.workerSrc?=?`//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;
class?MyApp?extends?Component?{
??state?=?{
????numPages:?null,
????pageNumber:?1,
??}
??onDocumentLoadSuccess?=?({?numPages?})?=>?{
????this.setState({?numPages?});
??}
??render()?{
????const?{?pageNumber,?numPages?}?=?this.state;
????return?(
??????<div>
????????<Document
??????????file="somefile.pdf"
??????????onLoadSuccess={this.onDocumentLoadSuccess}
? ? ? ? ? ?page = {pageNumber}
????????>
??????????<Page?pageNumber={pageNumber}?/>
????????</Document>
????????<p>Page?{pageNumber}?of?{numPages}</p>
??????</div>
????);
??}
}奉上官網demo地址https://www.npmjs.com/package/react-pdf
需求暫時可以實現(xiàn),但是不完美的是這個插件 實現(xiàn)的是pdf文件分頁預覽法,以為這不能夠常預覽,想預覽下頁,就需要自己做分頁,改變page屬性的值,后期想的解決辦法就是滾動翻頁代替點擊翻頁,雖然還是單頁預覽但是稍微比點擊翻頁好點,后期小編突發(fā)奇想,渲染多個封裝的pdf組件,每個組件只顯示一頁pdf內容,嘗試了下還是可以實現(xiàn)常預覽的,至于性能方面,小編的意思是在加載完第一頁之后在渲染之后的,防止一次加載同一文件多次,浪費性能
React pdf前端顯示 react-pdf-js踩坑
因為業(yè)務需求,大佬讓我做一個poc 可以在前端展示pdf,試了很多插件,也試了大名鼎鼎的pdfjs,但是由于本身架構就使用react,所以最后選用了react-pdf-js。
在查詢資料過程中發(fā)現(xiàn)官網的demo已經打不開了。這點比較坑,所以只能找一些其他大佬的文章了。
webpack
- "react-pdf-js": "^4.0.1",
- "webpack": "^2.7.0"
- "react": "16.5.1",
報錯
1.message: "Invalid PDF structure"
2.name: "InvalidPDFException"

原因引入方式不正確。
之前的代碼為
file={'../../doc/test.pdf'}應改為
const pdfurl = require('../../doc/test.pdf')
......
render(){
return (
<div>
<PDF
file={pdfurl}
onDocumentComplete={this.onDocumentComplete.bind(this)}
page={this.state.page}
/>
<Pagination onChange={this.onChange.bind(this)} total={this.state.pages} current={this.state.page}/>
</div>
)
}
}因為之前項目完全沒有做到pdf 所以在webpack中沒有做相對應的loader
報錯
You may need an appropriate loader to handle this file type.
(Source code omitted for this binary file)
webpack中添加如下
{
test: /\.(pdf|svg)$/,
use: 'file-loader?name=[path][name].[ext]',
}最后就可以了 完整代碼如下
import React from 'react';
import { connect } from 'react-redux';
import { Pagination } from 'antd'
const pdfurl = require('../../doc/test.pdf')
import PDF from 'react-pdf-js';
class Test extends React.Component {
constructor (){
super()
this.state={
"page":1,
"pages":1
}
}
onDocumentComplete(pages) {
this.setState({ page: 1, pages:pages });
}
onChange (page) {
this.setState({
page: page,
});
}
render(){
return (
<div>
<PDF
file={pdfurl}
onDocumentComplete={this.onDocumentComplete.bind(this)}
page={this.state.page}
/>
<Pagination onChange={this.onChange.bind(this)} total={this.state.pages} current={this.state.page}/>
</div>
)
}
}
const mapStateToProps = s => ({
})
export default connect(mapStateToProps,{
})( Test )分頁使用的是antd 然后發(fā)現(xiàn)antd的組件最多只有102頁em。。
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
React利用scheduler思想實現(xiàn)任務的打斷與恢復
這篇文章主要為大家詳細介紹了React如何利用scheduler思想實現(xiàn)任務的打斷與恢復,文中的示例代碼講解詳細,感興趣的小伙伴可以參考一下2024-03-03
react router4+redux實現(xiàn)路由權限控制的方法
本篇文章主要介紹了react router4+redux實現(xiàn)路由權限控制的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
react實現(xiàn)頭部導航,選中狀態(tài)底部出現(xiàn)藍色條塊問題
這篇文章主要介紹了react實現(xiàn)頭部導航,選中狀態(tài)底部出現(xiàn)藍色條塊問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11

