0
<html>
<body>
  <div>
    <button id="prev" onclick="goPrevious()">Previous</button>
    <button id="next" onclick="goNext()">Next</button>
    &nbsp; &nbsp;
    <span>Page: <span id="page_numo"></span> / <span id="page_count"></span></span>
    <input type=text id="elNum" value=1>

  </div>

  <div>
    <canvas id="the-canvas" style="border:0px solid black"></canvas>
  </div>

  <!-- Use latest PDF.js build from Github -->
  <script type="text/javascript" src="https://raw.github.com/mozilla/pdf.js/gh-pages/build/pdf.js"></script>

  <script type="text/javascript">
    //
    // NOTE: 
    // Modifying the URL below to another server will likely *NOT* work. Because of browser
    // security restrictions, we have to use a file server with special headers
    // (CORS) - most servers don't support cross-origin browser requests.
    //
    var url = 'http://cdn.mozilla.net/pdfjs/tracemonkey.pdf';

    //
    // Disable workers to avoid yet another cross-origin issue (workers need the URL of
    // the script to be loaded, and currently do not allow cross-origin scripts)
    //
    PDFJS.disableWorker = true;

    var pdfDoc = null,
        pageNum = 1,
        scale = 0.8,
        canvas = document.getElementById('the-canvas'),
        ctx = canvas.getContext('2d');





    //
    // Get page info from document, resize canvas accordingly, and render page
    //
    function renderPage(num) {
      // Using promise to fetch the page
      pdfDoc.getPage(num).then(function(page) {
        var viewport = page.getViewport(scale);
        canvas.height = viewport.height;
        canvas.width = viewport.width;

        // Render PDF page into canvas context
        var renderContext = {
          canvasContext: ctx,
          viewport: viewport
        };
        page.render(renderContext);
      });

      // Update page counters
      document.getElementById('page_numo').textContent = pageNum;
      document.getElementById('page_count').textContent = pdfDoc.numPages;
      sic = document.getElementById('elNum');
      sic.value = pageNum;
    }

   crc = 1;
    //



    // Go to previous page
    //

    function goPrevious() {
      if (pageNum <= 1)
        return;
      pageNum--;
      crc = pageNum;      
      renderPage(pageNum);
    }

    //
    // Go to next page
    //
    function goNext() {
      if (pageNum >= pdfDoc.numPages)
        return;
      pageNum++;
      crc = pageNum;
      renderPage(pageNum);
    }

    //
    // Asynchronously download PDF as an ArrayBuffer
    //
    PDFJS.getDocument(url).then(function getPdfHelloWorld(_pdfDoc) {
      pdfDoc = _pdfDoc;
      renderPage(pageNum);
    });
   document.write ('Current page is '+crc);
    </script>

</body>
</html>

That's the code. It's the simplest version of an online pdf renderer (pdf.js). I've been working on a project which needs to pass the pageNum (The current pdf page number) variable via url to another script for process. But the problem is I haven't been by no way able to retrieve pageNum value outside the renderPage function. I think it's matter of Javascript Variable Scopes but cannot see what's wrong with my attempt to extract via the crc variable or via document.getElementById('elNum').value... (See the document.write ('Current page is '+crc); line at the end of the script and the field in 8th line to see how I have been trying to retrieve the value).

Anybody can give me any clues about what am I doing wrong? Test page Here (http://jsbin.com/pdfjs-prevnext-v2/2091/edit)

Note: I have seen other questions on this topic but this seems to be a weird case. Really need some help and hopefully someone else will need it in the future.

2 Answers 2

0

document.write ('Current page is '+crc) executed only when the page is loaded. crc-is a global variable and can be used.

    var viewCurrPage = function() {
        console.log('Current page is '+crc);
    };

    function goPrevious() {
          if (pageNum <= 1)
            return;
          pageNum--;
          crc = pageNum;      
          renderPage(pageNum);
          viewCurrPage();
        };

function goNext() {
      if (pageNum >= pdfDoc.numPages)
        return;
      pageNum++;
      crc = pageNum;
      renderPage(pageNum);
      viewCurrPage();
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, thanks for your answer, that's great! But i can't figure out how to pass variable crc via url to another page for process, for instance: document.write('<a href=\'process.php?page='+crc+'\'>Page to process is '+crc+'</a>'); Any ideas?
document.write - not the best variant for this. You need to create a function that changes the global var crc. After you need Dynamically Create Link Javascript see here for an example stackoverflow.com/questions/9831074/…
0

This, combined with the simple function by vn_grv could be useful

var viewCurrPage = function() {
        //console.log('Current page is '+crc);
      document.getElementById('one').innerHTML = '<a href="process.php?page='+crc+'">Process page '+crc+'</a>';
    };
//Simply adding the corresponding div like: 
<div id="one"></div>

Thanks for your answer. Best regards,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.