binary-husky/gpt_academic · error · RuntimeError

解析PDF失败,请检查PDF是否损坏。

Error message

解析PDF失败,请检查PDF是否损坏。

What it means

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.

Source

Thrown at crazy_functions/pdf_fns/parse_pdf.py:39

        if _grobid_url.endswith('/'): _grobid_url = _grobid_url.rstrip('/')
        with ProxyNetworkActivate('Connect_Grobid'):
            res = requests.get(_grobid_url+'/api/isalive')
        if res.text=='true': return _grobid_url
        else: return None
    except:
        return None

@lru_cache(maxsize=32)
def parse_pdf(pdf_path, grobid_url):
    import scipdf   # pip install scipdf_parser
    if grobid_url.endswith('/'): grobid_url = grobid_url.rstrip('/')
    try:
        with ProxyNetworkActivate('Connect_Grobid'):
            article_dict = scipdf.parse_pdf_to_dict(pdf_path, grobid_url=grobid_url)
    except GROBID_OFFLINE_EXCEPTION:
        raise GROBID_OFFLINE_EXCEPTION("GROBID服务不可用,请修改config中的GROBID_URL,可修改成本地GROBID服务。")
    except:
        raise RuntimeError("解析PDF失败,请检查PDF是否损坏。")
    return article_dict


def produce_report_markdown(gpt_response_collection, meta, paper_meta_info, chatbot, fp, generated_conclusion_files):
    # -=-=-=-=-=-=-=-= 写出第1个文件:翻译前后混合 -=-=-=-=-=-=-=-=
    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)
    promote_file_to_downloadzone(res_path, rename_file=os.path.basename(res_path)+'.md', chatbot=chatbot)
    generated_conclusion_files.append(res_path)

    # -=-=-=-=-=-=-=-= 写出第2个文件:仅翻译后的文本 -=-=-=-=-=-=-=-=
    translated_res_array = []
    # 记录当前的大章节标题:
    last_section_name = ""
    for index, value in enumerate(gpt_response_collection):
        # 先挑选偶数序列号:
        if index % 2 != 0:
            # 先提取当前英文标题:
            cur_section_name = gpt_response_collection[index-1].split('\n')[0].split(" Part")[0]

View on GitHub (pinned to d6bde0fa54)

Solutions

  1. Confirm the PDF opens normally and has a text layer (pdftotext file.pdf - | head)
  2. Remove encryption/protection from the PDF before uploading (qpdf --decrypt in.pdf out.pdf)
  3. If the PDF is fine, check GROBID service logs — this branch also catches server-side errors
  4. If it is a scanned PDF, OCR it first (e.g. ocrmypdf) since GROBID cannot process image-only pages
Defensive patterns

Strategy: try-catch

Validate before calling

def looks_like_valid_pdf(path) -> bool:
    with open(path, 'rb') as f:
        return f.read(5) == b'%PDF-'

Try / catch

try:
    article = parse_pdf(fp, grobid_url)
except RuntimeError as e:
    if '解析PDF失败' in str(e):
        skip_and_log_bad_pdf(fp)  # in batch mode, quarantine instead of aborting
    else:
        raise

Prevention

When it happens

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

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

Related errors


AI-assisted analysis of binary-husky/gpt_academic@d6bde0fa54 (2026-08-14). Data as JSON: /api/errors/0c357db7b4106759. Report an issue: GitHub.