{"record":{"id":"0e1dafd922d98d25","repo":"mozilla/pdf.js","slug":"the-pdf-file-is-empty-i-e-its-size-is-zero-bytes","errorCode":null,"errorMessage":"The PDF file is empty, i.e. its size is zero bytes.","messagePattern":"The PDF file is empty, i\\.e\\. its size is zero bytes\\.","errorType":"exception","errorClass":"InvalidPDFException","httpStatus":null,"severity":"error","filePath":"src/core/document.js","lineNumber":1024,"sourceCode":"  #pagePromises = new Map();\n\n  // Map<id, {byteRange: number[4], pkcs7: Uint8Array}> — populated by the\n  // `signatures` getter, consumed by `getSignatureData`. We deliberately\n  // keep the signed byte spans out of the metadata array and only slice\n  // them out of the stream when the viewer actually asks to verify.\n  #signatureData = null;\n\n  #version = null;\n\n  constructor(pdfManager, stream) {\n    if (typeof PDFJSDev === \"undefined\" || PDFJSDev.test(\"TESTING\")) {\n      assert(\n        stream instanceof BaseStream,\n        'PDFDocument: Invalid \"stream\" argument.'\n      );\n    }\n    if (stream.length <= 0) {\n      throw new InvalidPDFException(\n        \"The PDF file is empty, i.e. its size is zero bytes.\"\n      );\n    }\n\n    this.pdfManager = pdfManager;\n    this.stream = stream;\n    this.xref = new XRef(stream, pdfManager);\n\n    const idCounters = {\n      font: 0,\n    };\n    this._globalIdFactory = class {\n      static getDocId() {\n        return `g_${pdfManager.docId}`;\n      }\n\n      static createFontId() {\n        return `f${++idCounters.font}`;","sourceCodeStart":1006,"sourceCodeEnd":1042,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/core/document.js#L1006-L1042","documentation":"InvalidPDFException thrown by the PDFDocument constructor when the input stream reports length <= 0. pdf.js refuses to construct a document from an empty stream because there is no PDF data to parse (no header, no xref, no body). This is fatal for that load.","triggerScenarios":"Loading a zero-byte file or URL; an aborted fetch whose response body resolved to an empty ArrayBuffer/Blob; an empty ReadableStream; a server returning 200 with no body.","commonSituations":"Network race where the response is truncated to zero bytes; misconfigured upload that wrote nothing; pointing getDocument at a non-existent path that returns an empty 200.","solutions":["Verify the source file size > 0 before calling getDocument (HEAD request, fs.stat, or blob.size).","Check the fetch response: if (!response.ok || response.headers.get('content-length') === '0') abort early.","Map InvalidPDFException to a clear 'file is empty or corrupt' message."],"exampleFix":"// before\nconst doc = await getDocument({ url }).promise;\n\n// after\nconst resp = await fetch(url);\nif (!resp.ok || Number(resp.headers.get('content-length')) === 0) {\n  throw new Error('The file is empty or could not be downloaded.');\n}\nconst buf = await resp.arrayBuffer();\nif (buf.byteLength === 0) throw new Error('Downloaded file is empty.');\nconst doc = await getDocument({ data: buf }).promise;","handlingStrategy":"validation","validationCode":"const resp = await fetch(url);\nif (!resp.ok) throw new Error(`HTTP ${resp.status}`);\nconst len = Number(resp.headers.get('content-length'));\nif (len === 0) throw new Error('File is empty (0 bytes).');\nconst data = await resp.arrayBuffer();\nif (data.byteLength === 0) throw new Error('Downloaded file is empty.');","typeGuard":"function isNonEmptyStreamData(data) {\n  return (data instanceof ArrayBuffer && data.byteLength > 0) ||\n         (data instanceof Uint8Array && data.length > 0) ||\n         (data instanceof Blob && data.size > 0);\n}","tryCatchPattern":"try {\n  const doc = await getDocument({ data }).promise;\n} catch (e) {\n  if (e.name === 'InvalidPDFException' && /size is zero bytes/.test(e.message)) {\n    notifyUser('The file is empty or could not be downloaded.');\n    return;\n  }\n  throw e;\n}","preventionTips":["Check Content-Length / arrayBuffer.byteLength before calling getDocument.","Handle fetch failures and 200-with-empty-body cases explicitly.","For uploads, validate file.size > 0 on the client before submission."],"tags":["validation","loading","invalid-pdf","network"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}