Skip to content

fix: use Path.as_uri() in render_pdf.py for proper file URL escaping#87

Merged
ShadyUnderLight merged 1 commit into
mainfrom
fix/render-pdf-file-uri-escaping
May 11, 2026
Merged

fix: use Path.as_uri() in render_pdf.py for proper file URL escaping#87
ShadyUnderLight merged 1 commit into
mainfrom
fix/render-pdf-file-uri-escaping

Conversation

@ShadyUnderLight
Copy link
Copy Markdown
Owner

问题

Closes #81render_pdf.py 手拼 file:// URL 未做 URI 转义,路径含 #?、中文等字符时 Playwright 会误判为 fragment/query,返回 net::ERR_FILE_NOT_FOUND

修改

scripts/render_pdf.py

  1. file_url = f"file://{html_path}"html_path.as_uri()
    Path.as_uri() 是标准库方法,按 RFC 8089 正确编码 #%23?%3F、空格 → %20 等。
  2. browser 生命周期加 try/finally
    原结构在 page.goto() 抛错时跳过了 browser.close()。改为 try/finally 确保异常路径也能关闭 Chromium 进程。

.github/workflows/ci.yml

smoke job 末尾新增一步:创建含 # 和空格的路径,运行完整 md_to_pdf.py pipeline,验证 PDF 正确生成。

不改的

  • 不改 md_to_pdf.py(子进程 argv 传参已正确处理特殊字符)
  • 不改 test_remote_block.py / README.md / SKILL.md
  • 不新增独立测试文件

测试

python3 -m py_compile scripts/*.py  # OK
python3 scripts/test_*              # 全部 PASS

CI smoke 会覆盖特殊字符路径集成测试。

- Replace hand-stitched f"file://{html_path}" with html_path.as_uri()
  so #, ?, Chinese characters, and other URL-special chars in file paths
  are properly encoded (closes #81).
- Wrap browser lifecycle in try/finally so page.goto() failure doesn't
  leak the Chromium process.
- Add CI smoke test covering paths with # and spaces.
@ShadyUnderLight ShadyUnderLight force-pushed the fix/render-pdf-file-uri-escaping branch from 2f2bbb8 to d2d45fb Compare May 11, 2026 10:16
@ShadyUnderLight ShadyUnderLight merged commit a844d35 into main May 11, 2026
2 checks passed
@ShadyUnderLight ShadyUnderLight deleted the fix/render-pdf-file-uri-escaping branch May 11, 2026 10:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

修复 render_pdf.py 手拼 file:// URL 导致特殊字符路径渲染失败

1 participant