| 42 | } |
| 43 | |
| 44 | export default class PDFExample extends React.Component< |
| 45 | Record<string, never>, |
| 46 | PDFExampleState |
| 47 | > { |
| 48 | private pdf: any; // usare `Pdf | null` se le typings del pacchetto esportano il tipo |
| 49 | |
| 50 | constructor(props: Record<string, never>) { |
| 51 | super(props); |
| 52 | this.state = { |
| 53 | page: 1, |
| 54 | scale: 1, |
| 55 | numberOfPages: 0, |
| 56 | horizontal: false, |
| 57 | showsHorizontalScrollIndicator: true, |
| 58 | showsVerticalScrollIndicator: true, |
| 59 | width: WIN_WIDTH, |
| 60 | }; |
| 61 | this.pdf = null; |
| 62 | } |
| 63 | |
| 64 | _onOrientationDidChange = (orientation: OrientationType): void => { |
| 65 | if (orientation === 'LANDSCAPE-LEFT' || orientation === 'LANDSCAPE-RIGHT') { |
| 66 | this.setState({ |
| 67 | width: WIN_HEIGHT > WIN_WIDTH ? WIN_HEIGHT : WIN_WIDTH, |
| 68 | horizontal: true, |
| 69 | }); |
| 70 | } else { |
| 71 | this.setState({ |
| 72 | width: WIN_HEIGHT > WIN_WIDTH ? WIN_HEIGHT : WIN_WIDTH, |
| 73 | horizontal: false, |
| 74 | }); |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | componentDidMount(): void { |
| 79 | Orientation.addOrientationListener(this._onOrientationDidChange); |
| 80 | |
| 81 | (async () => { |
| 82 | const url = 'https://www.africau.edu/images/default/sample.pdf'; |
| 83 | // handling blobs larger than 64 KB on Android requires patching React Native (https://github.com/facebook/react-native/pull/31789) |
| 84 | const result = await fetch(url); |
| 85 | const blob = await result.blob(); |
| 86 | const objectURL = URL.createObjectURL(blob); |
| 87 | this.setState({ ...this.state, objectURL, blob }); // keep blob in state so it doesn't get garbage-collected |
| 88 | })(); |
| 89 | } |
| 90 | |
| 91 | componentWillUnmount(): void { |
| 92 | Orientation.removeOrientationListener(this._onOrientationDidChange); |
| 93 | } |
| 94 | |
| 95 | prePage = (): void => { |
| 96 | const prePage = this.state.page > 1 ? this.state.page - 1 : 1; |
| 97 | this.pdf?.setPage(prePage); |
| 98 | console.log(`prePage: ${prePage}`); |
| 99 | }; |
| 100 | |
| 101 | nextPage = (): void => { |