11import pdfWorkerSource from 'pdfjs-dist/build/pdf.worker.min.mjs' ;
2- import React , { memo , useCallback , useLayoutEffect , useRef , useState } from 'react' ;
3- import type { CSSProperties , ReactNode } from 'react' ;
4- import times from 'lodash/times.js ' ;
2+ import React , { useCallback , useLayoutEffect , useRef , useState } from 'react' ;
3+ import type { CSSProperties , ReactNode , JSX } from 'react' ;
4+ import { times } from 'lodash' ;
55import { VariableSizeList as List } from 'react-window' ;
66import { Document , pdfjs } from 'react-pdf' ;
77import 'react-pdf/dist/Page/AnnotationLayer.css' ;
88import 'react-pdf/dist/Page/TextLayer.css' ;
99
10- import type { PDFDocument , PageViewport } from './types.js' ;
10+ import type { PDFDocument , PageViewport , RotationDegrees } from './types.js' ;
1111import { pdfPreviewerStyles as styles } from './styles.js' ;
1212import PDFPasswordForm , { type PDFPasswordFormProps } from './PDFPasswordForm.js' ;
1313import PageRenderer from './PageRenderer.js' ;
@@ -28,6 +28,8 @@ type Props = {
2828 onLoadError ?: ( ) => void ;
2929 containerStyle ?: CSSProperties ;
3030 contentContainerStyle ?: CSSProperties ;
31+ /** Rotation angle for all pages (0, 90, 180, 270 degrees) */
32+ rotation ?: RotationDegrees ;
3133} ;
3234
3335type OnPasswordCallback = ( password : string | null ) => void ;
@@ -51,7 +53,8 @@ function PDFPreviewer({
5153 contentContainerStyle,
5254 shouldShowErrorComponent = true ,
5355 onLoadError,
54- } : Props ) {
56+ rotation = 0 ,
57+ } : Props ) : JSX . Element {
5558 const [ pageViewports , setPageViewports ] = useState < PageViewport [ ] > ( [ ] ) ;
5659 const [ numPages , setNumPages ] = useState ( 0 ) ;
5760 const [ containerWidth , setContainerWidth ] = useState ( 0 ) ;
@@ -103,6 +106,7 @@ function PDFPreviewer({
103106 * Calculates a proper page height. The method should be called only when there are page viewports.
104107 * It is based on a ratio between the specific page viewport width and provided page width.
105108 * Also, the app should take into account the page borders.
109+ * When rotation is 90 or 270 degrees, width and height are swapped.
106110 */
107111 const calculatePageHeight = useCallback (
108112 ( pageIndex : number ) => {
@@ -112,12 +116,18 @@ function PDFPreviewer({
112116
113117 const pageWidth = calculatePageWidth ( ) ;
114118
115- const { width : pageViewportWidth , height : pageViewportHeight } = pageViewports [ pageIndex ] ;
119+ const { width : originalWidth , height : originalHeight } = pageViewports [ pageIndex ] ;
120+
121+ // Swap dimensions when rotated 90 or 270 degrees
122+ const isRotated90or270 = rotation === 90 || rotation === 270 ;
123+ const pageViewportWidth = isRotated90or270 ? originalHeight : originalWidth ;
124+ const pageViewportHeight = isRotated90or270 ? originalWidth : originalHeight ;
125+
116126 const scale = pageWidth / pageViewportWidth ;
117127
118128 return pageViewportHeight * scale + PAGE_BORDER * 2 ;
119129 } ,
120- [ pageViewports , calculatePageWidth ] ,
130+ [ pageViewports , calculatePageWidth , rotation ] ,
121131 ) ;
122132
123133 const estimatedPageHeight = calculatePageHeight ( 0 ) ;
@@ -207,7 +217,7 @@ function PDFPreviewer({
207217 if ( containerWidth > 0 && containerHeight > 0 ) {
208218 listRef . current ?. resetAfterIndex ( 0 ) ;
209219 }
210- } , [ containerWidth , containerHeight ] ) ;
220+ } , [ containerWidth , containerHeight , rotation ] ) ;
211221
212222 useLayoutEffect ( ( ) => {
213223 if ( ! containerRef . current ) {
@@ -238,6 +248,7 @@ function PDFPreviewer({
238248 error = { shouldShowErrorComponent ? ErrorComponent : null }
239249 onLoadError = { onLoadError }
240250 loading = { LoadingComponent }
251+ rotate = { rotation }
241252 onLoadSuccess = { onDocumentLoadSuccess }
242253 onPassword = { initiatePasswordChallenge }
243254 >
@@ -264,6 +275,6 @@ function PDFPreviewer({
264275 ) ;
265276}
266277
267- PDFPasswordForm . displayName = 'PDFPreviewer' ;
278+ PDFPreviewer . displayName = 'PDFPreviewer' ;
268279
269- export default memo ( PDFPreviewer ) ;
280+ export default PDFPreviewer ;
0 commit comments