@@ -12,6 +12,7 @@ import {
1212 DEFAULT_HEIGHT ,
1313 DEFAULT_SHOULD_GROW_WHEN_SCROLLING ,
1414 MAIN_CONTENT_ID ,
15+ READER_MARGIN ,
1516} from '../constants' ;
1617import LoadingSkeleton from '../ui/LoadingSkeleton' ;
1718import { fetchAsUint8Array , getResourceUrl , SCALE_STEP } from './lib' ;
@@ -128,48 +129,49 @@ export default function usePdfReader(args: PdfReaderArguments): ReaderReturn {
128129 } , [ state . resourceIndex , manifest , proxyUrl , getContent ] ) ;
129130
130131 /**
131- * calculate the height or width of the pdf page in paginated mode .
132+ * calculate the height or width of the pdf page to fit to dimensions .
132133 * - if the page's aspect ratio is taller than the container's, we will constrain
133134 * the page to the height of the container.
134135 * - if the page's aspect ratio is wider than the container's, we will constrain
135136 * the page to the width of the container
136137 */
137138 const resizePage = React . useCallback (
138- ( containerSize : { width : number ; height : number } , fitMode : FitMode ) => {
139+ (
140+ containerSize : { width : number ; height : number } ,
141+ fitMode : FitMode ,
142+ scale : number
143+ ) => {
139144 let width , height ;
140145 if ( fitMode === 'width' ) {
141- width = Math . round ( containerSize . width ) ;
146+ width = Math . round ( ( containerSize . width - READER_MARGIN ) * scale ) ;
142147 height = undefined ;
148+ } else if ( fitMode === 'height' && state . pdfWidth && state . pdfHeight ) {
149+ const aspectRatio = state . pdfHeight / state . pdfWidth ;
150+ width = Math . round (
151+ ( ( containerSize . height - READER_MARGIN ) / aspectRatio ) * scale
152+ ) ;
153+ height = Math . round ( ( containerSize . height - READER_MARGIN ) * scale ) ;
143154 } else {
144155 width = undefined ;
145- height = Math . round ( containerSize . height ) ;
156+ height = undefined ;
146157 }
147158 dispatch ( { type : 'RESIZE_PAGE' , width, height } ) ;
148159 } ,
149- [ ]
160+ [ state . pdfWidth , state . pdfHeight ]
150161 ) ;
151162
152163 React . useEffect ( ( ) => {
153- resizePage ( containerSize , state . fitMode ) ;
154- } , [ containerSize , resizePage , state . fitMode ] ) ;
164+ resizePage ( containerSize , state . fitMode , state . scale ) ;
165+ } , [ containerSize , resizePage , state . fitMode , state . scale ] ) ;
155166
156- const setInitialPageHeight = React . useCallback ( ( ) => {
157- if (
158- pageHeight === 0 &&
159- state . pdfWidth &&
160- state . pdfHeight &&
161- containerSize . width
162- ) {
163- const margin = 16 ;
167+ React . useEffect ( ( ) => {
168+ if ( state . pdfWidth && state . pdfHeight && pageHeight === 0 ) {
164169 const aspectRatio = state . pdfHeight / state . pdfWidth ;
165- const initialPageHeight = ( containerSize . width - margin ) * aspectRatio ;
166- setPageHeight ( initialPageHeight ) ;
170+ const initialPageHeight =
171+ ( containerSize . width - READER_MARGIN ) * aspectRatio ;
172+ setPageHeight ( Math . round ( initialPageHeight ) ) ;
167173 }
168- } , [ pageHeight , state . pdfWidth , state . pdfHeight , containerSize . width ] ) ;
169-
170- React . useEffect ( ( ) => {
171- setInitialPageHeight ( ) ;
172- } , [ setInitialPageHeight ] ) ;
174+ } , [ state . pdfWidth , state . pdfHeight , containerSize . width , pageHeight ] ) ;
173175
174176 /**
175177 * Update the atStart/atEnd state to tell the UI whether to show the prev/next buttons
@@ -376,7 +378,7 @@ export default function usePdfReader(args: PdfReaderArguments): ReaderReturn {
376378 width : Math . round ( page . width ) ,
377379 } ) ;
378380
379- resizePage ( containerSize , state . fitMode ) ;
381+ resizePage ( containerSize , state . fitMode , state . scale ) ;
380382 }
381383 }
382384
@@ -401,6 +403,11 @@ export default function usePdfReader(args: PdfReaderArguments): ReaderReturn {
401403 overflowX : 'hidden' ,
402404 overflowY : 'auto' ,
403405 } ,
406+ '.react-pdf__Page' : {
407+ width : `${
408+ containerSize . width ? containerSize . width - READER_MARGIN : 0
409+ } px`,
410+ } ,
404411 } }
405412 >
406413 < Document
@@ -414,7 +421,7 @@ export default function usePdfReader(args: PdfReaderArguments): ReaderReturn {
414421 Array . from ( new Array ( state . numPages ) , ( _ , index ) => (
415422 < ScrollPage
416423 key = { `page_${ index + 1 } ` }
417- width = { containerSize . width - 16 }
424+ width = { state . pageWidth }
418425 height = { state . pageHeight }
419426 placeholderHeight = { state . pdfHeight }
420427 placeholderWidth = { state . pdfWidth }
0 commit comments