DefinitelyTyped/types/react-pdf/react-pdf-tests.tsx
Yuji Matsumoto e96ddce698
🤖 Merge PR #47751 [react-pdf] Add missing properties in PDFPageProxy interface by @queq1890
* Extend PDFPageProxy type

* Fix prettier error
2020-09-19 18:32:49 -07:00

56 lines
1.6 KiB
TypeScript

import * as React from 'react';
import { Document, Page, pdfjs } from 'react-pdf';
import { PDFPageProxy } from 'react-pdf/dist/Page';
import { PDFDocumentProxy } from 'pdfjs-dist';
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;
interface State {
numPages: number | null;
pageNumber: number;
originalHeight: number | null;
originalWidth: number | null;
}
export class MyApp extends React.Component<{}, State> {
constructor(props: {}) {
super(props);
this.state = {
numPages: null,
pageNumber: 1,
originalHeight: null,
originalWidth: null,
};
}
onDocumentLoadSuccess = ({ numPages }: PDFDocumentProxy) => {
this.setState({ numPages });
}
onPageLoadSuccess = ({ originalHeight, originalWidth }: PDFPageProxy) => {
this.setState({
originalHeight,
originalWidth,
});
}
render() {
const { pageNumber, numPages, originalHeight, originalWidth } = this.state;
return (
<div>
<Document file="somefile.pdf" onLoadSuccess={this.onDocumentLoadSuccess}>
<Page pageNumber={pageNumber} onLoadSuccess={this.onPageLoadSuccess} />
</Document>
<p>
Page {pageNumber} of {numPages}
</p>
<p>
PDF height: {originalHeight}
PDF width: {originalWidth}
</p>
</div>
);
}
}