Chuyển đến nội dung
Skillpedia
PDF, Word và slide7.8k · #12914

pdf-debug-unbuffered

Skill này làm gì

Skill này là một mẹo gỡ lỗi cho các script PDF Python thất bại với thông báo mơ hồ. Dành cho người có code PDF dừng lại và không hiện lý do hữu ích.

Khi nào nên dùng

  • ✓Khi một script PDF Python thất bại với thông báo lỗi không rõ chung chung.
  • ✓Khi script dường như thất bại âm thầm và không có traceback nào hiện ra.
  • ✓Khi tôi nghi ngờ có phông chữ, import hoặc đường dẫn tệp bị thiếu trong code PDF.

Liên kết skill

https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdf-debug-unbuffered/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdf-debug-unbuffered/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Script PDF Python của tôi thất bại với một thông báo không nói lên điều gì hữu ích. Hãy chạy nó để chúng ta thấy lỗi thật.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

AI chạy script với cờ unbuffered, kết hợp đầu ra lỗi với đầu ra thông thường, rồi đọc các dòng đầu để tìm traceback.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

PDF Generation Debug Technique

When Python PDF libraries (reportlab, fpdf, etc.) fail with generic "unknown error" messages, the actual cause is often hidden due to output buffering or error swallowing. This skill provides a technique to expose the real traceback.

Core Technique

Run the Python script with the -u flag (unbuffered output) and pipe stderr through head to capture the actual error:

python -u your_script.py 2>&1 | head -100

Or more specifically, to focus on stderr:

python -u your_script.py 2>&1 | head -50

Why This Works

  1. -u flag: Forces Python to run in unbuffered mode, ensuring output (including errors) is flushed immediately rather than held in buffers that may be lost on crash
  2. 2>&1: Redirects stderr to stdout so both streams are captured together
  3. head: Limits output to show the most relevant error messages at the top

Step-by-Step Debugging

Step 1: Identify the failing script

Locate the Python script that generates the PDF and is producing generic errors.

Step 2: Run with unbuffered output

python -u generate_pdf.py 2>&1 | head -100

Step 3: Analyze the traceback

Look for:

  • Import errors (missing dependencies)
  • File path issues (invalid paths, permission errors)
  • Font registration problems (common with reportlab)
  • Memory or resource constraints

Step 4: Fix the underlying issue

Address the specific error revealed in the traceback.

Step 5: Re-run normally

Once fixed, run the script without the debug flags to confirm it works:

python generate_pdf.py

Common Issues Revealed

Symptom Likely Cause
"unknown error" on PDF save File path doesn't exist or no write permissions
Generic failure during build Missing font files or font registration issues
Silent crash ImportError for missing dependencies
Incomplete PDF Script terminated early due to unhandled exception

Alternative Approaches

If the above doesn't work, try:

# Capture full stderr to a file
python -u script.py 2> error.log

# Use Python's traceback module explicitly
python -c "import traceback; exec(open('script.py').read())" 2>&1

# Run with verbose import tracing
python -v -u script.py 2>&1 | head -200

Example: Debugging reportlab

# Before: Generic error
python create_checklist.py
# Output: "Error: unknown"

# After: Actual traceback  
python -u create_checklist.py 2>&1 | head -50
# Output: "FileNotFoundError: [Errno 2] No such file or directory: '/fonts/Helvetica.ttf'"

Notes

  • This technique applies to any Python script with opaque error messages, not just PDF generation
  • The -u flag is available in Python 2.7+ and all Python 3 versions
  • For production debugging, consider logging to a file instead of using head
Câu hỏi thường gặp

Cờ unbuffered làm gì?

Nó khiến Python gửi đầu ra ngay lập tức, nên thông báo không bị mất nếu chương trình bị sập.

Vì sao giới hạn đầu ra bằng head?

Giới hạn đầu ra giữ các dòng lỗi quan trọng nhất ở đầu, nơi dễ đọc.

Những loại lỗi nào được phát hiện?

Hướng dẫn nhắc đến import bị thiếu, vấn đề đường dẫn tệp, lỗi đăng ký phông chữ và giới hạn tài nguyên.

Chi tiết

Mô tả gốc của tác giả

Debug Python PDF generation errors by using unbuffered output and stderr inspection to reveal actual tracebacks

repo: HKUDS/AgentPilot

Độ phổ biến

Sao GitHub
7.8k
Vị trí xếp hạng
#12914
Giấy phép
MIT
Cập nhật
1/11/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự