binary-husky/gpt_academic · error · RuntimeError

PDF生成失败或文件为空

Error message

PDF生成失败或文件为空

What it means

Raised as RuntimeError on the LibreOffice (Linux) branch after a 'successful' conversion when the expected PDF is missing or zero bytes. LibreOffice exited 0 but either wrote the file somewhere else (e.g. kept a different base name for legacy .doc inputs), silently skipped conversion, or produced an empty file. The code already renames default_pdf to pdf_path when they differ, so this fires only when even the default output is absent/empty.

Source

Thrown at crazy_functions/doc_fns/conversation_doc/word2pdf.py:68

                    capture_output=True, text=True
                )

                if result.returncode != 0:
                    error_msg = result.stderr or "未知错误"
                    print(f"LibreOffice转换失败,错误信息: {error_msg}")
                    raise RuntimeError(f"LibreOffice转换失败: {error_msg}")

                print(f"LibreOffice转换输出: {result.stdout}")

                # 如果输出路径与默认生成的不同,则重命名
                default_pdf = word_path.with_suffix('.pdf')
                if default_pdf != pdf_path and default_pdf.exists():
                    os.rename(default_pdf, pdf_path)
                    print(f"已将PDF从 {default_pdf} 重命名为 {pdf_path}")

                # 验证PDF是否成功生成
                if not pdf_path.exists() or pdf_path.stat().st_size == 0:
                    raise RuntimeError("PDF生成失败或文件为空")

                print(f"PDF转换成功,文件大小: {pdf_path.stat().st_size} 字节")
            else:
                # Windows和MacOS使用docx2pdf
                print(f"使用docx2pdf转换 {word_path} 到 {pdf_path}")
                convert(word_path, pdf_path)

                # 验证PDF是否成功生成
                if not pdf_path.exists() or pdf_path.stat().st_size == 0:
                    raise RuntimeError("PDF生成失败或文件为空")

                print(f"PDF转换成功,文件大小: {pdf_path.stat().st_size} 字节")

            return str(pdf_path)

        except Exception as e:
            print(f"PDF转换异常: {str(e)}")
            raise Exception(f"转换PDF失败: {str(e)}")

View on GitHub (pinned to d6bde0fa54)

Solutions

  1. List the actual outdir contents after conversion to see what filename LibreOffice wrote (it may differ from word_path.stem + '.pdf' for multi-part or temp-named files)
  2. Test the same file with the manual libreoffice CLI command and inspect output/stdout
  3. If the source is .doc, convert to .docx first or match the output name LibreOffice actually produces
  4. Check disk space — a full filesystem yields empty writes with exit 0 in some LO versions
  5. Capture result.stdout in the failure message: LibreOffice reports 'convert ... -> ...' paths there, which pinpoints where the PDF went

Example fix

// before
if not pdf_path.exists() or pdf_path.stat().st_size == 0:
    raise RuntimeError("PDF生成失败或文件为空")

// after
if not pdf_path.exists() or pdf_path.stat().st_size == 0:
    produced = list(pdf_path.parent.glob('*.pdf'))
    raise RuntimeError(
        f"PDF生成失败或文件为空: expected={pdf_path}, "
        f"dir_pdf_files={[str(p) for p in produced]}, lo_stdout={result.stdout!r}")
Defensive patterns

Strategy: validation

Validate before calling

from pathlib import Path

def pdf_is_valid(p) -> bool:
    p = Path(p)
    return p.exists() and p.stat().st_size > 0 and p.read_bytes()[:5] == b'%PDF-'

Try / catch

try:
    pdf = WordToPdfConverter.convert_to_pdf(docx)
except RuntimeError as e:
    if 'PDF生成失败' in str(e):
        # LibreOffice can exit 0 silently; look in outdir for what it wrote
        found = sorted(Path(docx).parent.glob('*.pdf'))
        if found and found[0].stat().st_size > 0:
            pdf = str(found[0])  # accept the actually-produced file
        else:
            raise

Prevention

When it happens

Trigger: Input is .doc (not .docx) so with_suffix('.pdf') name assumptions hold but LibreOffice failed silently; outdir different from expected; empty/corrupt document converts to a 0-byte PDF; rename raced or default_pdf never existed and pdf_path was never at the outdir location.

Common situations: LibreOffice exit code 0 despite failure (common with headless quirks, e.g. missing javadb filter warnings escalating); wrong --outdir when pdf_path.parent differs from cwd; documents that LibreOffice opens as blank.

Related errors


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