mozilla/pdf.js · error · Error

_triggerDownload - not a valid URL: ${originalUrl}

Error message

_triggerDownload - not a valid URL: ${originalUrl}

What it means

Thrown inside `DownloadManager._triggerDownload` (web/download_manager.js:31), the host-side (viewer) download path. When no blob URL was produced for a non-attachment download, it falls back to downloading from the original document URL; that URL is validated with `createValidAbsoluteUrl(url, 'http://example.com')`. If validation fails (the URL is not absolute or not parseable), the download cannot proceed and it throws. This module is restricted to CHROME and GENERIC builds (an earlier `throw` guards the build target).

Source

Thrown at web/download_manager.js:31

 * limitations under the License.
 */

import { BaseDownloadManager } from "./base_download_manager.js";
import { createValidAbsoluteUrl } from "pdfjs-lib";

if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("CHROME || GENERIC")) {
  throw new Error(
    'Module "pdfjs-web/download_manager" shall not be used ' +
      "outside CHROME and GENERIC builds."
  );
}

class DownloadManager extends BaseDownloadManager {
  _triggerDownload(blobUrl, originalUrl, filename, isAttachment = false) {
    if (!blobUrl && !isAttachment) {
      // Fallback to downloading non-attachments by their URL.
      if (!createValidAbsoluteUrl(originalUrl, "http://example.com")) {
        throw new Error(`_triggerDownload - not a valid URL: ${originalUrl}`);
      }
      blobUrl = originalUrl + "#pdfjs.action=download";
    }

    const a = document.createElement("a");
    a.href = blobUrl;
    a.target = "_parent";
    // Use a.download if available. This increases the likelihood that
    // the file is downloaded instead of opened by another PDF plugin.
    if ("download" in a) {
      a.download = filename;
    }
    // <a> must be in the document for recent Firefox versions,
    // otherwise .click() is ignored.
    (document.body || document.documentElement).append(a);
    a.click();
    a.remove();
  }

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Ensure the document is opened from a valid absolute URL (use `new URL(file, location.href).href` to resolve relative paths).
  2. When sourcing from binary data, create a blob URL first (`URL.createObjectURL(blob)`) so the fallback path is not taken.
  3. Patch the caller of `_triggerDownload` to validate the URL with `createValidAbsoluteUrl` before invoking and surface a user-facing message instead of throwing.
  4. Confirm you are using a CHROME or GENERIC build; in MOZCENTRAL builds a different download manager is used.

Example fix

// before
viewer configured with a relative path: ?file=docs/sample.pdf
// after
// resolve to an absolute URL before loading:
const abs = new URL('docs/sample.pdf', window.location.href).href;
PDFViewerApplication.open(abs);
Defensive patterns

Strategy: validation

Validate before calling

import { createValidAbsoluteUrl } from 'pdfjs-lib';

function isDownloadableUrl(url) {
  return !!createValidAbsoluteUrl(url, 'http://example.com');
}

function toAbsoluteUrl(file) {
  try {
    return new URL(file, window.location.href).href;
  } catch {
    return null;
  }
}

Type guard

function isValidAbsoluteUrl(url) {
  try { new URL(url); return true; } catch { return false; }
}

Prevention

When it happens

Trigger: The viewer tries to download a document whose source was a relative path, a `data:` URL that was not converted to a blob, an empty string, or a malformed URL, and no blob URL was available (`blobUrl` falsy and `isAttachment` false).

Common situations: Loading PDF.js with a relative `?file=` parameter on a page where the base/resolve step failed; passing a raw ArrayBuffer without an associated URL and triggering a non-attachment download; corrupt `originalUrl` from a custom stream; misconfigured proxy/CORS stripping the URL.

Related errors


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