Ng2-pdf-viewer: Cannot read property 'div' of undefined

Created on 13 Dec 2017  路  15Comments  路  Source: VadimDez/ng2-pdf-viewer

- [x] bug report -> please search issues before submitting
- [ ] feature request

When page changed it's success to change the page number, but It's failed when scrolling to the page because of this error :

ERROR TypeError: Cannot read property 'div' of undefined
    at PDFViewer._resetCurrentPageView (pdf_viewer.js:1556)
    at PDFViewer._setCurrentPageNumber (pdf_viewer.js:1281)
    at PDFViewer.set (pdf_viewer.js:1858)
    at eval (pdf-viewer.component.js:258)
    at ZoneDelegate.invokeTask (zone.js:425)
    at Object.onInvokeTask (core.js:4620)
    at ZoneDelegate.invokeTask (zone.js:424)
    at Zone.runTask (zone.js:192)
    at ZoneTask.invokeTask (zone.js:499)
    at ZoneTask.invoke (zone.js:488)

I'm using Angular 5.0.3 and ng2-pdf-viewer 3.0.2

stale

Most helpful comment

I had the same error and I fixed it with following changes.

  • ng2-pdf-viewer has a dependency called pdfjs-dist. You can find it in node_modules.
  • Open this file -> node_modules/pdfjs-dist/web/pdf_viewer.js
  • In pdf_viewer.js file, search for this line of code -> var elt = views[index].div; It is inside 'backtrackBeforeAllVisibleElements' function.
  • Change that code line to var elt = views[index-1].div; and save.

All 15 comments

Could you provide steps to reproduce this?

same here! any update?

I had the same problem. I fixed it by removing [stick-to-page]="true" from my html definition.
You should try to check your configuration.

I am also getting the error cannot read property of undefined when I pop a page which renders the PDF in ionic.

core.js:1449 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'div' of undefined
TypeError: Cannot read property 'div' of undefined
    at backtrackBeforeAllVisibleElements (pdf_viewer.js:558)
    at getVisibleElements (pdf_viewer.js:605)
    at PDFViewer._getVisiblePages (pdf_viewer.js:5400)
    at PDFViewer.forceRendering (pdf_viewer.js:5080)
    at PDFRenderingQueue.renderHighestPriority (pdf_viewer.js:4191)
    at continueRendering (pdf_viewer.js:4256)
    at t.invoke (polyfills.js:3)
    at Object.onInvoke (core.js:4760)
    at t.invoke (polyfills.js:3)
    at r.run (polyfills.js:3)
    at backtrackBeforeAllVisibleElements (pdf_viewer.js:558)
    at getVisibleElements (pdf_viewer.js:605)
    at PDFViewer._getVisiblePages (pdf_viewer.js:5400)
    at PDFViewer.forceRendering (pdf_viewer.js:5080)
    at PDFRenderingQueue.renderHighestPriority (pdf_viewer.js:4191)
    at continueRendering (pdf_viewer.js:4256)
    at t.invoke (polyfills.js:3)
    at Object.onInvoke (core.js:4760)
    at t.invoke (polyfills.js:3)
    at r.run (polyfills.js:3)
    at c (polyfills.js:3)
    at polyfills.js:3
    at t.invokeTask (polyfills.js:3)
    at Object.onInvokeTask (core.js:4751)
    at t.invokeTask (polyfills.js:3)
    at r.runTask (polyfills.js:3)
    at o (polyfills.js:3)

I am using Ionic 3.20.0 and ng2-pdf-viewer ^5.1.1

So I see this issue when I render the pdf viewer in a component and its a large pdf and as it is rendering (can see the first page but the rest are not rendered yet) and I decide to close it (removes the pdf-viewer from the page via a *ngIf) then it crashes and gives me that error

This looks to be the same as #367

I get this issue when i try to render the pdf viewer inside a bootstrap modal

I am getting the same error when I am trying to render pdf inside iframe.

I'm also having a hard time with this issue.
It seems like the error is thrown whenever the pdf-viewer is hidden or closed.
For example -

  1. Like @Durdush mentioned, when you use the pdf-viewer as a fallback to an