
本文旨在解决readthedocs平台中,自定义pdf构建成功但无法通过侧边菜单下载(显示404错误)的问题。核心在于指导用户如何正确配置`.readthedocs.yml`文件,确保自定义生成的pdf文件能被readthedocs识别并正确链接。通过重命名pdf文件为项目特定名称,并将其放置在指定路径,可以有效解决此问题,确保用户能够顺利下载文档的pdf版本。
在使用ReadTheDocs托管Sphinx项目时,开发者经常需要提供PDF版本的文档供用户下载。虽然ReadTheDocs默认支持生成PDF,但有时为了实现特定的样式或功能(例如使用sphinx-simplepdf等扩展),我们会选择自定义PDF的构建过程。然而,一个常见的问题是,即使自定义构建过程在ReadTheDocs上显示成功,用户点击侧边菜单中的PDF下载选项时,却可能遇到“404 Not Found”错误。这表明ReadTheDocs未能找到或正确链接到我们自定义生成的PDF文件。
ReadTheDocs在提供下载链接时,通常会查找一个特定命名和位置的PDF文件。当自定义构建流程完成后,如果生成的PDF文件名或存放路径不符合ReadTheDocs的预期,即使文件实际存在于构建环境中,也无法通过其标准的下载机制访问。通常,ReadTheDocs期望在_readthedocs/pdf/目录下找到一个以项目名称命名的PDF文件,例如_readthedocs/pdf/$READTHEDOCS_PROJECT.pdf。
解决此问题的关键在于确保自定义生成的PDF文件被正确命名,并放置在ReadTheDocs期望的位置。这可以通过在.readthedocs.yml配置文件中的commands部分添加一个重命名步骤来实现。
以下是详细的配置步骤和示例:
首先,确保你的构建环境已准备好,并且所有必要的Python依赖项都已安装。这通常通过pip install -r docs/requirements.txt命令完成。
# .readthedocs.yaml
version: 2
build:
os: ubuntu-20.04
tools:
python: "3.9"
commands:
# 安装文档构建所需的Python依赖
- pip install -r docs/requirements.txt
# ... 其他命令 ...在每次构建之前,清理并创建用于存放自定义PDF的目录是一个好习惯,以避免旧文件干扰。
# .readthedocs.yaml
# ...
commands:
- pip install -r docs/requirements.txt
# 清理旧的PDF目录
- rm -rf _readthedocs/pdf
# 创建新的PDF目录
- mkdir -p _readthedocs/pdf
# ...使用你选择的Sphinx构建器(例如simplepdf)来生成PDF。确保输出路径指向我们准备好的_readthedocs/pdf目录。
# .readthedocs.yaml
# ...
commands:
- pip install -r docs/requirements.txt
- rm -rf _readthedocs/pdf
- mkdir -p _readthedocs/pdf
# 使用simplepdf构建器生成PDF
- sphinx-build -b simplepdf docs _readthedocs/pdf
# ...如果你的自定义PDF构建过程在输出目录中生成了除了.pdf文件之外的其他文件或子目录,可以使用find命令清理它们,确保目录只包含PDF文件。
# .readthedocs.yaml
# ...
commands:
- pip install -r docs/requirements.txt
- rm -rf _readthedocs/pdf
- mkdir -p _readthedocs/pdf
- sphinx-build -b simplepdf docs _readthedocs/pdf
# 删除_readthedocs/pdf中除.pdf文件外的所有文件
- find _readthedocs/pdf -type f ! -name '*.pdf' -delete
# 删除_readthedocs/pdf中所有子目录
- find _readthedocs/pdf -mindepth 1 -type d -delete
# ...通常,你还需要构建HTML版本的文档。
# .readthedocs.yaml
# ...
commands:
- pip install -r docs/requirements.txt
- rm -rf _readthedocs/pdf
- mkdir -p _readthedocs/pdf
- sphinx-build -b simplepdf docs _readthedocs/pdf
- find _readthedocs/pdf -type f ! -name '*.pdf' -delete
- find _readthedocs/pdf -mindepth 1 -type d -delete
# 创建HTML输出目录
- mkdir -p _readthedocs/html/
# 构建HTML文档
- sphinx-build -b html docs _readthedocs/html
# ...这是解决404问题的关键步骤。将自定义构建生成的PDF文件重命名为${READTHEDOCS_PROJECT}.pdf。$READTHEDOCS_PROJECT是一个环境变量,在ReadTheDocs构建环境中会自动设置为你的项目名称。
# .readthedocs.yaml
# ...
commands:
- pip install -r docs/requirements.txt
- rm -rf _readthedocs/pdf
- mkdir -p _readthedocs/pdf
- sphinx-build -b simplepdf docs _readthedocs/pdf
- find _readthedocs/pdf -type f ! -name '*.pdf' -delete
- find _readthedocs/pdf -mindepth 1 -type d -delete
- mkdir -p _readthedocs/html/
- sphinx-build -b html docs _readthedocs/html
# 重命名生成的PDF文件为项目名称,并放置在正确的目录下
- mv _readthedocs/pdf/*.pdf _readthedocs/pdf/$READTHEDOCS_PROJECT.pdf结合以上所有步骤,一个完整的、可解决自定义PDF 404问题的.readthedocs.yml文件可能如下所示:
# .readthedocs.yaml
# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
# Required
version: 2
build:
os: ubuntu-20.04
tools:
python: "3.9"
commands:
# 1. 安装文档构建所需的Python依赖
- pip install -r docs/requirements.txt
# 2. 清理并创建PDF输出目录
- rm -rf _readthedocs/pdf
- mkdir -p _readthedocs/pdf
# 3. 使用simplepdf构建器生成自定义PDF
- sphinx-build -b simplepdf docs _readthedocs/pdf
# 4. 清理PDF目录,只保留.pdf文件
- find _readthedocs/pdf -type f ! -name '*.pdf' -delete
- find _readthedocs/pdf -mindepth 1 -type d -delete
# 5. 创建HTML输出目录并构建HTML文档
- mkdir -p _readthedocs/html/
- sphinx-build -b html docs _readthedocs/html
# 6. 核心步骤:将生成的PDF重命名为项目名称,并放置在正确的目录下
- mv _readthedocs/pdf/*.pdf _readthedocs/pdf/$READTHEDOCS_PROJECT.pdf
# Build documentation in the docs/ directory with Sphinx
sphinx:
configuration: docs/conf.py
# If using Sphinx, optionally build your docs in additional formats such as PDF
# 这里的formats: all确保ReadTheDocs会尝试处理所有格式,包括自定义的
formats: all
# Optionally declare the Python requirements required to build your docs
python:
install:
- requirements: docs/requirements.txt通过在.readthedocs.yml的commands部分添加一个简单的mv命令,将自定义生成的PDF文件重命名为${READTHEDOCS_PROJECT}.pdf并放置在_readthedocs/pdf/目录下,可以有效地解决ReadTheDocs侧边菜单PDF下载404的问题。这个解决方案确保了自定义构建的灵活性,同时满足了ReadTheDocs对PDF文件命名和路径的预期,从而为用户提供无缝的文档下载体验。
以上就是解决ReadTheDocs自定义PDF在菜单中404错误的教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号