{"record":{"id":"7313fd736436ef46","repo":"santifer/career-ops","slug":"could-not-determine-the-rendered-pdf-page-count-r","errorCode":null,"errorMessage":"Could not determine the rendered PDF page count (received ${pageCount}).","messagePattern":"Could not determine the rendered PDF page count \\(received (.+?)\\)\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"generate-pdf.mjs","lineNumber":287,"sourceCode":"      throw new Error(message);\n    }\n  }\n}\n\n/**\n * Decide whether a rendered CV fits its configured page budget.\n *\n * This is deliberately separate from rendering: page count comes from the\n * PDF Chromium actually produced, and the renderer never changes layout to\n * force content under the limit.\n *\n * @param {number} pageCount - Actual pages in the rendered PDF.\n * @param {{ maxPages?: number, strictPages?: boolean }} [options]\n * @returns {void}\n */\nexport function enforcePageBudget(pageCount, { maxPages = 2, strictPages = false } = {}) {\n  if (!Number.isInteger(pageCount) || pageCount < 1) {\n    throw new Error(`Could not determine the rendered PDF page count (received ${pageCount}).`);\n  }\n  if (!Number.isInteger(maxPages) || maxPages < 1) {\n    throw new Error(`Invalid page budget \"${maxPages}\". Use a positive integer.`);\n  }\n  if (pageCount <= maxPages) return;\n\n  const actualLabel = 'pages';\n  const allowedLabel = maxPages === 1 ? 'page' : 'pages';\n  const message =\n    `CV is ${pageCount} ${actualLabel}; the allowed maximum is ${maxPages} ${allowedLabel}. ` +\n    'Trim lower-priority bullets, older roles, secondary projects, or the competencies strip, then regenerate.';\n\n  if (strictPages) {\n    throw new Error(`${message} (--strict-pages requested)`);\n  }\n\n  console.warn(`⚠️  ${message} Continuing because overflow is warning-only by default; use --strict-pages to reject it.`);\n}","sourceCodeStart":269,"sourceCodeEnd":305,"githubUrl":"https://github.com/santifer/career-ops/blob/9b17a8ac97b398a496b38e423ae24e433b43254f/generate-pdf.mjs#L269-L305","documentation":"enforcePageBudget receives pageCount (the number of pages Chromium actually produced) and validates it is a positive integer before comparing against maxPages. This error means pageCount was not a usable integer — null, NaN, 0, undefined, a float, or a negative number. The root cause is upstream: countRenderedPdfPages failed to extract a page count from the PDF buffer, typically because the PDF was empty, corrupt, or unparseable.","triggerScenarios":"Playwright/Chromium returned an empty or near-empty buffer; the PDF catalog's /Pages dictionary could not be parsed; the render produced 0 pages (blank template, no content); a race condition where the PDF was read before Chromium finished writing.","commonSituations":"Chromium failed silently (no error thrown but no PDF produced); the CV template rendered no visible content; a headless environment missing Chromium dependencies produced a corrupt PDF; the PDF buffer was truncated.","solutions":["Verify the Playwright render step succeeded and produced a non-empty PDF buffer.","Inspect the generated PDF file directly (open it in a viewer or check its size).","Ensure Chromium is installed and its dependencies are present in the environment (npx playwright install chromium).","Check that the CV HTML has actual content (not an empty template).","Debug countRenderedPdfPages by logging the raw PDF buffer and the parsed page count separately."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// After rendering, verify the buffer is non-empty before calling enforcePageBudget\nif (!pdfBuffer || pdfBuffer.length < 100) {\n  throw new Error('Playwright returned an empty or near-empty PDF buffer');\n}\nconst pageCount = countRenderedPdfPages(pdfBuffer);\nif (!Number.isInteger(pageCount) || pageCount < 1) {\n  throw new Error(`Page count extraction failed: got ${pageCount} from a ${pdfBuffer.length}-byte buffer`);\n}\nenforcePageBudget(pageCount, { maxPages });","typeGuard":"/** @param {unknown} n */\nfunction isPositiveIntegerPageCount(n) {\n  return typeof n === 'number' && Number.isInteger(n) && n >= 1;\n}","tryCatchPattern":"try {\n  enforcePageBudget(pageCount, { maxPages: 2 });\n} catch (err) {\n  if (err.message.includes('Could not determine the rendered PDF page count')) {\n    console.error('PDF page count extraction failed — likely an empty/corrupt PDF. Buffer size:', pdfBuffer.length);\n    // Investigate: re-render, check Chromium, inspect the HTML\n    process.exit(1);\n  }\n  throw err;\n}","preventionTips":["Verify the Playwright render returned a non-empty buffer before page-count extraction.","Ensure Chromium is installed (npx playwright install chromium) and its OS deps are present.","Check that the CV HTML has visible content — an empty or display:none template yields 0 pages.","Log pdfBuffer.length alongside pageCount to distinguish render failure from parse failure."],"tags":["pdf","playwright","validation","chromium"],"backgroundTag":null,"analyzedSha":"9b17a8ac97b398a496b38e423ae24e433b43254f","analyzedAt":"2026-08-13T00:48:39.135Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}