PDF.js how to create a rendered page?

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

This should happen when the document is opened, scrolled or scaled.

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

+3


source to share


1 answer


For explicit rendering:

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

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

      

What docs say:

Promise upon completion of the task.



Implicit rendering

Well, you can always override the page to provide the event yourself, not rendering on any prototype, so this gets really ugly. render

calls the return RenderTask

that has InternalRenderTask

, which has a callback. Thus, you can trick him by doing the following:

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 may break. There is no documented way to do this.

+2


source







All Articles