0

pdf.js 使用网络工作者:

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

但据我了解,网络工作者无法访问 javascript 文件来自的 url 之外的任何内容:

我可以从绝对 URL 加载 Web Worker 脚本吗?

我在 AWS EC2 上托管我的网站,但从 AWS S3 提供我的静态文件。

因此,在尝试运行与此行等效时出现错误: PDFJS.workerSrc = '../../src/worker_loader.js'; .js 无法加载。

我是否正确理解了这个问题,如果是,我有什么选择来解决这个问题?

4

1 回答 1

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.

于 2013-02-15T07:39:20.307 回答