Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pdf.js with local pdf file

Tags:

I'm trying out the pdf.js library and just want to display a local pdf file on my server instead of the pdf file provided by the example.

<html>
<body>
  <canvas id="the-canvas" style="border:1px solid black"></canvas>

  <!-- 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 = '/test.pdf';

    //
    // Disable workers to avoid yet another cross-origin issue (workers need the URL of
    // the script to be loaded, and dynamically loading a cross-origin script does
    // not work)
    //
    PDFJS.disableWorker = true;

    //
    // Asynchronous download PDF as an ArrayBuffer
    //
    PDFJS.getDocument(url).then(function getPdfHelloWorld(pdf) {
      //
      // Fetch the first page
      //
      pdf.getPage(1).then(function getPageHelloWorld(page) {
        var scale = 1.5;
        var viewport = page.getViewport(scale);

        //
        // Prepare canvas using PDF page dimensions
        //
        var canvas = document.getElementById('the-canvas');
        var context = canvas.getContext('2d');
        canvas.height = viewport.height;
        canvas.width = viewport.width;

        //
        // Render PDF page into canvas context
        //
        page.render({canvasContext: context, viewport: viewport});
      });
    });
  </script>

</body>
</html>

So I changed there pdf url to my local '/test.pdf' url. However this gives me the message it cannot find the file when it's clearly there in my root folder. Any idea what could cause this error?

like image 993
arnoutaertgeerts Avatar asked Mar 20 '13 20:03

arnoutaertgeerts


People also ask

How do I embed a local PDF in HTML?

The easiest way to put PDF in an HTML document is using the <a> tag with its href attribute. You need to add the URL or the reference link of your PDF file to the element.

What is a local PDF?

Local PDF uses Webassembly to edit your PDFs inside your Browser. Your files won't leave your System, they will not be sent to another server.


1 Answers

modify pdfjs/web/viewer.js file and change the

var DEFAULT_URL = '<file path on your server>'

This worked when I tried to implement the demo at http://mozilla.github.com/pdf.js/web/viewer.html on my local system

like image 66
Akash Avatar answered Jan 03 '23 01:01

Akash