0

Trying to auto-search with pdfjs, (and having hard time with the docs..)

THIS WORKS:

<input type="button" value="click-me" onclick="searchPDF('cpu');" />

THIS DOESN'T (jquery running ok)

 $(document).ready(function() {
    searchPDF('cpu');
 });

HOW DO SUSPEND EXECUTION UNTIL THE PDF IS PARSED? (OR WHY ISN'T IT WORKING)

MODIFIED viewer.html (thanks to PDF.js - Using search function on embedded PDF )

// search with PDF.js
function searchPDF(td_text) {
  //PDFView.findBar.open();
  $('#findInput').val(td_text);
  $("#tableDiv").focus();
  PDFView.findBar.findNextButton.click();

var event = document.createEvent('CustomEvent');

    event.initCustomEvent('find', true, true, {
        query: td_text,
        caseSensitive: $("#findMatchCase").prop('checked'),
        highlightAll: $("#findHighlightAll").prop('checked'),
        findPrevious: undefined
    });
    return event;
Community
  • 1
  • 1
Teson
  • 6,644
  • 8
  • 46
  • 69
  • Check this out: [Mozilla](https://github.com/mozilla/pdf.js/issues/5601) or this [Stack](http://stackoverflow.com/questions/12693207/how-to-know-if-pdf-js-has-finished-rendering) – prizm1 Aug 16 '16 at 13:59

1 Answers1

1

Finally found a solution for us that doesn't digest javascript for breakfast:

(thanks to

Based on viewer.html and added jquery:

$(document).ready(function() {
  document.addEventListener('textlayerrendered', function (e) {
    if (e.detail.pageNumber === PDFView.page) {
      // finished rendering, call a JS-function..
      searchPDF('cpu');
    }
  }, true);
});

function searchPDF(str) {
  alert('working');
  ..add code from 
  http://stackoverflow.com/questions/38976490/how-do-i-get-javascript-to-run-when-pdfjs-is-loaded 
}
Teson
  • 6,644
  • 8
  • 46
  • 69