forked from Expensify/react-fast-pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
96 lines (82 loc) · 3.4 KB
/
App.tsx
File metadata and controls
96 lines (82 loc) · 3.4 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import React, {useState} from 'react';
import pdfWorkerSource from 'pdfjs-dist/build/pdf.worker.min.mjs';
import * as pdfjs from 'pdfjs-dist';
import ReactFastPDF, {PDFPreviewer} from 'react-fast-pdf';
import type {RotationDegrees} from 'react-fast-pdf';
import './index.css';
pdfjs.GlobalWorkerOptions.workerSrc = URL.createObjectURL(new Blob([pdfWorkerSource], {type: 'text/javascript'}));
function App() {
const [file, setFile] = useState<string | null>(null);
const [rotation, setRotation] = useState<RotationDegrees>(0);
// `.default` is required when referencing the legacy CJS package.
const packageName = ('default' in ReactFastPDF ? (ReactFastPDF.default as {PackageName: string}) : ReactFastPDF).PackageName;
const handleRotate = () => {
setRotation((prev) => ((prev + 90) % 360) as RotationDegrees);
};
return (
<main className="container">
<h1 className="title">Hello, I am {packageName}!</h1>
{file ? (
<>
<div style={{display: 'flex', gap: '10px', marginBottom: '10px', flexWrap: 'wrap'}}>
<button
className="button button_back"
type="button"
onClick={() => {
setFile(null);
setRotation(0);
}}
>
Back
</button>
<button
className="button"
type="button"
onClick={handleRotate}
>
Rotate 90° (Current: {rotation}°)
</button>
</div>
<PDFPreviewer
file={file}
pageMaxWidth={1000}
isSmallScreen={false}
rotation={rotation}
/>
</>
) : (
<>
<h3>Please choose a file for previewing:</h3>
<div className="buttons_container">
<button
className="button"
type="button"
onClick={() => setFile('example.pdf')}
>
example.pdf
</button>
<button
className="button"
type="button"
onClick={() => setFile('example_protected.pdf')}
>
example_protected.pdf (Password: 123456)
</button>
<input
className="button"
type="file"
onChange={(event) => {
const uploadedFile = event?.target?.files?.[0];
if (!uploadedFile) {
return;
}
setFile(URL.createObjectURL(uploadedFile));
}}
/>
</div>
</>
)}
</main>
);
}
export default App;