Pdf.js: How to wait for page rendered?

Created on 13 Oct 2016  路  11Comments  路  Source: mozilla/pdf.js

Hey,

how to check if pdf.js is currently rendering or finished rendering?

Im using the viewer example as a foundation. In my external code I zoom and want to wait for pdf.js finished rendering before executing more of my code.

Thanks for any answers!

All 11 comments

You can listen for the pagerendered event. In general there is no way to know that the entire document is rendered because it may load incrementally (for example, based on range requests or in the viewer when you scroll a page into view).

Problem is that this is too general. It gets called a few times (for every page rendered?). But i don't know when the viewer is actually rendering. Perfect would be a flag var rendering:boolean that i can access.

@MickL. As it was said, the demo viewer never renders all pages (only visible) and re-renders some pages when zoom changes. Looks like you have to write specialized viewer that will render all pages, so you can run your code. See how to wait on page to be rendered with RenderTask.promise at https://github.com/mozilla/pdf.js/blob/master/examples/learning/prevnext.html

Sorry if it wasnt clear. I know it only renders the visible pages. But i need to know when it finished this rendering so i can continue. When "pagerendered" is called twice, how do i know the second is the final one? It would be perfect if there is some variable or event like "pdf js is currently working" or "finished all needed page-renderings"

It hard to tell without seeing the example: 1) "pagerendered" could be called for different page (e.g. after rendering page 2 then after page 1 -- they both are visible but page 2 had less content); 2) zoom/scale parameter changed e.g. due to window.resize; 3) bug in the viewer.

Is there any startrendering event?
I could catch every start and every end so i would know when there is no rendering active.

To clearify: I just want to know if the pdf.js is currently rendering. If yes i would wait. If no I can continue. Its just about the JavaScript-performance which isnt the greatest while pdf.js is rendering so i want to wait before it is finished.

There is renderingState for every page view, see https://github.com/mozilla/pdf.js/blob/8c5b9255471d7c2f472e477592fca4a50f2a5962/web/pdf_page_view.js#L96 . You can query state for every page view in the document?

Ok thanks this is kind of what i was looking for. It may not be a good idea to check every page when having a 200 page document. But the surrounding 2 may be fine.

It may look something like this:

if(PDFViewerApplication.pdfViewer._pages[currentPage].renderingState == 3) {
    // continue
} else聽{
   // setTimeout iteration
}

Not the best solution but it would work. A global variable or promise from pdf.js saying "hey im currently doing some hard work" and "im currently doing nothing" would be more nicely :)

There is renderingState for every page view, see

https://github.com/mozilla/pdf.js/blob/8c5b9255471d7c2f472e477592fca4a50f2a5962/web/pdf_page_view.js#L96

. You can query state for every page view in the document?

There is renderingState for every page view, see

https://github.com/mozilla/pdf.js/blob/8c5b9255471d7c2f472e477592fca4a50f2a5962/web/pdf_page_view.js#L96

. You can query state for every page view in the document?

thanks for mentioning renderingStates property , it really solved the hurdle

var RenderingStates = {
  INITIAL: 0,
  RUNNING: 1,
  PAUSED: 2,
  FINISHED: 3
};
Was this page helpful?
0 / 5 - 0 ratings

Related issues

xingxiaoyiyio picture xingxiaoyiyio  路  3Comments

PeterNerlich picture PeterNerlich  路  3Comments

THausherr picture THausherr  路  3Comments

dmisdm picture dmisdm  路  3Comments

azetutu picture azetutu  路  4Comments