跳到正文
Skillpedia
PDF、Word 与幻灯片7.8k · #12927

pdf-gen-fallback-dee20c

它能做什么

这项技能是 PDF 生成的备用方案,包含库检查和可见的错误处理。它适合 PDF 或报告任务在沙箱中失败、又没有明确原因的人。

适用场景

  • ✓当我在制作 PDF 或报告时,沙箱给出不明确的错误的时候。
  • ✓当我希望显示错误信息,以便看清问题出在哪里的时候。
  • ✓当我需要把一个简单的 PDF 保存为这台电脑上的文件的时候。

技能链接

https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdf-gen-fallback-dee20c/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdf-gen-fallback-dee20c/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请用我的标题和正文制作一个简单的 PDF。沙箱失败且没有明确信息,请在 shell 中运行,并把所有错误展示给我。”

每周 5 个新技能,1 分钟读完。

工作原理

AI 会检查所需的库,然后在 shell 中运行一个 Python 脚本。该脚本会写入 PDF 并打印成功信息,代码外层包含错误处理。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

PDF Generation Fallback Workflow

Purpose

When execute_code_sandbox returns opaque or unhelpful errors during document/PDF generation, use run_shell with direct python -c execution as a reliable alternative. This pattern includes library availability checks and explicit error handling.

When to Use

  • execute_code_sandbox fails with unclear error messages during document generation
  • PDF, report, or document creation tasks are failing unexpectedly
  • You need more control over the Python execution environment

Step-by-Step Instructions

1. Check Library Availability

Before attempting generation, verify required libraries are installed:

run_shell: python -c "import reportlab; print('reportlab OK')"
run_shell: python -c "import fpdf; print('fpdf OK')"
run_shell: python -c "import PyPDF2; print('PyPDF2 OK')"

If a library is missing, install it:

run_shell: pip install reportlab --quiet

2. Execute Generation via run_shell

Instead of execute_code_sandbox, use run_shell with python -c:

run_shell: python -c "
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
c = canvas.Canvas('output.pdf', pagesize=letter)
c.drawString(100, 750, 'Document Content')
c.save()
print('PDF generated successfully')
"

3. Handle Errors Explicitly

Wrap generation code with try/except and capture stderr:

run_shell: python -c "
import sys
try:
    # Your generation code here
    from reportlab.pdfgen import canvas
    c = canvas.Canvas('output.pdf')
    c.drawString(100, 750, 'Content')
    c.save()
    print('SUCCESS: PDF created')
    sys.exit(0)
except Exception as e:
    print(f'ERROR: {type(e).__name__}: {e}', file=sys.stderr)
    sys.exit(1)
" 2>&1

4. Verify Output File

After generation, confirm the file was created:

run_shell: ls -la output.pdf
run_shell: file output.pdf

5. Fallback Hierarchy

If the first approach fails, try alternatives in this order:

  1. reportlab - Full-featured PDF generation
  2. fpdf - Simpler PDF library
  3. wkhtmltopdf - HTML to PDF conversion
  4. Direct text/Markdown output as last resort

Example: Complete Workflow

# Step 1: Check library
run_shell: python -c "import reportlab; print('OK')"

# Step 2: Generate with error handling
run_shell: python -c "
import sys
try:
    from reportlab.lib.pagesizes import letter
    from reportlab.pdfgen import canvas
    c = canvas.Canvas('report.pdf', pagesize=letter)
    c.setTitle('Surveillance Report')
    c.drawString(100, 750, 'Revised Surveillance Report')
    c.drawString(100, 730, 'Summary: Key findings documented')
    c.save()
    print('SUCCESS')
except Exception as e:
    print(f'FAILED: {e}', file=sys.stderr)
    sys.exit(1)
"

# Step 3: Verify
run_shell: test -f report.pdf && echo 'File exists' || echo 'File missing'

Troubleshooting

Issue Solution
Module not found pip install <module> --quiet
Permission denied Check output directory permissions
Font errors Use default fonts or install font packages
Encoding issues Specify encoding explicitly in string operations

Best Practices

  • Always capture both stdout and stderr for debugging
  • Exit with proper status codes (0 for success, 1 for failure)
  • Keep generation code concise when using python -c
  • For complex generation, write a temporary .py file instead
  • Test library availability before attempting generation
常见问题

为什么要把代码包裹在错误处理中?

指南说,这样失败会被清楚地显示,而不会被隐藏。

脚本最后会打印什么?

一条成功信息,便于确认结果。

示例使用了哪个库?

示例使用 reportlab 的 canvas 绘制文字并保存 PDF。

详情

作者的原始描述

Fallback workflow for document generation when code sandbox returns opaque errors

repo: HKUDS/AgentPilot

热度

GitHub 星标
7.8k
排名
#12927
许可证
MIT
更新时间
2026/11/1

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能