pdf.js not working with Safari

Yarin picture Yarin · Jun 10, 2013 · Viewed 17.1k times · Source

We're testing out pdf.js and while it seems like an awesome project we can't get it working in Safari.

(Tested on PDF.JS version = 0.8.229 (latest) / Safari 5.1.9 - 6.0.4 / Mac OSX 10.6.8 - 10.8.3)

EXAMPLE:

This is an example of the demo code served from our server with a sample PDF that works on Chrome/FFox but not Safari: http://test.appgrinders.com/pdf_js/test.html

Console output:

Warning: Setting up fake worker. 
Error: Invalid XRef stream (while reading XRef): 
Error: Invalid XRef stream pdf.js:850undefined 
Warning: Indexing all PDF objects 
Error: Invalid XRef stream (while reading XRef): 
Error: Invalid XRef stream pdf.js:850undefined

More tests:

The following is a list of sample PDFs we tested (They were all served from our server, and all worked in Chrome/FFox/Android). The only one that worked with Safari was the PDF file served from the pdf.js project itself:

FAILS IN SAFARI:
http://samplepdf.com/sample.pdf
http://forums.adobe.com/servlet/JiveServlet/previewBody/2041-102-1-2139/Sample.pdf
https://github.com/prawnpdf/prawn/raw/master/data/pdfs/form.pdf

WORKS IN SAFARI:
http://cdn.mozilla.net/pdfjs/helloworld.pdf
(NOTE: This is a sample PDF from the pdf.js project and the only one we ever got working)


We've submitted a bug report, but the developers do not seem to have an answer, so I'm hoping someone here might...

How can we get pdf.js working with Safari?

Answer

Yarin picture Yarin · Jun 15, 2013

I've figured out how to get things working on Mac Safari (without necessarily understanding why)...

  1. compatibility.js must be included.

  2. PDFJS.workerSrc must be assigned.

    The demo code I had been testing (from the JS Bin demos here) does not do this, though some other online examples do (including the hello world example and the examples provided by @AndrewBenjamin – thanks). Other browsers don't seem to require this, but Safari won't work without it.

    <script type="text/javascript" src="compatibility.js"></script>
    <script type="text/javascript" src="pdf.js"></script>
    
    <!-- NEED THIS for Safari Mac to render work -->
    <script type="text/javascript">
        // Specify the main script used to create a new PDF.JS web worker.  
        // In production, change this to point to the combined `pdf.js` file.  
        PDFJS.workerSrc = 'pdf.worker.js';  
    </script>
    

Again, can't explain why, but this is how we got it working for us.