{"record":{"id":"2b827afc6ea6d48a","repo":"feder-cr/Jobs_Applier_AI_Agent_AIHawk","slug":"il-contenuto-html-deve-essere-una-stringa-non-vuot","errorCode":null,"errorMessage":"Il contenuto HTML deve essere una stringa non vuota.","messagePattern":"Il contenuto HTML deve essere una stringa non vuota\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/utils/chrome_utils.py","lineNumber":63,"sourceCode":"    except Exception as e:\n        logger.error(f\"Failed to initialize browser: {str(e)}\")\n        raise RuntimeError(f\"Failed to initialize browser: {str(e)}\")\n\n\n\ndef HTML_to_PDF(html_content, driver):\n    \"\"\"\n    Converte una stringa HTML in un PDF e restituisce il PDF come stringa base64.\n\n    :param html_content: Stringa contenente il codice HTML da convertire.\n    :param driver: Istanza del WebDriver di Selenium.\n    :return: Stringa base64 del PDF generato.\n    :raises ValueError: Se l'input HTML non è una stringa valida.\n    :raises RuntimeError: Se si verifica un'eccezione nel WebDriver.\n    \"\"\"\n    # Validazione del contenuto HTML\n    if not isinstance(html_content, str) or not html_content.strip():\n        raise ValueError(\"Il contenuto HTML deve essere una stringa non vuota.\")\n\n    # Codifica l'HTML in un URL di tipo data\n    encoded_html = urllib.parse.quote(html_content)\n    data_url = f\"data:text/html;charset=utf-8,{encoded_html}\"\n\n    try:\n        driver.get(data_url)\n        # Attendi che la pagina si carichi completamente\n        time.sleep(2)  # Potrebbe essere necessario aumentare questo tempo per HTML complessi\n\n        # Esegue il comando CDP per stampare la pagina in PDF\n        pdf_base64 = driver.execute_cdp_cmd(\"Page.printToPDF\", {\n            \"printBackground\": True,          # Includi lo sfondo nella stampa\n            \"landscape\": False,               # Stampa in verticale (False per ritratto)\n            \"paperWidth\": 8.27,               # Larghezza del foglio in pollici (A4)\n            \"paperHeight\": 11.69,             # Altezza del foglio in pollici (A4)\n            \"marginTop\": 0.8,                  # Margine superiore in pollici (circa 2 cm)\n            \"marginBottom\": 0.8,               # Margine inferiore in pollici (circa 2 cm)","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/feder-cr/Jobs_Applier_AI_Agent_AIHawk/blob/79155b52faccfbd19b834680af285eac70dd2df4/src/utils/chrome_utils.py#L45-L81","documentation":"Validation error from HTML_to_PDF: the html_content argument is not a string or is empty/whitespace-only. The function refuses to proceed because loading an empty data: URL into Chrome would produce a blank or broken PDF. Raised as a plain ValueError before any WebDriver work.","triggerScenarios":"HTML_to_PDF('', driver), HTML_to_PDF(None, driver), or passing bytes/a template object instead of str; a template render that silently produced an empty string.","commonSituations":"Upstream template rendering failed silently and returned ''; passing file contents opened in binary mode; None from a failed fetch of the HTML; whitespace-only output from an empty Jinja template.","solutions":["Check what produces html_content upstream — fix the empty/None render (missing template variables, wrong template path).","If reading from a file, open with mode='r' (text) or decode bytes: f.read().decode('utf-8').","Guard before calling: if not html_content or not html_content.strip(): raise/log early with context.","Pass the rendered template's actual output, not the template object (str(JinjaTemplate.render(...)))."],"exampleFix":"// before\nhtml = template.render(user=user)  # may be '' if template path wrong\npdf_b64 = HTML_to_PDF(html, driver)\n\n# after\nhtml = template.render(user=user)\nif not html or not html.strip():\n    raise ValueError(f\"rendered template is empty: {template_name}\")\npdf_b64 = HTML_to_PDF(html, driver)","handlingStrategy":"validation","validationCode":"if not isinstance(html_content, str) or not html_content.strip():\n    raise ValueError(f\"html_content empty or wrong type: {type(html_content).__name__}\")","typeGuard":"def is_non_empty_str(x: Any) -> TypeGuard[str]:\n    return isinstance(x, str) and bool(x.strip())","tryCatchPattern":"try:\n    pdf_b64 = HTML_to_PDF(html, driver)\nexcept ValueError as e:\n    logger.error(\"refusing to render empty HTML: %s\", e)\n    # fix template/render upstream instead of retrying\n    raise","preventionTips":["Assert rendered templates are non-empty before PDF conversion","Open HTML files in text mode or decode bytes","Fail fast on empty renders with the template name in the message"],"tags":["validation","html","pdf","python"],"backgroundTag":"empty-string-input-validation","analyzedSha":"79155b52faccfbd19b834680af285eac70dd2df4","analyzedAt":"2026-08-28T14:10:26.659Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}