-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
70 lines (63 loc) · 1.92 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// mobile不支持 PDFViewer
// 解决方法 https://github.com/diegomura/react-pdf/issues/1113#issuecomment-781053667
import { useState } from 'react';
import { BlobProvider, PDFDownloadLink } from '@react-pdf/renderer';
import { Document, Page } from 'react-pdf/dist/esm/entry.webpack5';
import PDFDocument from './components/PdfDocument';
import './App.css';
function App() {
const [totalPageNumber, settotalPageNumber] = useState(1);
const [pageNumber, setPageNumber] = useState(1);
const handleLoadSuccess = (pdf) => {
settotalPageNumber(pdf.numPages);
}
// 上一页
const goPrevPage = () => {
if (pageNumber > 1) {
setPageNumber(pageNumber - 1);
}
}
// 下一页
const goNextPage = () => {
if (pageNumber < totalPageNumber) {
setPageNumber(pageNumber + 1);
}
}
return (
<div className="App">
{/* 工具栏 */}
<div className='toolbar'>
<button onClick={goPrevPage}>上一页</button>
<button onClick={goNextPage}>下一页</button>
</div>
{/* PDF预览 */}
<div className="pdf-view-container" id="pdf-view-container">
<BlobProvider document={<PDFDocument />}>
{({ blob, url, loading }) => {
return loading ? 'loading' : (
<Document
file={url}
onLoadSuccess={(pdf) => handleLoadSuccess(pdf)}
renderMode="canvas">
<Page
pageNumber={pageNumber}
width={window.innerWidth - 20} />
</Document>
)
}}
</BlobProvider>
</div>
{/* 下载 */}
<div className='download-container'>
<PDFDownloadLink
style={{ color: "#fff" }}
document={<PDFDocument />}>
{({ loading }) =>
loading ? 'Loading...' : '下载该pdf'
}
</PDFDownloadLink>
</div>
</div>
);
}
export default App;