{"record":{"id":"0c357db7b4106759","repo":"binary-husky/gpt_academic","slug":"pdf-pdf","errorCode":null,"errorMessage":"解析PDF失败，请检查PDF是否损坏。","messagePattern":"解析PDF失败，请检查PDF是否损坏。","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"crazy_functions/pdf_fns/parse_pdf.py","lineNumber":39,"sourceCode":"        if _grobid_url.endswith('/'): _grobid_url = _grobid_url.rstrip('/')\n        with ProxyNetworkActivate('Connect_Grobid'):\n            res = requests.get(_grobid_url+'/api/isalive')\n        if res.text=='true': return _grobid_url\n        else: return None\n    except:\n        return None\n\n@lru_cache(maxsize=32)\ndef parse_pdf(pdf_path, grobid_url):\n    import scipdf   # pip install scipdf_parser\n    if grobid_url.endswith('/'): grobid_url = grobid_url.rstrip('/')\n    try:\n        with ProxyNetworkActivate('Connect_Grobid'):\n            article_dict = scipdf.parse_pdf_to_dict(pdf_path, grobid_url=grobid_url)\n    except GROBID_OFFLINE_EXCEPTION:\n        raise GROBID_OFFLINE_EXCEPTION(\"GROBID服务不可用，请修改config中的GROBID_URL，可修改成本地GROBID服务。\")\n    except:\n        raise RuntimeError(\"解析PDF失败，请检查PDF是否损坏。\")\n    return article_dict\n\n\ndef produce_report_markdown(gpt_response_collection, meta, paper_meta_info, chatbot, fp, generated_conclusion_files):\n    # -=-=-=-=-=-=-=-= 写出第1个文件：翻译前后混合 -=-=-=-=-=-=-=-=\n    res_path = write_history_to_file(meta +  [\"# Meta Translation\" , paper_meta_info] + gpt_response_collection, file_basename=f\"{gen_time_str()}translated_and_original.md\", file_fullname=None)\n    promote_file_to_downloadzone(res_path, rename_file=os.path.basename(res_path)+'.md', chatbot=chatbot)\n    generated_conclusion_files.append(res_path)\n\n    # -=-=-=-=-=-=-=-= 写出第2个文件：仅翻译后的文本 -=-=-=-=-=-=-=-=\n    translated_res_array = []\n    # 记录当前的大章节标题：\n    last_section_name = \"\"\n    for index, value in enumerate(gpt_response_collection):\n        # 先挑选偶数序列号：\n        if index % 2 != 0:\n            # 先提取当前英文标题：\n            cur_section_name = gpt_response_collection[index-1].split('\\n')[0].split(\" Part\")[0]","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/binary-husky/gpt_academic/blob/d6bde0fa54373309bd05823a49bda8da019d2c77/crazy_functions/pdf_fns/parse_pdf.py#L21-L57","documentation":"The catch-all in parse_pdf(): any exception from scipdf.parse_pdf_to_dict that is NOT the recognized GROBID-offline signal is re-raised as 'PDF parsing failed, check if the PDF is corrupted'. So either the PDF itself could not be parsed (encrypted, malformed, scanned-without-text) or an unrecognized GROBID/transport error occurred and got mislabeled.","triggerScenarios":"Password-protected or DRM'd PDFs; zero-page/corrupt PDF files; GROBID returning 500 for an unusual PDF; scipdf library version incompatibility raising inside parsing; the bare 'except:' also swallows KeyboardInterrupt-class errors.","commonSituations":"Batch PDF translation jobs where one bad file aborts; scanned image-only PDFs GROBID cannot structure; the same message appearing for network issues that do not match the GROBID_OFFLINE signature.","solutions":["Confirm the PDF opens normally and has a text layer (pdftotext file.pdf - | head)","Remove encryption/protection from the PDF before uploading (qpdf --decrypt in.pdf out.pdf)","If the PDF is fine, check GROBID service logs — this branch also catches server-side errors","If it is a scanned PDF, OCR it first (e.g. ocrmypdf) since GROBID cannot process image-only pages"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"def looks_like_valid_pdf(path) -> bool:\n    with open(path, 'rb') as f:\n        return f.read(5) == b'%PDF-'","typeGuard":null,"tryCatchPattern":"try:\n    article = parse_pdf(fp, grobid_url)\nexcept RuntimeError as e:\n    if '解析PDF失败' in str(e):\n        skip_and_log_bad_pdf(fp)  # in batch mode, quarantine instead of aborting\n    else:\n        raise","preventionTips":["Validate the %PDF- magic header before sending to GROBID","Decrypt/qpdf-processed uploads before parsing","In batch loops, catch per-file and quarantine failures so one bad PDF does not kill the run"],"tags":["pdf-parsing","grobid","corrupt-pdf","wrapper"],"backgroundTag":null,"analyzedSha":"d6bde0fa54373309bd05823a49bda8da019d2c77","analyzedAt":"2026-08-14T22:48:35.038Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}