setup.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #!/usr/bin/env bash
  2. # ---
  3. # name: pdf-setup
  4. # author: Z.AI
  5. # version: "1.0"
  6. # description: Environment setup for the PDF skill. Checks/installs CORE deps (Python + ReportLab/pypdf/PyMuPDF/pikepdf/pdfplumber + CJK font). Playwright/Chromium and Tectonic are OPTIONAL — install on demand.
  7. # ---
  8. #
  9. # Installs only dependencies required by the PDF skill.
  10. set -euo pipefail
  11. RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; BLUE='\033[0;34m'; NC='\033[0m'
  12. ok() { echo -e " ${GREEN}✓${NC} $1"; }
  13. fail() { echo -e " ${RED}✗${NC} $1"; }
  14. warn() { echo -e " ${YELLOW}○${NC} $1"; }
  15. info() { echo -e " ${BLUE}→${NC} $1"; }
  16. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  17. echo "============================================"
  18. echo " PDF Skill — Environment Setup"
  19. echo "============================================"
  20. echo ""
  21. # ── Detect platform ──
  22. OS="$(uname -s)"
  23. ARCH="$(uname -m)"
  24. echo "Platform: $OS $ARCH"
  25. echo ""
  26. # ── 0. macOS: Homebrew ──
  27. if [ "$OS" = "Darwin" ]; then
  28. echo "--- Homebrew (macOS package manager) ---"
  29. if command -v brew &>/dev/null; then
  30. BREW_VER=$(brew --version 2>/dev/null | head -1)
  31. ok "brew ($BREW_VER)"
  32. else
  33. fail "brew not found — most dependencies below need Homebrew on macOS"
  34. info "Install: /bin/bash -c \"\$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\""
  35. fi
  36. echo ""
  37. fi
  38. # ── 1. Python 3 ──
  39. echo "--- Python ---"
  40. if command -v python3 &>/dev/null; then
  41. PY_VER=$(python3 --version 2>&1)
  42. ok "python3 ($PY_VER)"
  43. # macOS: warn if using system Python
  44. if [ "$OS" = "Darwin" ]; then
  45. PY_PATH=$(which python3 2>/dev/null)
  46. if [[ "$PY_PATH" == "/usr/bin/python3" ]]; then
  47. warn "Using macOS system Python (limited). Recommend: brew install python3"
  48. fi
  49. fi
  50. else
  51. fail "python3 not found"
  52. case "$OS" in
  53. Darwin) info "Install: brew install python3" ;;
  54. Linux) info "Install: sudo apt install python3 python3-pip (Debian/Ubuntu)"
  55. info " sudo dnf install python3 python3-pip (Fedora/RHEL)" ;;
  56. *) info "Install: https://www.python.org/downloads/" ;;
  57. esac
  58. fi
  59. # ── 2. pip ──
  60. echo ""
  61. echo "--- pip ---"
  62. if python3 -m pip --version &>/dev/null 2>&1; then
  63. PIP_VER=$(python3 -m pip --version 2>/dev/null | head -1)
  64. ok "pip ($PIP_VER)"
  65. else
  66. fail "pip not found"
  67. case "$OS" in
  68. Darwin) info "Install: python3 -m ensurepip --upgrade"
  69. info " or: brew install python3 (includes pip)" ;;
  70. Linux) info "Install: sudo apt install python3-pip (Debian/Ubuntu)" ;;
  71. *) info "Install: python3 -m ensurepip --upgrade" ;;
  72. esac
  73. fi
  74. # ── 3. Python packages (pip) ──
  75. echo ""
  76. echo "--- Python Packages ---"
  77. PY_PKGS=(
  78. "pikepdf:pikepdf"
  79. "pdfplumber:pdfplumber"
  80. "pypdf:pypdf"
  81. "reportlab:reportlab"
  82. "pymupdf:PyMuPDF"
  83. )
  84. MISSING_PY=()
  85. for entry in "${PY_PKGS[@]}"; do
  86. mod="${entry%%:*}"
  87. pkg="${entry##*:}"
  88. if python3 -c "import $mod" 2>/dev/null; then
  89. ver=$(python3 -c "import $mod; print(getattr($mod, '__version__', 'installed'))" 2>/dev/null)
  90. ok "$pkg ($ver)"
  91. else
  92. fail "$pkg not installed"
  93. MISSING_PY+=("$pkg")
  94. fi
  95. done
  96. if [ ${#MISSING_PY[@]} -gt 0 ]; then
  97. echo ""
  98. if [ -t 0 ]; then
  99. read -p " Install missing Python packages? [Y/n] " -n 1 -r REPLY
  100. echo ""
  101. REPLY=${REPLY:-Y}
  102. else
  103. warn "Non-interactive mode — skipping auto-install. Run interactively or install manually."
  104. REPLY=N
  105. fi
  106. if [[ ! $REPLY =~ ^[Nn]$ ]]; then
  107. python3 -m pip install -q "${MISSING_PY[@]}" 2>/dev/null \
  108. || python3 -m pip install -q --user "${MISSING_PY[@]}" 2>/dev/null \
  109. || python3 -m pip install -q --break-system-packages "${MISSING_PY[@]}" 2>/dev/null \
  110. || { fail "pip install failed. Try manually: pip install ${MISSING_PY[*]}"; }
  111. ok "Installed: ${MISSING_PY[*]}"
  112. fi
  113. fi
  114. # ── 4. Node.js (OPTIONAL — Creative/HTML pipeline only) ──
  115. echo ""
  116. echo "--- Node.js (optional: Creative/HTML pipeline) ---"
  117. if command -v node &>/dev/null; then
  118. NODE_VER=$(node --version)
  119. ok "node ($NODE_VER)"
  120. else
  121. warn "node not found (optional — only needed for the Creative/poster/HTML→PDF route)"
  122. case "$OS" in
  123. Darwin) info "Install on demand: brew install node" ;;
  124. Linux) info "Install on demand: curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -"
  125. info " sudo apt install -y nodejs" ;;
  126. *) info "Install on demand: https://nodejs.org/" ;;
  127. esac
  128. fi
  129. # ── 5. npm (OPTIONAL) ──
  130. echo ""
  131. echo "--- npm (optional) ---"
  132. if command -v npm &>/dev/null; then
  133. NPM_VER=$(npm --version 2>/dev/null)
  134. ok "npm ($NPM_VER)"
  135. else
  136. warn "npm not found (optional — bundled with Node.js)"
  137. case "$OS" in
  138. Darwin) info "Install on demand: brew install node (includes npm)" ;;
  139. Linux) info "Install on demand: comes with nodejs" ;;
  140. *) info "Install on demand: https://nodejs.org/" ;;
  141. esac
  142. fi
  143. # ── 6. Playwright + Chromium (OPTIONAL — Creative/HTML pipeline only) ──
  144. echo ""
  145. echo "--- Playwright + Chromium (optional: HTML→PDF engine) ---"
  146. info "Optional. Covers now render with ReportLab; Playwright is only for Creative/poster/HTML→PDF."
  147. info "Install on demand, with the user's confirmation (large download / long first-run wait)."
  148. if node -e "require('playwright')" 2>/dev/null; then
  149. PW_VER=$(node -e "console.log(require('playwright/package.json').version)" 2>/dev/null)
  150. ok "playwright ($PW_VER)"
  151. else
  152. warn "playwright not installed (optional)"
  153. info "Install on demand: npm install -g playwright"
  154. fi
  155. # Check Chromium
  156. if [ "$OS" = "Darwin" ]; then
  157. PW_CACHE="$HOME/Library/Caches/ms-playwright"
  158. else
  159. PW_CACHE="$HOME/.cache/ms-playwright"
  160. fi
  161. if ls "$PW_CACHE"/chromium-* &>/dev/null 2>&1; then
  162. CR_DIR=$(ls -d "$PW_CACHE"/chromium-* 2>/dev/null | tail -1)
  163. ok "chromium ($(basename "$CR_DIR"))"
  164. else
  165. warn "chromium not installed (optional)"
  166. info "Install on demand: npx playwright install chromium"
  167. if [ "$OS" = "Linux" ]; then
  168. info " npx playwright install-deps (system libs, needs sudo)"
  169. fi
  170. fi
  171. # ── 7. Tectonic (LaTeX engine, optional) ──
  172. echo ""
  173. echo "--- Tectonic (LaTeX→PDF, optional) ---"
  174. BUNDLED="$SCRIPT_DIR/tectonic"
  175. if [ -x "$BUNDLED" ]; then
  176. if [ "$OS" = "Darwin" ] && [ "$ARCH" = "arm64" ]; then
  177. ok "tectonic (bundled, macOS arm64)"
  178. else
  179. warn "bundled tectonic is macOS arm64 only — cannot run on $OS $ARCH"
  180. if command -v tectonic &>/dev/null; then
  181. TEC_VER=$(tectonic --version 2>&1 | head -1)
  182. ok "tectonic (system: $TEC_VER)"
  183. else
  184. fail "tectonic not in PATH"
  185. case "$OS" in
  186. Darwin) info "Install: brew install tectonic" ;;
  187. Linux) info "Install: conda install -c conda-forge tectonic"
  188. info " or: curl -fsSL https://drop-sh.fullyjustified.net | sh" ;;
  189. MINGW*|MSYS*|CYGWIN*) info "Install: scoop install tectonic / choco install tectonic" ;;
  190. esac
  191. fi
  192. fi
  193. elif command -v tectonic &>/dev/null; then
  194. TEC_VER=$(tectonic --version 2>&1 | head -1)
  195. ok "tectonic ($TEC_VER)"
  196. elif [ -x "$HOME/tectonic" ]; then
  197. ok "tectonic (~/tectonic)"
  198. else
  199. warn "tectonic not installed (needed only for LaTeX/academic PDFs)"
  200. case "$OS" in
  201. Darwin) info "Install: brew install tectonic" ;;
  202. Linux) info "Install: conda install -c conda-forge tectonic"
  203. info " or: curl -fsSL https://drop-sh.fullyjustified.net | sh" ;;
  204. MINGW*|MSYS*|CYGWIN*) info "Install: scoop install tectonic / choco install tectonic" ;;
  205. esac
  206. fi
  207. # ── 8. LibreOffice (install-on-demand, NOT substitutable) ──
  208. echo ""
  209. echo "--- LibreOffice (Office→PDF) ---"
  210. if command -v soffice &>/dev/null; then
  211. LO_VER=$(soffice --version 2>/dev/null | head -1)
  212. ok "libreoffice ($LO_VER)"
  213. else
  214. warn "libreoffice not installed — REQUIRED for any .docx/.xlsx/.pptx→PDF task"
  215. info "You MUST install it. FORBIDDEN: using Word/WPS/Keynote/Pages instead of"
  216. info "installing. A large download is NOT a reason to skip -- it is expected to be"
  217. info "large. REQUIRED: explain the necessity to the user, say the package is large,"
  218. info "THEN install it."
  219. info "PREFERRED source (fast in China, current build): Tsinghua TUNA mirror"
  220. info " https://mirrors.tuna.tsinghua.edu.cn/libreoffice/libreoffice/stable/26.8.0/"
  221. info " Pick the subdir for this platform/arch ($(uname -m)):"
  222. info " Linux deb -> deb/x86_64/ or deb/aarch64/ (tar -xzf, then dpkg -i */DEBS/*.deb)"
  223. info " Linux rpm -> rpm/x86_64/"
  224. info " macOS -> mac/aarch64/ (Apple Silicon) or mac/x86_64/ (Intel) (.dmg)"
  225. info " If 26.8.0 is gone, pick the newest version under .../libreoffice/stable/"
  226. info "Package-manager FALLBACK (only if the mirror is unreachable):"
  227. case "$OS" in
  228. Darwin) info " brew install --cask libreoffice" ;;
  229. Linux) info " sudo apt install libreoffice-core (Debian/Ubuntu)" ;;
  230. esac
  231. info "AFTER installing, REGISTER the binary on PATH -- the method differs per OS:"
  232. case "$OS" in
  233. Darwin) info " sudo ln -sf /Applications/LibreOffice.app/Contents/MacOS/soffice /usr/local/bin/soffice"
  234. info " (Apple Silicon: use /opt/homebrew/bin/soffice if /usr/local/bin is not on PATH)" ;;
  235. Linux) info " sudo ln -sf /opt/libreoffice*/program/soffice /usr/local/bin/soffice" ;;
  236. esac
  237. info " then re-verify: soffice --version"
  238. info "An install left off PATH looks 'not installed' next run and gets reinstalled"
  239. info "needlessly. If soffice already exists on disk but is off PATH, just link it."
  240. info "Another program is allowed ONLY after an install attempt has FAILED. Report it."
  241. info "No install attempt = no substitute program."
  242. fi
  243. # ── 9. CJK Fonts ──
  244. echo ""
  245. echo "--- CJK Fonts ---"
  246. FONT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)/fonts"
  247. if [ -d "$FONT_DIR" ]; then
  248. FONT_COUNT=$(find "$FONT_DIR" -name "*.ttf" -o -name "*.otf" 2>/dev/null | head -20 | wc -l | tr -d ' ')
  249. ok "fonts directory ($FONT_COUNT font files in $FONT_DIR)"
  250. else
  251. warn "no fonts/ directory found — CJK PDFs may have missing glyphs"
  252. info "Expected at: $FONT_DIR"
  253. fi
  254. # Check system CJK fonts
  255. if [ "$OS" = "Darwin" ]; then
  256. if ls /System/Library/Fonts/PingFang.ttc &>/dev/null 2>&1 \
  257. || ls /System/Library/Fonts/STHeiti*.ttc &>/dev/null 2>&1 \
  258. || ls "$HOME/Library/Fonts/"*SimHei* &>/dev/null 2>&1; then
  259. ok "macOS CJK system fonts available"
  260. else
  261. warn "no common CJK system fonts found"
  262. fi
  263. elif [ "$OS" = "Linux" ]; then
  264. if fc-list :lang=zh 2>/dev/null | head -1 | grep -q .; then
  265. ok "system CJK fonts available (fc-list)"
  266. else
  267. warn "no CJK fonts found. Install: sudo apt install fonts-noto-cjk"
  268. fi
  269. fi
  270. # ── Summary ──
  271. echo ""
  272. echo "============================================"
  273. echo " Setup complete."
  274. echo " Run 'python3 pdf.py env.check' for detailed status."
  275. echo " Run 'python3 pdf.py env.fix' to auto-install Python deps."
  276. echo "============================================"