Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does PDF.js raise an event as I browse between pages?

Does PDF.js raise an event as I browse between pages?

Basically I want to update the URL and add to pushstate when I browse between pages in a PDF.js document.

Does PDF.js raise an event as I browse between pages, and if so what is the event called?

Thanks

like image 442
JMK Avatar asked Sep 18 '15 20:09

JMK


People also ask

How does PDF JS work?

PDF. js leverages Asynchronous JavaScript and XML (AJAX) to download the PDF file from a web server and parse its contents. Once prepared, content is then rendered onto an HTML5 <canvas> element using canvas drawing commands.

Does PDF JS work on Chrome?

What Browsers does PDF. js Support. PDF. js is used within Mozilla Firefox today as the built-in PDF viewer, and it works well within a website when viewed using the latest versions of Chrome and Firefox, whether through the pre-built PDF.

What is PDF JS library?

js is a JavaScript library that renders Portable Document Format (PDF) files using the web standards-compliant HTML5 Canvas. The project is led by the Mozilla Corporation after Andreas Gal launched it (initially as an experiment) in 2011.


3 Answers

Yeah, there is an event named pagechange.

You can use it like that:

document.addEventListener('pagechange', function(e) {
  if (e.pageNumber !== e.previousPageNumber) {
    console.log('page changed from ' + e.previousPageNumber + ' to ' + e.pageNumber);
  }
});

Open the viewer and drop the code above into the console, and see what happens!

like image 196
Buzinas Avatar answered Oct 03 '22 14:10

Buzinas


Now in v2.1.233 of pdf.js, the event is pagechanging.

like image 24
Lastrer Avatar answered Oct 03 '22 14:10

Lastrer


The following code worked for me -

window.PDFViewerApplication.eventBus.on('pagechanging', function pagechange(evt) { 
   console.log(evt)
});
like image 24
vishesh Avatar answered Oct 03 '22 14:10

vishesh