Address review comments. Use only one PDFJS.workerSrc variable to specify the worker source
This commit is contained in:
parent
1e6d1f9922
commit
3b7829d057
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
getPdf('helloworld.pdf', function getPdfHelloWorld(data) {
|
PDFJS.getPdf('helloworld.pdf', function getPdfHelloWorld(data) {
|
||||||
//
|
//
|
||||||
// Instantiate PDFDoc with PDF data
|
// Instantiate PDFDoc with PDF data
|
||||||
//
|
//
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
<head>
|
<head>
|
||||||
<!-- In production, only one script (pdf.js) is necessary -->
|
<!-- In production, only one script (pdf.js) is necessary -->
|
||||||
|
<!-- In production, change the content of PDFJS.workerSrc below -->
|
||||||
<script type="text/javascript" src="../../src/core.js"></script>
|
<script type="text/javascript" src="../../src/core.js"></script>
|
||||||
<script type="text/javascript" src="../../src/util.js"></script>
|
<script type="text/javascript" src="../../src/util.js"></script>
|
||||||
<script type="text/javascript" src="../../src/canvas.js"></script>
|
<script type="text/javascript" src="../../src/canvas.js"></script>
|
||||||
@ -23,9 +24,9 @@
|
|||||||
<script type="text/javascript" src="../../src/worker.js"></script>
|
<script type="text/javascript" src="../../src/worker.js"></script>
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
// Specify the directory of the source files, such that the web worker
|
// Specify the main script used to create a new PDF.JS web worker.
|
||||||
// knows where to load them.
|
// In production, change this to point to the combined `pdf.js` file.
|
||||||
var PDFJS_WORKER_DIR = '../../src/';
|
PDFJS.workerSrc = '../../src/worker_loader.js';
|
||||||
</script>
|
</script>
|
||||||
<script type="text/javascript" src="hello.js"></script>
|
<script type="text/javascript" src="hello.js"></script>
|
||||||
</head>
|
</head>
|
||||||
|
30
src/core.js
30
src/core.js
@ -471,26 +471,18 @@ var PDFDoc = (function() {
|
|||||||
this.pageCache = [];
|
this.pageCache = [];
|
||||||
|
|
||||||
if (useWorker) {
|
if (useWorker) {
|
||||||
var worker;
|
var workerSrc = PDFJS.workerSrc;
|
||||||
if (typeof PDFJS_WORKER_DIR !== 'undefined') {
|
if (typeof workerSrc === 'undefined') {
|
||||||
// If `PDFJS_WORKER_DIR` is specified, we assume the pdf.js files
|
throw 'No PDFJS.workerSrc specified';
|
||||||
// located all in that directory. Create a new worker and tell him
|
|
||||||
// the directory, such that he can load the scripts from there.
|
|
||||||
worker = new Worker(PDFJS_WORKER_DIR + 'worker_loader.js');
|
|
||||||
console.log('main: post dir');
|
|
||||||
|
|
||||||
worker.postMessage(PDFJS_WORKER_DIR);
|
|
||||||
} else if (typeof PDFJS_WORKER_FILE !== 'undefined') {
|
|
||||||
// If we build the worker using a worker file, then we assume, that
|
|
||||||
// everything the worker needs is already included in that file.
|
|
||||||
// Therefore the worker doesn't have to call `importScripts` to load
|
|
||||||
// all the single files and therefore it's not necessary to tell the
|
|
||||||
// worker a directory to laod the js files from.
|
|
||||||
// (Which is different from the PDFJS_WORKER_DIR case above.)
|
|
||||||
worker = new Worker(PDFJS_WORKER_FILE);
|
|
||||||
} else {
|
|
||||||
throw 'No worker file or directory specified.';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var worker = new Worker(workerSrc);
|
||||||
|
|
||||||
|
// Tell the worker the file it was created from.
|
||||||
|
worker.postMessage({
|
||||||
|
action: 'workerSrc',
|
||||||
|
data: workerSrc
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
// If we don't use a worker, just post/sendMessage to the main thread.
|
// If we don't use a worker, just post/sendMessage to the main thread.
|
||||||
var worker = {
|
var worker = {
|
||||||
|
@ -10,5 +10,5 @@ var PDFJS = {};
|
|||||||
// Files are inserted below - see Makefile
|
// Files are inserted below - see Makefile
|
||||||
/* PDFJSSCRIPT_INCLUDE_ALL */
|
/* PDFJSSCRIPT_INCLUDE_ALL */
|
||||||
|
|
||||||
})();
|
}).call((typeof window === 'undefined') ? this : window);
|
||||||
|
|
||||||
|
@ -47,6 +47,13 @@ var WorkerProcessorHandler = {
|
|||||||
setup: function(handler) {
|
setup: function(handler) {
|
||||||
var pdfDoc = null;
|
var pdfDoc = null;
|
||||||
|
|
||||||
|
handler.on('workerSrc', function(data) {
|
||||||
|
// In development, the `workerSrc` message is handled in the
|
||||||
|
// `worker_loader.js` file. In production the workerProcessHandler is
|
||||||
|
// called for this. This servers as a dummy to prevent calling an
|
||||||
|
// undefined action `workerSrc`.
|
||||||
|
});
|
||||||
|
|
||||||
handler.on('doc', function(data) {
|
handler.on('doc', function(data) {
|
||||||
// Create only the model of the PDFDoc, which is enough for
|
// Create only the model of the PDFDoc, which is enough for
|
||||||
// processing the content of the pdf.
|
// processing the content of the pdf.
|
||||||
@ -174,7 +181,7 @@ var workerConsole = {
|
|||||||
|
|
||||||
// Worker thread?
|
// Worker thread?
|
||||||
if (typeof window === 'undefined') {
|
if (typeof window === 'undefined') {
|
||||||
this.console = workerConsole;
|
globalScope.console = workerConsole;
|
||||||
|
|
||||||
var handler = new MessageHandler('worker_processor', this);
|
var handler = new MessageHandler('worker_processor', this);
|
||||||
WorkerProcessorHandler.setup(handler);
|
WorkerProcessorHandler.setup(handler);
|
||||||
|
@ -7,11 +7,19 @@ this.onmessage = function(evt) {
|
|||||||
// Reset the `onmessage` function as it was only set to call
|
// Reset the `onmessage` function as it was only set to call
|
||||||
// this function the first time a message is passed to the worker
|
// this function the first time a message is passed to the worker
|
||||||
// but shouldn't get called anytime afterwards.
|
// but shouldn't get called anytime afterwards.
|
||||||
delete this.onmessage;
|
this.onmessage = null;
|
||||||
|
|
||||||
// Directory the include files are contained is send as the
|
if (evt.data.action !== 'workerSrc') {
|
||||||
// first message to the worker.
|
throw 'Worker expects first message to be `workerSrc`';
|
||||||
var dir = evt.data;
|
}
|
||||||
|
|
||||||
|
// Content of `PDFJS.workerSrc` as defined on the main thread.
|
||||||
|
var workerSrc = evt.data.data;
|
||||||
|
|
||||||
|
// Extract the directory that contains the source files to load.
|
||||||
|
// Assuming the source files have the same relative possition as the
|
||||||
|
// `workerSrc` file.
|
||||||
|
var dir = workerSrc.substring(0, workerSrc.lastIndexOf('/') + 1);
|
||||||
|
|
||||||
// List of files to include;
|
// List of files to include;
|
||||||
var files = [
|
var files = [
|
||||||
|
@ -3,25 +3,29 @@
|
|||||||
<head>
|
<head>
|
||||||
<title>pdf.js test slave</title>
|
<title>pdf.js test slave</title>
|
||||||
<style type="text/css"></style>
|
<style type="text/css"></style>
|
||||||
<script type="text/javascript" src="/src/core.js"></script>
|
<script type="text/javascript" src="/src/core.js"></script>
|
||||||
<script type="text/javascript" src="/src/util.js"></script>
|
<script type="text/javascript" src="/src/util.js"></script>
|
||||||
<script type="text/javascript" src="/src/canvas.js"></script>
|
<script type="text/javascript" src="/src/canvas.js"></script>
|
||||||
<script type="text/javascript" src="/src/obj.js"></script>
|
<script type="text/javascript" src="/src/obj.js"></script>
|
||||||
<script type="text/javascript" src="/src/function.js"></script>
|
<script type="text/javascript" src="/src/function.js"></script>
|
||||||
<script type="text/javascript" src="/src/charsets.js"></script>
|
<script type="text/javascript" src="/src/charsets.js"></script>
|
||||||
<script type="text/javascript" src="/src/cidmaps.js"></script>
|
<script type="text/javascript" src="/src/cidmaps.js"></script>
|
||||||
<script type="text/javascript" src="/src/colorspace.js"></script>
|
<script type="text/javascript" src="/src/colorspace.js"></script>
|
||||||
<script type="text/javascript" src="/src/crypto.js"></script>
|
<script type="text/javascript" src="/src/crypto.js"></script>
|
||||||
<script type="text/javascript" src="/src/evaluator.js"></script>
|
<script type="text/javascript" src="/src/evaluator.js"></script>
|
||||||
<script type="text/javascript" src="/src/fonts.js"></script>
|
<script type="text/javascript" src="/src/fonts.js"></script>
|
||||||
<script type="text/javascript" src="/src/glyphlist.js"></script>
|
<script type="text/javascript" src="/src/glyphlist.js"></script>
|
||||||
<script type="text/javascript" src="/src/image.js"></script>
|
<script type="text/javascript" src="/src/image.js"></script>
|
||||||
<script type="text/javascript" src="/src/metrics.js"></script>
|
<script type="text/javascript" src="/src/metrics.js"></script>
|
||||||
<script type="text/javascript" src="/src/parser.js"></script>
|
<script type="text/javascript" src="/src/parser.js"></script>
|
||||||
<script type="text/javascript" src="/src/pattern.js"></script>
|
<script type="text/javascript" src="/src/pattern.js"></script>
|
||||||
<script type="text/javascript" src="/src/stream.js"></script>
|
<script type="text/javascript" src="/src/stream.js"></script>
|
||||||
<script type="text/javascript" src="/src/worker.js"></script>
|
<script type="text/javascript" src="/src/worker.js"></script>
|
||||||
<script type="text/javascript" src="driver.js"></script>
|
<script type="text/javascript" src="driver.js"></script>
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
PDFJS.workerSrc = '/src/worker_loader.js';
|
||||||
|
</script>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
<!-- This snippet is used in production, see Makefile -->
|
<!-- This snippet is used in production, see Makefile -->
|
||||||
<script type="text/javascript" src="../build/pdf.js"></script>
|
<script type="text/javascript" src="../build/pdf.js"></script>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
// This specifies the location of the pdf.js file. This is necessary to
|
// This specifies the location of the pdf.js file.
|
||||||
// spawn the web worker.
|
PDFJS.workerSrc = "../build/pdf.js";
|
||||||
var PDFJS_WORKER_FILE = "../build/pdf.js";
|
|
||||||
</script>
|
</script>
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
<link rel="stylesheet" href="viewer.css"/>
|
<link rel="stylesheet" href="viewer.css"/>
|
||||||
|
|
||||||
<!-- PDFJSSCRIPT_INCLUDE_BUILD -->
|
<!-- PDFJSSCRIPT_INCLUDE_BUILD -->
|
||||||
<script type="text/javascript">var PDFJS_WORKER_DIR = "../src/"</script> <!-- PDFJSSCRIPT_REMOVE -->
|
|
||||||
<script type="text/javascript" src="../src/core.js"></script> <!-- PDFJSSCRIPT_REMOVE -->
|
<script type="text/javascript" src="../src/core.js"></script> <!-- PDFJSSCRIPT_REMOVE -->
|
||||||
<script type="text/javascript" src="../src/util.js"></script> <!-- PDFJSSCRIPT_REMOVE -->
|
<script type="text/javascript" src="../src/util.js"></script> <!-- PDFJSSCRIPT_REMOVE -->
|
||||||
<script type="text/javascript" src="../src/canvas.js"></script> <!-- PDFJSSCRIPT_REMOVE -->
|
<script type="text/javascript" src="../src/canvas.js"></script> <!-- PDFJSSCRIPT_REMOVE -->
|
||||||
@ -25,6 +24,8 @@
|
|||||||
<script type="text/javascript" src="../src/stream.js"></script> <!-- PDFJSSCRIPT_REMOVE -->
|
<script type="text/javascript" src="../src/stream.js"></script> <!-- PDFJSSCRIPT_REMOVE -->
|
||||||
<script type="text/javascript" src="../src/worker.js"></script> <!-- PDFJSSCRIPT_REMOVE -->
|
<script type="text/javascript" src="../src/worker.js"></script> <!-- PDFJSSCRIPT_REMOVE -->
|
||||||
|
|
||||||
|
<script type="text/javascript">PDFJS.workerSrc = '../../src/worker_loader.js';</script> <!-- PDFJSSCRIPT_REMOVE -->
|
||||||
|
|
||||||
<script type="text/javascript" src="compatibility.js"></script>
|
<script type="text/javascript" src="compatibility.js"></script>
|
||||||
<script type="text/javascript" src="viewer.js"></script>
|
<script type="text/javascript" src="viewer.js"></script>
|
||||||
</head>
|
</head>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user