@@ -213,6 +213,7 @@ export default {
213213 boundHandleWheel: null ,
214214 debouncedApplyZoom: null ,
215215 visualScale: this .initialScale ,
216+ resizeObserver: null ,
216217 }
217218 },
218219 mounted () {
@@ -230,6 +231,14 @@ export default {
230231 this .$el ? .addEventListener (' wheel' , this .boundHandleWheel , { passive: false })
231232 if (this .autoFitZoom ) {
232233 window .addEventListener (' resize' , this .adjustZoomToFit )
234+ if (typeof ResizeObserver !== ' undefined' ) {
235+ this .resizeObserver = new ResizeObserver (() => {
236+ this .scheduleAutoFitZoom ()
237+ })
238+ if (this .$el ) {
239+ this .resizeObserver .observe (this .$el )
240+ }
241+ }
233242 }
234243 },
235244 beforeUnmount () {
@@ -250,6 +259,10 @@ export default {
250259 this .$el ? .removeEventListener (' scroll' , this .onViewportScroll )
251260 if (this .autoFitZoom ) {
252261 window .removeEventListener (' resize' , this .adjustZoomToFit )
262+ if (this .resizeObserver ) {
263+ this .resizeObserver .disconnect ()
264+ this .resizeObserver = null
265+ }
253266 }
254267 if (this .viewportRafId ) {
255268 window .cancelAnimationFrame (this .viewportRafId )
@@ -274,8 +287,16 @@ export default {
274287 }
275288
276289 const pages = []
290+ const pageWidths = Array (pdfDoc .numPages ).fill (0 )
277291 for (let p = 1 ; p <= pdfDoc .numPages ; p++ ) {
278- pages .push (pdfDoc .getPage (p))
292+ const pagePromise = pdfDoc .getPage (p)
293+ pagePromise .then ((page ) => {
294+ pageWidths[p - 1 ] = page .getViewport ({ scale: 1 }).width
295+ if (this .autoFitZoom ) {
296+ this .scheduleAutoFitZoom ()
297+ }
298+ })
299+ pages .push (pagePromise)
279300 }
280301
281302 docs .push ({
@@ -284,6 +305,7 @@ export default {
284305 pdfDoc,
285306 numPages: pdfDoc .numPages ,
286307 pages,
308+ pageWidths,
287309 pagesScale: Array (pdfDoc .numPages ).fill (this .scale ),
288310 allObjects: Array (pdfDoc .numPages ).fill (0 ).map (() => []),
289311 })
@@ -296,7 +318,7 @@ export default {
296318 this .$emit (' pdf-elements:end-init' , { docsCount: docs .length })
297319 this .$nextTick (() => {
298320 if (this .autoFitZoom ) {
299- this .adjustZoomToFit ()
321+ this .scheduleAutoFitZoom ()
300322 }
301323 })
302324 }
@@ -798,6 +820,9 @@ export default {
798820 if (docIndex < 0 || docIndex >= this .pdfDocuments .length ) return
799821 this .pdfDocuments [docIndex].pagesScale [pageIndex] = e .scale
800822 this .cachePageBounds ()
823+ if (this .autoFitZoom ) {
824+ this .scheduleAutoFitZoom ()
825+ }
801826 },
802827
803828 formatPageNumber (currentPage , totalPages ) {
@@ -826,15 +851,34 @@ export default {
826851 const availableWidth = containerWidth - 40
827852 return Math .max (0.1 , Math .min (2 , availableWidth / maxPageWidth))
828853 },
854+ scheduleAutoFitZoom () {
855+ if (this .zoomRafId ) return
856+ this .zoomRafId = window .requestAnimationFrame (() => {
857+ this .zoomRafId = 0
858+ this .adjustZoomToFit ()
859+ })
860+ },
829861 adjustZoomToFit () {
830862 if (! this .autoFitZoom || ! this .pdfDocuments .length ) return
831863
832- const canvases = this .$el ? .querySelectorAll (' canvas' )
833- if (! canvases? .length ) return
864+ const widths = this .pdfDocuments
865+ .flatMap (doc => doc .pageWidths || [])
866+ .filter (width => width > 0 )
834867
835- const maxCanvasWidth = Math .max (... Array .from (canvases).map (canvas =>
836- canvas .width / (this .scale || 1 ),
837- ))
868+ let maxCanvasWidth = 0
869+ if (widths .length ) {
870+ maxCanvasWidth = Math .max (... widths)
871+ } else {
872+ if (this .autoFitZoom ) {
873+ this .scheduleAutoFitZoom ()
874+ return
875+ }
876+ const canvases = this .$el ? .querySelectorAll (' canvas' )
877+ if (! canvases? .length ) return
878+ maxCanvasWidth = Math .max (... Array .from (canvases).map (canvas =>
879+ canvas .width / (this .scale || 1 ),
880+ ))
881+ }
838882
839883 const optimalScale = this .calculateOptimalScale (maxCanvasWidth)
840884 if (Math .abs (optimalScale - this .scale ) > 0.01 ) {
0 commit comments