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

pdf-generation-troubleshooting

它能做什么

这项技能是一个三步计划,用于制作 PDF,并从 pandoc 退回到 Python 库。它适合在某些工具可能缺失时转换 Markdown 或制作 PDF 的人。

适用场景

  • ✓当我有一个 Markdown 或 HTML 文件需要变成 PDF 的时候。
  • ✓当 pandoc 缺失,或转换失败的时候。
  • ✓当其他工具都不可用,需要用 Python 从零制作 PDF 的时候。

技能链接

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

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

使用方法

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

可直接粘贴的消息

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

可以这样问 AI

“请把这份 Markdown 转换为 PDF。如果这里没有 pandoc,就改用 Python 库。”

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

工作原理

AI 会检查 pandoc 是否存在,存在则进行转换。否则,它会检查 reportlab,需要时安装它,最后再尝试 fpdf2。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

PDF Generation Troubleshooting Workflow

This skill provides a systematic approach to generating PDFs when multiple tools may be unavailable. Follow this fallback sequence to ensure PDF generation succeeds.

Workflow Overview

  1. Try pandoc first (simplest for markdown/HTML conversion)
  2. Fallback to reportlab (Python library for programmatic PDFs)
  3. Fallback to fpdf2 (alternative Python PDF library)

Each step includes installation verification before execution.

Step 1: Attempt pandoc Conversion

Use pandoc for markdown or HTML to PDF conversion. Check availability first:

# Check if pandoc is available
which pandoc || echo "pandoc not found"

If available, convert:

pandoc input.md -o output.pdf

If pandoc fails or is unavailable, proceed to Step 2.

Step 2: Attempt reportlab

Check Installation

python3 -c "import reportlab" 2>/dev/null && echo "reportlab available" || echo "reportlab not found"

Install if Needed

pip install reportlab

Create PDF Script

Use write_file to create a Python script:

from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas

def create_pdf(filename, content):
    c = canvas.Canvas(filename, pagesize=letter)
    c.drawString(100, 750, content)
    c.save()

if __name__ == "__main__":
    create_pdf("output.pdf", "Your content here")

Execute Script

Use run_shell with proper directory context:

cd /path/to/working/directory && python3 script.py

If reportlab fails, proceed to Step 3.

Step 3: Attempt fpdf2

Check Installation

python3 -c "from fpdf import FPDF" 2>/dev/null && echo "fpdf2 available" || echo "fpdf2 not found"

Install if Needed

pip install fpdf2

Create PDF Script

from fpdf import FPDF

def create_pdf(filename, text):
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("Arial", size=12)
    pdf.cell(200, 10, txt=text, ln=True)
    pdf.output(filename)

if __name__ == "__main__":
    create_pdf("output.pdf", "Your content here")

Execute Script

cd /path/to/working/directory && python3 script.py

Decision Matrix

Tool Best For Limitations
pandoc Markdown/HTML conversion Requires external installation
reportlab Complex layouts, graphics Steeper learning curve
fpdf2 Simple text PDFs Limited formatting options

Error Handling Pattern

For each tool attempt:

  1. Check availability/installation
  2. Install if missing (pip install <package>)
  3. Create script using write_file
  4. Execute using run_shell with cd <dir> && <command>
  5. Verify output file exists
  6. If fails, move to next tool in sequence

Example Fallback Implementation

# Pseudocode for full workflow
if command -v pandoc >/dev/null 2>&1; then
    pandoc input.md -o output.pdf && exit 0
fi

if python3 -c "import reportlab" 2>/dev/null; then
    # Create and run reportlab script
    exit 0
fi

# Final fallback: fpdf2
pip install fpdf2
# Create and run fpdf2 script

Key Principles

  • Always verify tool availability before attempting use
  • Install on-demand when a tool is missing
  • Use write_file for script creation (ensures clean, versionable code)
  • Use run_shell with explicit directory context (cd <dir> && <cmd>)
  • Progressive fallback ensures at least one method succeeds
  • Verify output after each attempt before moving to next tool
常见问题

首选是什么?

pandoc,它是处理 Markdown 或 HTML 文件最简单的选项。

pandoc 之后是什么?

reportlab,一个 Python 库;如果 reportlab 不能用,再用 fpdf2。

使用工具前会检查吗?

会。每一步都会先检查是否已安装,再运行。

详情

作者的原始描述

Systematic fallback workflow for PDF generation through pandoc, reportlab, and fpdf2 with installation verification

repo: HKUDS/AgentPilot

热度

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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能