sollniss
sollniss

Reputation: 2003

PDF.js how to create a page rendered event?

I am using PDF.js with the default viewer and need to detect every time a page is rendered.

This should happen when the document is opended, scrolled or zoomed.

As far as I know I can only use promises when I myself render a page explicitly, but is there a callback or something to listen to whenever a page is rendered? Actually getting the page (as int would be enough) is also very important.

Upvotes: 3

Views: 3707

Answers (1)

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276596

For explicit rendering:

Well, I checked the source and it seems that if you render the page explicitly you can use:

page.render(content).promise.then(function(){
  alert("done");
});

Which the docs state:

Promise for rendering task completion.

Implicit rendering

Well, you can always override page to provide an event yourself, render is not on any prototype so this gets really ugly. render calls return a RenderTask which has an InternalRenderTask which has a callback. So you can trick it by doing this:

var renderTask = page.render(...); // make any page render anything anywhere
var internal = renderTask.__proto__;
var oldNext = internal._next;

internal._next = function(){
     if((this.operatorListIdx === this.operatorList.argsArray.length) &&
         this.operatorList.lastChunk){
         anyPageRendered(this.pageNumber);             
     }
     // decorate
     return oldNext.apply(this, arguments);
};

function anyPageRendered(pageNumber){
    // your code here
    alert("Rendered " + pageNumber); 
}

This is undocumented and can break. There is no documented way to do this.

Upvotes: 2

Related Questions