mozilla/pdf.js · error · Error

getDocument - expected either `data`, `range`, or `url` para

Error message

getDocument - expected either `data`, `range`, or `url` parameter.

What it means

A generic Error from getDocument when none of data, rangeTransport (range), or url was supplied in the source parameters. At least one of these is mandatory; the loader cannot obtain PDF bytes otherwise. The branch is the final else after data/range/url checks.

Source

Thrown at src/display/api.js:442

          disableRange,
          disableStream,
        });
      } else if (url) {
        if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
          throw new Error("Not implemented: NetworkStream");
        }
        const NetworkStream = getNetworkStream(url);

        networkStream = new NetworkStream({
          url,
          httpHeaders,
          withCredentials,
          rangeChunkSize,
          disableRange,
          disableStream,
        });
      } else {
        throw new Error(
          "getDocument - expected either `data`, `range`, or `url` parameter."
        );
      }

      return workerIdPromise.then(workerId => {
        if (worker.destroyed) {
          throw new Error("Worker was destroyed");
        }

        const messageHandler = new MessageHandler(docId, workerId, worker.port);
        const transport = new WorkerTransport(
          messageHandler,
          task,
          networkStream,
          transportParams,
          transportFactory,
          pagesMapper
        );

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Provide exactly one of data (Uint8Array/ArrayBuffer), url (string), or range (PDFDataRangeTransport) in the getDocument source.
  2. Guard the call site: only invoke getDocument once the source field is non-empty.
  3. For Node, read the file: const data = new Uint8Array(fs.readFileSync(path)).

Example fix

// before
pdfjsLib.getDocument({}); // throws
pdfjsLib.getDocument({ path }); // typo: 'path' is not recognized

// after
const data = new Uint8Array(fs.readFileSync(path));
pdfjsLib.getDocument({ data });
Defensive patterns

Strategy: validation

Validate before calling

function assertSource(src) {
  if (!src || (!src.data && !src.url && !src.range)) {
    throw new Error('getDocument needs one of: data, url, range');
  }
}
assertSource(src);
pdfjsLib.getDocument(src);

Type guard

// Type guard narrowing a valid getDocument source.
function isValidPdfSource(src) {
  return !!src && (
    src.data instanceof Uint8Array ||
    typeof src.url === 'string' ||
    !!src.range
  );
}

Prevention

When it happens

Trigger: Calling getDocument({}) or getDocument({ foo: bar }) with no recognized source field; passing a source object whose data/range/url keys are undefined (e.g. typo, destructuring bug, or late-assigned variable).

Common situations: Missing input file/URL in a dynamic UI before the user selected one; wrong key name (e.g. byteArray instead of data); Node.js usage forgetting to read the file into a buffer.

Related errors


AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13). Data as JSON: /api/errors/d965da551d4f815b. Report an issue: GitHub.