{"record":{"id":"2878bd64916e7eac","repo":"mozilla/pdf.js","slug":"triggerdownload-not-a-valid-url-originalurl","errorCode":null,"errorMessage":"_triggerDownload - not a valid URL: ${originalUrl}","messagePattern":"_triggerDownload - not a valid URL: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"web/download_manager.js","lineNumber":31,"sourceCode":" * limitations under the License.\n */\n\nimport { BaseDownloadManager } from \"./base_download_manager.js\";\nimport { createValidAbsoluteUrl } from \"pdfjs-lib\";\n\nif (typeof PDFJSDev !== \"undefined\" && !PDFJSDev.test(\"CHROME || GENERIC\")) {\n  throw new Error(\n    'Module \"pdfjs-web/download_manager\" shall not be used ' +\n      \"outside CHROME and GENERIC builds.\"\n  );\n}\n\nclass DownloadManager extends BaseDownloadManager {\n  _triggerDownload(blobUrl, originalUrl, filename, isAttachment = false) {\n    if (!blobUrl && !isAttachment) {\n      // Fallback to downloading non-attachments by their URL.\n      if (!createValidAbsoluteUrl(originalUrl, \"http://example.com\")) {\n        throw new Error(`_triggerDownload - not a valid URL: ${originalUrl}`);\n      }\n      blobUrl = originalUrl + \"#pdfjs.action=download\";\n    }\n\n    const a = document.createElement(\"a\");\n    a.href = blobUrl;\n    a.target = \"_parent\";\n    // Use a.download if available. This increases the likelihood that\n    // the file is downloaded instead of opened by another PDF plugin.\n    if (\"download\" in a) {\n      a.download = filename;\n    }\n    // <a> must be in the document for recent Firefox versions,\n    // otherwise .click() is ignored.\n    (document.body || document.documentElement).append(a);\n    a.click();\n    a.remove();\n  }","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/web/download_manager.js#L13-L49","documentation":"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).","triggerScenarios":"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).","commonSituations":"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.","solutions":["Ensure the document is opened from a valid absolute URL (use `new URL(file, location.href).href` to resolve relative paths).","When sourcing from binary data, create a blob URL first (`URL.createObjectURL(blob)`) so the fallback path is not taken.","Patch the caller of `_triggerDownload` to validate the URL with `createValidAbsoluteUrl` before invoking and surface a user-facing message instead of throwing.","Confirm you are using a CHROME or GENERIC build; in MOZCENTRAL builds a different download manager is used."],"exampleFix":"// before\nviewer configured with a relative path: ?file=docs/sample.pdf\n// after\n// resolve to an absolute URL before loading:\nconst abs = new URL('docs/sample.pdf', window.location.href).href;\nPDFViewerApplication.open(abs);","handlingStrategy":"validation","validationCode":"import { createValidAbsoluteUrl } from 'pdfjs-lib';\n\nfunction isDownloadableUrl(url) {\n  return !!createValidAbsoluteUrl(url, 'http://example.com');\n}\n\nfunction toAbsoluteUrl(file) {\n  try {\n    return new URL(file, window.location.href).href;\n  } catch {\n    return null;\n  }\n}","typeGuard":"function isValidAbsoluteUrl(url) {\n  try { new URL(url); return true; } catch { return false; }\n}","tryCatchPattern":null,"preventionTips":["Open documents from absolute URLs; resolve relative paths with new URL(file, location.href).","When loading from binary data, create a blob URL first (URL.createObjectURL) so the fallback path is not used.","Use a CHROME or GENERIC build if you rely on this DownloadManager; MOZCENTRAL uses a different one.","Validate the URL with createValidAbsoluteUrl before triggering a download and show a user-facing message on failure."],"tags":["viewer","download","url-validation","web","network"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}