Reliable PDF generation using shell-based Python execution when sandbox fails
复制下面这句话,粘贴给 Claude Code、Codex、Cursor 等 AI 编程工具,它会读取安装说明并在你确认后完成安装。
请阅读 https://ai.atlankj.com/install/asset/gh-pdf-gen-fallback-cdd967782164 ,按照其中的说明把「pdf-gen-fallback」安装到你(当前 AI 工具)中。执行前先告诉我将运行的命令和写入的位置,等我确认。
查看 AI 将读取的安装说明正在读取 GitHub 原文…
内容来自 GitHub 原始文件,由原作者维护。在 GitHub 查看
This skill provides a reliable workflow for generating PDFs and documents when execute_code_sandbox returns opaque errors. Use direct run_shell with python -c as a fallback, with library availability checks and explicit error handling.
execute_code_sandbox fails with cryptic error messages during document/PDF generationBefore attempting generation, verify that required libraries are installed:
run_shell command="python -c \"import reportlab; print('reportlab available')\""
run_shell command="python -c \"import fpdf; print('fpdf available')\""
run_shell command="python -c \"import pypdf; print('pypdf available')\""
Try the sandbox approach initially (it's cleaner for iteration):
execute_code_sandbox code="""
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("ARTIFACT_PATH:/path/to/output.pdf")
"""
Watch for these failure indicators:
If sandbox fails, use direct shell execution:
run_shell command="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 created successfully')
\""
After generation, confirm the file exists:
run_shell command="ls -la output.pdf && file output.pdf"
1. Check libraries: run_shell "python -c \"import reportlab\""
2. Try sandbox: execute_code_sandbox with PDF generation code
3. If error detected: Fall back to run_shell with python -c
4. Verify: run_shell "ls -la *.pdf"
5. Read result: read_file filetype="pdf" file_path="output.pdf"
run_shell command="pip install reportlab"If reportlab is unavailable, try:
fpdf - Simpler API, good for basic PDFsweasyprint - HTML/CSS to PDF conversionpypdf - PDF manipulation and merging