Reputation: 2003
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
Reputation: 276596
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.
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