AirTransmissionLibrary/docs/generate_pdf.md

3.2 KiB
Raw Blame History

生成 API 文档 PDF 指南

本文档说明如何将 API 文档转换为 PDF 格式。

前提条件

  1. 安装 DocFX

    dotnet tool install -g docfx
    
  2. 安装 wkhtmltopdf 从官方网站下载并安装:https://wkhtmltopdf.org/downloads.html

生成步骤

  1. 首先生成 API 文档的 HTML 文件:

    docfx build docfx.json
    

    这会在 _site 目录下生成 HTML 文档。

  2. 使用 wkhtmltopdf 将 HTML 文件转换为 PDF

    wkhtmltopdf --enable-local-file-access \
    _site/api/AirTransmission.html \
    _site/api/AirTransmission.WeatherType.html \
    _site/api/AirTransmission.WeatherCondition.html \
    _site/api/AirTransmission.AtmosphericTransmittanceCalculator.html \
    api_doc.pdf
    

PDF 格式优化选项

如果需要优化 PDF 输出效果,可以添加以下 wkhtmltopdf 参数:

  1. 页面大小和方向:

    --page-size A4 --orientation Portrait
    
  2. 页边距(单位:毫米):

    --margin-top 20 --margin-bottom 20 --margin-left 20 --margin-right 20
    
  3. 添加页眉页脚:

    --header-center "[page]/[topage]" \
    --header-line \
    --footer-center "大气传输计算库 API 文档" \
    --footer-line \
    --footer-spacing 10 \
    --footer-font-size 8
    
  4. 封面和目录:

    cover _site/docs/introduction.html \
    toc --toc-header-text "目录"
    
  5. 自定义样式:

    --user-style-sheet docs/custom.css
    

    自定义样式文件 custom.css 的内容:

    /* 隐藏目录切换按钮 */
    .sideaffix {
        display: none !important;
    }
    
    /* 隐藏导航按钮 */
    .mobile-hide {
        display: none !important;
    }
    
    /* 隐藏顶部导航栏 */
    .navbar {
        display: none !important;
    }
    
    /* 隐藏面包屑导航 */
    .subnav {
        display: none !important;
    }
    
    /* 移除文档内容的左边距 */
    .article {
        margin-left: 0 !important;
    }
    
    /* 隐藏页脚 */
    .footer {
        display: none !important;
    }
    

完整的优化命令示例:

wkhtmltopdf \
  --enable-local-file-access \
  --page-size A4 \
  --orientation Portrait \
  --margin-top 20 \
  --margin-bottom 20 \
  --margin-left 20 \
  --margin-right 20 \
  --header-center "[page]/[topage]" \
  --header-line \
  --footer-center "大气传输计算库 API 文档" \
  --footer-line \
  --footer-spacing 10 \
  --footer-font-size 8 \
  --user-style-sheet docs/custom.css \
  cover _site/docs/introduction.html \
  toc --toc-header-text "目录" \
  _site/docs/usage_examples.html \
  _site/api/AirTransmission.html \
  _site/api/AirTransmission.WeatherType.html \
  _site/api/AirTransmission.WeatherCondition.html \
  _site/api/AirTransmission.AtmosphericTransmittanceCalculator.html \
  api_doc.pdf

注意事项

  1. 确保所有的 HTML 文件都已经正确生成
  2. 如果遇到路径问题,请使用完整的文件路径
  3. 如果文档包含中文,确保系统安装了相应的中文字体
  4. 生成的 PDF 文件默认保存在当前目录下
  5. 封面页不会显示页眉和页脚
  6. 自定义样式可以隐藏不需要的 HTML 元素