santifer/career-ops · error · Error

Could not determine the rendered PDF page count (received ${

Error message

Could not determine the rendered PDF page count (received ${pageCount}).

What it means

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.

Source

Thrown at generate-pdf.mjs:287

      throw new Error(message);
    }
  }
}

/**
 * Decide whether a rendered CV fits its configured page budget.
 *
 * This is deliberately separate from rendering: page count comes from the
 * PDF Chromium actually produced, and the renderer never changes layout to
 * force content under the limit.
 *
 * @param {number} pageCount - Actual pages in the rendered PDF.
 * @param {{ maxPages?: number, strictPages?: boolean }} [options]
 * @returns {void}
 */
export function enforcePageBudget(pageCount, { maxPages = 2, strictPages = false } = {}) {
  if (!Number.isInteger(pageCount) || pageCount < 1) {
    throw new Error(`Could not determine the rendered PDF page count (received ${pageCount}).`);
  }
  if (!Number.isInteger(maxPages) || maxPages < 1) {
    throw new Error(`Invalid page budget "${maxPages}". Use a positive integer.`);
  }
  if (pageCount <= maxPages) return;

  const actualLabel = 'pages';
  const allowedLabel = maxPages === 1 ? 'page' : 'pages';
  const message =
    `CV is ${pageCount} ${actualLabel}; the allowed maximum is ${maxPages} ${allowedLabel}. ` +
    'Trim lower-priority bullets, older roles, secondary projects, or the competencies strip, then regenerate.';

  if (strictPages) {
    throw new Error(`${message} (--strict-pages requested)`);
  }

  console.warn(`⚠️  ${message} Continuing because overflow is warning-only by default; use --strict-pages to reject it.`);
}

View on GitHub (pinned to 9b17a8ac97)

Solutions

  1. Verify the Playwright render step succeeded and produced a non-empty PDF buffer.
  2. Inspect the generated PDF file directly (open it in a viewer or check its size).
  3. Ensure Chromium is installed and its dependencies are present in the environment (npx playwright install chromium).
  4. Check that the CV HTML has actual content (not an empty template).
  5. Debug countRenderedPdfPages by logging the raw PDF buffer and the parsed page count separately.
Defensive patterns

Strategy: validation

Validate before calling

// After rendering, verify the buffer is non-empty before calling enforcePageBudget
if (!pdfBuffer || pdfBuffer.length < 100) {
  throw new Error('Playwright returned an empty or near-empty PDF buffer');
}
const pageCount = countRenderedPdfPages(pdfBuffer);
if (!Number.isInteger(pageCount) || pageCount < 1) {
  throw new Error(`Page count extraction failed: got ${pageCount} from a ${pdfBuffer.length}-byte buffer`);
}
enforcePageBudget(pageCount, { maxPages });

Type guard

/** @param {unknown} n */
function isPositiveIntegerPageCount(n) {
  return typeof n === 'number' && Number.isInteger(n) && n >= 1;
}

Try / catch

try {
  enforcePageBudget(pageCount, { maxPages: 2 });
} catch (err) {
  if (err.message.includes('Could not determine the rendered PDF page count')) {
    console.error('PDF page count extraction failed — likely an empty/corrupt PDF. Buffer size:', pdfBuffer.length);
    // Investigate: re-render, check Chromium, inspect the HTML
    process.exit(1);
  }
  throw err;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of santifer/career-ops@9b17a8ac97 (2026-08-13). Data as JSON: /api/errors/7313fd736436ef46. Report an issue: GitHub.