0

pdf.js uses web workers:

<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 = '../../src/worker_loader.js';
</script>

but as I understand it web workers can't access anything outside of the url that the javascript file came from:

Can I load a web worker script from an absolute URL?

I'm hosting my site on AWS EC2, but serving my static files from AWS S3.

I therefore get an error when trying to run the equivalent of this line: PDFJS.workerSrc = '../../src/worker_loader.js'; that the .js cannot be loaded.

Have I understood the problem correctly, and if so, what options do I have to get around this?

Community
  • 1
  • 1
Chris Wheadon
  • 840
  • 6
  • 17
  • 1
    What's stopping you from hosting pdf.js files on the same site? – async5 Feb 14 '13 at 01:07
  • I'd like to serve most of the static files from aws s3 - is it possible to serve just the pdf.js files from the same server as the main django application and keep the rest on s3? – Chris Wheadon Feb 14 '13 at 08:39

1 Answers1

1

There appear to be two possible methods. Either turn off the workers:

From a pdf.js example:

// 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;

or as suggested by async5 it is relatively easy to serve pdf.js on the same Apache VirtualHost as Django, with some files served by URLs as static media, and others using the mod_wsgi interface to Django. The documentation here is relatively easy to follow for the apache deployment. For local development the following snippet added to urls.py is easy to adapt to serve up pdf.js:

from django.conf import settings

# ... the rest of your URLconf goes here ...

if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
   )

with MEDIA_URL and MEDIA_ROOT set appropriately in settings.py

Both will have performance implications.

Community
  • 1
  • 1
Chris Wheadon
  • 840
  • 6
  • 17