setup_windows.ps1 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #Requires -Version 5.1
  2. <#
  3. .SYNOPSIS
  4. PDF Skill — Environment Setup for Windows (Win10/Win11)
  5. .DESCRIPTION
  6. Detects platform, checks and installs all dependencies for the PDF skill.
  7. Supports China mirror fallback for pip/npm/playwright.
  8. #>
  9. param(
  10. [switch]$UseChinaMirror
  11. )
  12. $ErrorActionPreference = "Continue"
  13. function Write-Ok { param($msg) Write-Host " [OK] $msg" -ForegroundColor Green }
  14. function Write-Fail { param($msg) Write-Host " [FAIL] $msg" -ForegroundColor Red }
  15. function Write-Warn { param($msg) Write-Host " [WARN] $msg" -ForegroundColor Yellow }
  16. function Write-Info { param($msg) Write-Host " [->] $msg" -ForegroundColor Cyan }
  17. # ── Resolve PDF_SKILL_DIR ──
  18. $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
  19. $PDF_SKILL_DIR = Split-Path -Parent $ScriptDir
  20. $env:PDF_SKILL_DIR = $PDF_SKILL_DIR
  21. Write-Host "============================================"
  22. Write-Host " PDF Skill - Environment Setup"
  23. Write-Host " (Windows)"
  24. Write-Host "============================================"
  25. Write-Host ""
  26. # ── Step 1: Platform Detection ──
  27. $WinVer = [System.Environment]::OSVersion.Version
  28. $WinName = if ($WinVer.Build -ge 22000) { "Windows 11" } elseif ($WinVer.Build -ge 10240) { "Windows 10" } else { "Windows (older)" }
  29. Write-Host "Platform: $WinName (Build $($WinVer.Build)), $([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)"
  30. Write-Host "PDF_SKILL_DIR=$PDF_SKILL_DIR"
  31. Write-Host ""
  32. # ── China mirror detection ──
  33. $PipMirrorArgs = @()
  34. $NpmMirrorArgs = @()
  35. $PlaywrightHost = ""
  36. if ($UseChinaMirror) {
  37. $global:UseCN = $true
  38. } else {
  39. try {
  40. $null = Invoke-WebRequest -Uri "https://pypi.org" -TimeoutSec 3 -UseBasicParsing -ErrorAction Stop
  41. $global:UseCN = $false
  42. } catch {
  43. Write-Warn "pypi.org unreachable - enabling China mirrors"
  44. $global:UseCN = $true
  45. }
  46. }
  47. if ($global:UseCN) {
  48. $PipMirrorArgs = @("-i", "https://pypi.tuna.tsinghua.edu.cn/simple", "--trusted-host", "pypi.tuna.tsinghua.edu.cn")
  49. $NpmMirrorArgs = @("--registry", "https://registry.npmmirror.com")
  50. $PlaywrightHost = "https://npmmirror.com/mirrors/playwright/"
  51. Write-Info "China mirrors enabled (pip: tuna, npm: npmmirror, playwright: npmmirror)"
  52. Write-Host ""
  53. }
  54. $Errors = 0
  55. # ── Step 2a: Python 3 ──
  56. Write-Host "--- [1/9] Python 3 ---"
  57. $PyCmd = $null
  58. foreach ($cmd in @("python3", "python", "py")) {
  59. try {
  60. $ver = & $cmd --version 2>&1
  61. if ($ver -match "Python 3") {
  62. $PyCmd = $cmd
  63. Write-Ok "$cmd ($ver)"
  64. break
  65. }
  66. } catch {}
  67. }
  68. if (-not $PyCmd) {
  69. Write-Fail "Python 3 not found"
  70. Write-Info "Install option 1: winget install Python.Python.3.11"
  71. Write-Info "Install option 2: https://www.python.org/downloads/"
  72. Write-Info "Install option 3: choco install python3"
  73. if ($global:UseCN) {
  74. Write-Info "China alt: https://npmmirror.com/mirrors/python/"
  75. }
  76. $Errors++
  77. }
  78. Write-Host ""
  79. # ── Step 2b: pip ──
  80. Write-Host "--- [2/9] pip ---"
  81. if ($PyCmd) {
  82. try {
  83. $pipVer = & $PyCmd -m pip --version 2>&1
  84. if ($pipVer -match "pip") {
  85. Write-Ok "pip ($pipVer)"
  86. } else { throw "no pip" }
  87. } catch {
  88. Write-Fail "pip not found"
  89. Write-Info "Install: $PyCmd -m ensurepip --upgrade"
  90. $Errors++
  91. }
  92. } else {
  93. Write-Fail "pip - skipped (Python not found)"
  94. $Errors++
  95. }
  96. Write-Host ""
  97. # ── Step 2c: Python packages ──
  98. Write-Host "--- [3/9] Python Packages (pikepdf, pdfplumber, pypdf, reportlab, PyMuPDF) ---"
  99. $PyPkgs = @(
  100. @{ Module = "pikepdf"; Package = "pikepdf" },
  101. @{ Module = "pdfplumber"; Package = "pdfplumber" },
  102. @{ Module = "pypdf"; Package = "pypdf" },
  103. @{ Module = "reportlab"; Package = "reportlab" },
  104. @{ Module = "fitz"; Package = "PyMuPDF" }
  105. )
  106. $MissingPy = @()
  107. if ($PyCmd) {
  108. foreach ($pkg in $PyPkgs) {
  109. try {
  110. $result = & $PyCmd -c "import $($pkg.Module); print(getattr($($pkg.Module), '__version__', getattr($($pkg.Module), 'version', 'ok')))" 2>&1
  111. if ($LASTEXITCODE -eq 0) {
  112. Write-Ok "$($pkg.Package) ($result)"
  113. } else { throw "not installed" }
  114. } catch {
  115. Write-Fail "$($pkg.Package) not installed"
  116. $MissingPy += $pkg.Package
  117. }
  118. }
  119. if ($MissingPy.Count -gt 0) {
  120. Write-Info "Installing: $($MissingPy -join ', ')"
  121. $installArgs = @("-m", "pip", "install") + $PipMirrorArgs + $MissingPy
  122. try {
  123. & $PyCmd @installArgs 2>&1 | Out-Null
  124. Write-Ok "Installed: $($MissingPy -join ', ')"
  125. } catch {
  126. Write-Fail "pip install failed. Try: $PyCmd -m pip install $($PipMirrorArgs -join ' ') $($MissingPy -join ' ')"
  127. $Errors++
  128. }
  129. }
  130. } else {
  131. Write-Fail "Python packages - skipped (Python not found)"
  132. }
  133. Write-Host ""
  134. # ── Step 2d: Node.js + npm (OPTIONAL — Creative/HTML pipeline only) ──
  135. Write-Host "--- [4/9] Node.js + npm (optional: Creative/HTML pipeline) ---"
  136. try {
  137. $nodeVer = & node --version 2>&1
  138. if ($LASTEXITCODE -eq 0) {
  139. Write-Ok "node ($nodeVer)"
  140. } else { throw "no node" }
  141. } catch {
  142. Write-Warn "node not found (optional — only for the Creative/poster/HTML->PDF route)"
  143. Write-Info "Install on demand 1: winget install OpenJS.NodeJS.LTS"
  144. Write-Info "Install on demand 2: https://nodejs.org/"
  145. Write-Info "Install on demand 3: choco install nodejs-lts"
  146. if ($global:UseCN) {
  147. Write-Info "China alt: https://npmmirror.com/mirrors/node/"
  148. }
  149. }
  150. try {
  151. $npmVer = & npm --version 2>&1
  152. if ($LASTEXITCODE -eq 0) {
  153. Write-Ok "npm ($npmVer)"
  154. } else { throw "no npm" }
  155. } catch {
  156. Write-Warn "npm not found (optional — installed with Node.js)"
  157. }
  158. Write-Host ""
  159. # ── Step 2e: Playwright + Chromium (OPTIONAL — Creative/HTML pipeline only) ──
  160. Write-Host "--- [5/9] Playwright + Chromium (optional: HTML->PDF engine) ---"
  161. Write-Info "Optional. Covers now render with ReportLab; Playwright is only for Creative/poster/HTML->PDF."
  162. Write-Info "Install on demand, with the user's confirmation (large download / long first-run wait)."
  163. try {
  164. $null = & node -e "require('playwright')" 2>&1
  165. if ($LASTEXITCODE -eq 0) {
  166. $pwVer = & node -e "console.log(require('playwright/package.json').version)" 2>&1
  167. Write-Ok "playwright ($pwVer)"
  168. } else { throw "no pw" }
  169. } catch {
  170. Write-Warn "playwright not installed (optional)"
  171. if ($global:UseCN) {
  172. Write-Info "Install on demand: npm install -g playwright@1.50.0 $($NpmMirrorArgs -join ' ')"
  173. } else {
  174. Write-Info "Install on demand: npm install -g playwright@1.50.0"
  175. }
  176. }
  177. $PwCache = Join-Path $env:LOCALAPPDATA "ms-playwright"
  178. if (Test-Path "$PwCache\chromium-*") {
  179. Write-Ok "chromium installed"
  180. } else {
  181. Write-Warn "chromium not installed (optional)"
  182. if ($global:UseCN) {
  183. Write-Info "Install on demand: `$env:PLAYWRIGHT_DOWNLOAD_HOST='$PlaywrightHost'; npx playwright install chromium"
  184. } else {
  185. Write-Info "Install on demand: npx playwright install chromium"
  186. }
  187. }
  188. Write-Host ""
  189. # ── Step 2f: Tectonic (optional) ──
  190. Write-Host "--- [6/9] Tectonic (optional, LaTeX/Academic PDFs) ---"
  191. try {
  192. $tecVer = & tectonic --version 2>&1
  193. if ($LASTEXITCODE -eq 0) {
  194. Write-Ok "tectonic ($tecVer)"
  195. } else { throw "no tectonic" }
  196. } catch {
  197. Write-Warn "tectonic not installed (needed only for LaTeX/academic PDFs)"
  198. Write-Info "Install option 1: scoop install tectonic"
  199. Write-Info "Install option 2: choco install tectonic"
  200. }
  201. Write-Host ""
  202. # ── Step 2g: LibreOffice (install-on-demand, NOT substitutable) ──
  203. Write-Host "--- [7/9] LibreOffice (Office->PDF conversion) ---"
  204. $SofficePath = "C:\Program Files\LibreOffice\program\soffice.exe"
  205. if (Test-Path $SofficePath) {
  206. Write-Ok "libreoffice found"
  207. } elseif (Get-Command soffice -ErrorAction SilentlyContinue) {
  208. Write-Ok "libreoffice (in PATH)"
  209. } else {
  210. Write-Warn "libreoffice not installed - REQUIRED for any .docx/.xlsx/.pptx -> PDF task"
  211. Write-Info "You MUST install it. FORBIDDEN: using Word/WPS/Keynote/Pages instead of"
  212. Write-Info "installing. A large download is NOT a reason to skip - it is expected to be large."
  213. Write-Info "REQUIRED: explain the necessity to the user, say the package is large, THEN install."
  214. Write-Info "PREFERRED source (fast in China, current build): Tsinghua TUNA mirror"
  215. Write-Info " https://mirrors.tuna.tsinghua.edu.cn/libreoffice/libreoffice/stable/26.8.0/"
  216. Write-Info " Windows x64: win/x86_64/LibreOffice_26.8.0_Win_x86-64.msi"
  217. Write-Info " Windows ARM64: win/aarch64/LibreOffice_26.8.0_Win_aarch64.msi"
  218. Write-Info " Then: msiexec /i <file>.msi /qn (needs admin)"
  219. Write-Info " If 26.8.0 is gone, pick the newest version under .../libreoffice/stable/"
  220. Write-Info "FALLBACK (only if the mirror is unreachable):"
  221. Write-Info " winget install TheDocumentFoundation.LibreOffice"
  222. Write-Info " or: https://www.libreoffice.org/download/"
  223. Write-Info "AFTER installing, REGISTER it on PATH (Windows method - differs per OS):"
  224. Write-Info ' setx PATH "$([Environment]::GetEnvironmentVariable(''PATH'',''User''));C:\Program Files\LibreOffice\program"'
  225. Write-Info " setx affects NEW shells only - for this session use the full soffice.exe path."
  226. Write-Info ' then re-verify: & "C:\Program Files\LibreOffice\program\soffice.exe" --version'
  227. Write-Info "An install left off PATH looks 'not installed' next run and gets reinstalled"
  228. Write-Info "needlessly. If soffice already exists on disk but is off PATH, just register it."
  229. Write-Info "Another program is allowed ONLY after an install attempt has FAILED. Report it."
  230. Write-Info "No install attempt = no substitute program."
  231. }
  232. Write-Host ""
  233. # ── Step 2h: Font Installation (from CDN) ──
  234. Write-Host "--- [8/9] Font Installation ---"
  235. $FontCdnBase = "https://z-cdn.chatglm.cn/office-skill/fonts"
  236. $FontList = Join-Path $ScriptDir "font_list.txt"
  237. $UserFontDir = Join-Path $env:LOCALAPPDATA "Microsoft\Windows\Fonts"
  238. if (-not (Test-Path $UserFontDir)) { New-Item -ItemType Directory -Path $UserFontDir -Force | Out-Null }
  239. $Marker = Join-Path $UserFontDir ".office-skill-fonts-installed"
  240. if (Test-Path $Marker) {
  241. Write-Ok "Fonts already installed (marker found). To re-install, delete $Marker"
  242. } else {
  243. if (-not (Test-Path $FontList)) {
  244. Write-Fail "Font list not found: $FontList"
  245. $Errors++
  246. } else {
  247. $lines = Get-Content $FontList | Where-Object { $_.Trim() -ne "" }
  248. $Total = $lines.Count
  249. $Installed = 0; $Skipped = 0; $Failed = 0
  250. Write-Info "Downloading $Total fonts from CDN..."
  251. foreach ($relPath in $lines) {
  252. $fname = Split-Path $relPath -Leaf
  253. $dest = Join-Path $UserFontDir $fname
  254. if (Test-Path $dest) {
  255. $Skipped++
  256. continue
  257. }
  258. $encoded = $relPath -replace '\[','%5B' -replace '\]','%5D' -replace ' ','%20'
  259. $url = "$FontCdnBase/$encoded"
  260. try {
  261. Invoke-WebRequest -Uri $url -OutFile $dest -TimeoutSec 30 -ErrorAction Stop
  262. $regPath = "HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"
  263. $null = New-ItemProperty -Path $regPath -Name $fname -Value $dest -PropertyType String -Force -ErrorAction SilentlyContinue
  264. $Installed++
  265. } catch {
  266. Write-Warn "Failed to download: $relPath"
  267. $Failed++
  268. Remove-Item $dest -Force -ErrorAction SilentlyContinue
  269. }
  270. }
  271. if ($Failed -eq 0) {
  272. New-Item -ItemType File -Path $Marker -Force | Out-Null
  273. Write-Ok "Fonts: $Installed newly installed, $Skipped already present (target: $UserFontDir)"
  274. } else {
  275. Write-Warn "Fonts: $Installed installed, $Skipped skipped, $Failed failed (marker not written, will retry next run)"
  276. $Errors++
  277. }
  278. }
  279. }
  280. # Set PDF_FONTS_DIR to user font directory (fonts are now installed there)
  281. $PDF_FONTS_DIR = $UserFontDir
  282. $env:PDF_FONTS_DIR = $PDF_FONTS_DIR
  283. Write-Host ""
  284. # ── Step 2i: CJK Font Verification ──
  285. Write-Host "--- [9/9] CJK Font Verification ---"
  286. $CjkFound = $false
  287. $FontsDir = Join-Path $env:WINDIR "Fonts"
  288. $UserFontDir2 = Join-Path $env:LOCALAPPDATA "Microsoft\Windows\Fonts"
  289. $CjkFonts = @("NotoSansSC[wght].ttf", "NotoSansSC[wght].ttf", "NotoSansSC[wght].ttf", "NotoSerifSC-Regular.ttf")
  290. foreach ($f in $CjkFonts) {
  291. if ((Test-Path (Join-Path $FontsDir $f)) -or (Test-Path (Join-Path $UserFontDir2 $f))) {
  292. Write-Ok "CJK font found: $f"
  293. $CjkFound = $true
  294. break
  295. }
  296. }
  297. if (-not $CjkFound) {
  298. Write-Warn "No CJK font verified yet - fonts were just installed, restart may be needed"
  299. }
  300. Write-Host ""
  301. # ── Summary ──
  302. Write-Host "============================================"
  303. if ($Errors -eq 0) {
  304. Write-Host " All dependencies OK."
  305. } else {
  306. Write-Host " $Errors issue(s) found. Fix them above."
  307. }
  308. Write-Host " PDF_SKILL_DIR=$PDF_SKILL_DIR"
  309. Write-Host "============================================"