Pdf.js does not work with Safari

We are testing pdf.js , and although this seems like a terrific project, we cannot get it to work in Safari.

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

Example:

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

Console exit:

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 

Additional tests:

Below is a list of samples that we tested (they were all submitted from our server, and they all worked in Chrome / FFox / Android). The only one who worked with Safari was the PDF file that was sent from the pdf.js project itself:

FAULTS 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 PDF sample from the pdf.js project and the only one we have ever worked with)


We sent a bug report, but the developers do not seem to have an answer, so I hope someone can do this ...

How can we make pdf.js work with Safari?

+4
source share
5 answers

I figured out how to get everything working on Mac Safari (not necessarily understanding why) ...

  • compatibility.js must be enabled.

  • PDFJS.workerSrc .

    The demo code I tested (from JS Bin demos here ) does not do this, although some other online examples (including the welcome world example and examples provided by @AndrewBenjamin - thanks). Other browsers do not seem to require this, but Safari will not 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, I can’t explain why, but this is how we earned for us.

+7
source

I have a PDF.js file that works fine in Safari on my local server, but when I put it on a remote server, a goofy error is returned:

Warning: setting up a fake worker.
Unhandled rejection: error: INVALID_STATE_ERR: DOM 11 exception

It will also show this error on my local computer if I manage to open the developer console. Close the console, display the PDF in Safari; open the console and it no longer works.

The question arises: how are the developer tools and the remote server different from working on the local server? Is this still a range checking problem?

I have PDF.js to work! I have changed so many things, I don’t know which part I worked in. Here is a list of the things I did.

  • Added compatibility.js - changed the last function in it like this:

     (function checkRangeRequests() { var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0; if (!isSafari) { return; } document.addEventListener('DOMContentLoaded', function (e) { if (isSafari) { PDFJS.disableRange = true; } }); })(); 
  • The order of xhr.open() calls in pdf.js , so xhr.setRequestHeader() happens after xhr.open()

  • Removed all lines of 'use strict';

  • Added xhr.setRequestHeader("Cache-control", "no-cache"); after xhr.open on lines 37272 and 41262

  • Minified pdf.js and it all works all the time!

+3
source

PDFJS.getDocument () will accept either a string connection or Uint8Array. In my client version, I pass PDFJS.getDocument () to Uint8Array, but if I need an existing file, I just pass the file path:

jspdf line 965:

 PDFJS.getDocument('..files/pdf/sample.pdf').then(function(pdf) { 

I do not know what makes your safari browser unsuccessful, but if you can see sample.pdf on my test site , you need to be very close to solving this.

0
source

I figured out how to get Mac Safari and iPad to work. I removed "strict mode" at the beginning

 1. pdf.js/src/core/worker.js 2. pdf.js/web/download_manager.js 

And working!!

0
source

I had the same problem with Safari Mobile ... It was impossible to open more than 1 MB of PDF ... I decided to fragment the file in parts of 900 kb, combine them in the local one, and then inject it into pdf.js as Uint8Array (). I think this is not a documented limitation.

-1
source

Source: https://habr.com/ru/post/1485404/


All Articles