zcode-app-cli 3.3.5-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. package/LICENSE +24 -0
  2. package/README.md +642 -0
  3. package/bin/zcode.js +848 -0
  4. package/config.example.json +110 -0
  5. package/package.json +81 -0
  6. package/vendor/.node-bundle-meta.json +6 -0
  7. package/vendor/extraction.json +11 -0
  8. package/vendor/node_modules/@zcode/tui/dist/index.js +161875 -0
  9. package/vendor/node_modules/@zcode/tui/package.json +14 -0
  10. package/vendor/packages/android-emulator-plugin/.mcp.json +19 -0
  11. package/vendor/packages/android-emulator-plugin/.zcode-plugin/plugin.json +63 -0
  12. package/vendor/packages/android-emulator-plugin/README.md +134 -0
  13. package/vendor/packages/android-emulator-plugin/commands/android-dev.md +11 -0
  14. package/vendor/packages/android-emulator-plugin/dist/lib/path.d.ts +4 -0
  15. package/vendor/packages/android-emulator-plugin/dist/lib/path.js +17 -0
  16. package/vendor/packages/android-emulator-plugin/dist/lib/result.d.ts +5 -0
  17. package/vendor/packages/android-emulator-plugin/dist/lib/result.js +22 -0
  18. package/vendor/packages/android-emulator-plugin/dist/lib/run.d.ts +18 -0
  19. package/vendor/packages/android-emulator-plugin/dist/lib/run.js +99 -0
  20. package/vendor/packages/android-emulator-plugin/dist/mcp/server.d.ts +3 -0
  21. package/vendor/packages/android-emulator-plugin/dist/mcp/server.js +47607 -0
  22. package/vendor/packages/android-emulator-plugin/dist/providers/app.d.ts +39 -0
  23. package/vendor/packages/android-emulator-plugin/dist/providers/app.js +50 -0
  24. package/vendor/packages/android-emulator-plugin/dist/providers/avd.d.ts +37 -0
  25. package/vendor/packages/android-emulator-plugin/dist/providers/avd.js +185 -0
  26. package/vendor/packages/android-emulator-plugin/dist/providers/build.d.ts +68 -0
  27. package/vendor/packages/android-emulator-plugin/dist/providers/build.js +131 -0
  28. package/vendor/packages/android-emulator-plugin/dist/providers/config.d.ts +7 -0
  29. package/vendor/packages/android-emulator-plugin/dist/providers/config.js +29 -0
  30. package/vendor/packages/android-emulator-plugin/dist/providers/device.d.ts +20 -0
  31. package/vendor/packages/android-emulator-plugin/dist/providers/device.js +91 -0
  32. package/vendor/packages/android-emulator-plugin/dist/providers/logs.d.ts +15 -0
  33. package/vendor/packages/android-emulator-plugin/dist/providers/logs.js +31 -0
  34. package/vendor/packages/android-emulator-plugin/dist/providers/preflight.d.ts +15 -0
  35. package/vendor/packages/android-emulator-plugin/dist/providers/preflight.js +123 -0
  36. package/vendor/packages/android-emulator-plugin/dist/providers/project-template.d.ts +13 -0
  37. package/vendor/packages/android-emulator-plugin/dist/providers/project-template.js +211 -0
  38. package/vendor/packages/android-emulator-plugin/dist/providers/project.d.ts +33 -0
  39. package/vendor/packages/android-emulator-plugin/dist/providers/project.js +195 -0
  40. package/vendor/packages/android-emulator-plugin/dist/providers/screenshot.d.ts +14 -0
  41. package/vendor/packages/android-emulator-plugin/dist/providers/screenshot.js +73 -0
  42. package/vendor/packages/android-emulator-plugin/dist/providers/sdk.d.ts +14 -0
  43. package/vendor/packages/android-emulator-plugin/dist/providers/sdk.js +185 -0
  44. package/vendor/packages/android-emulator-plugin/dist/providers/ui.d.ts +92 -0
  45. package/vendor/packages/android-emulator-plugin/dist/providers/ui.js +146 -0
  46. package/vendor/packages/android-emulator-plugin/hooks/hooks.json +3 -0
  47. package/vendor/packages/android-emulator-plugin/package.json +35 -0
  48. package/vendor/packages/android-emulator-plugin/skills/android-dev/INSTALL_ENVIRONMENT.md +357 -0
  49. package/vendor/packages/android-emulator-plugin/skills/android-dev/SKILL.md +73 -0
  50. package/vendor/packages/android-emulator-plugin/templates/compose-app/README.md +5 -0
  51. package/vendor/packages/document-skills-plugin/.zcode-plugin/plugin.json +8 -0
  52. package/vendor/packages/document-skills-plugin/package.json +8 -0
  53. package/vendor/packages/document-skills-plugin/skills/docx/LICENSE.txt +13 -0
  54. package/vendor/packages/document-skills-plugin/skills/docx/SKILL.md +201 -0
  55. package/vendor/packages/document-skills-plugin/skills/docx/references/chart-templates.md +386 -0
  56. package/vendor/packages/document-skills-plugin/skills/docx/references/common-rules.md +419 -0
  57. package/vendor/packages/document-skills-plugin/skills/docx/references/decorations.md +537 -0
  58. package/vendor/packages/document-skills-plugin/skills/docx/references/design-system.md +1797 -0
  59. package/vendor/packages/document-skills-plugin/skills/docx/references/docx-js-advanced.md +257 -0
  60. package/vendor/packages/document-skills-plugin/skills/docx/references/docx-js-core.md +332 -0
  61. package/vendor/packages/document-skills-plugin/skills/docx/references/faq.md +323 -0
  62. package/vendor/packages/document-skills-plugin/skills/docx/references/math-formulas.md +276 -0
  63. package/vendor/packages/document-skills-plugin/skills/docx/references/ooxml.md +222 -0
  64. package/vendor/packages/document-skills-plugin/skills/docx/references/toc.md +264 -0
  65. package/vendor/packages/document-skills-plugin/skills/docx/routes/comment.md +88 -0
  66. package/vendor/packages/document-skills-plugin/skills/docx/routes/create.md +207 -0
  67. package/vendor/packages/document-skills-plugin/skills/docx/routes/edit.md +115 -0
  68. package/vendor/packages/document-skills-plugin/skills/docx/routes/format.md +120 -0
  69. package/vendor/packages/document-skills-plugin/skills/docx/routes/read.md +114 -0
  70. package/vendor/packages/document-skills-plugin/skills/docx/scenes/academic.md +783 -0
  71. package/vendor/packages/document-skills-plugin/skills/docx/scenes/contract.md +463 -0
  72. package/vendor/packages/document-skills-plugin/skills/docx/scenes/copywriting.md +139 -0
  73. package/vendor/packages/document-skills-plugin/skills/docx/scenes/exam.md +698 -0
  74. package/vendor/packages/document-skills-plugin/skills/docx/scenes/official-doc.md +411 -0
  75. package/vendor/packages/document-skills-plugin/skills/docx/scenes/report.md +340 -0
  76. package/vendor/packages/document-skills-plugin/skills/docx/scenes/resume.md +534 -0
  77. package/vendor/packages/document-skills-plugin/skills/docx/scripts/__init__.py +1 -0
  78. package/vendor/packages/document-skills-plugin/skills/docx/scripts/add_toc_placeholders.py +749 -0
  79. package/vendor/packages/document-skills-plugin/skills/docx/scripts/document.py +1333 -0
  80. package/vendor/packages/document-skills-plugin/skills/docx/scripts/postcheck.py +807 -0
  81. package/vendor/packages/document-skills-plugin/skills/docx/scripts/templates/comments.xml +3 -0
  82. package/vendor/packages/document-skills-plugin/skills/docx/scripts/templates/commentsExtended.xml +3 -0
  83. package/vendor/packages/document-skills-plugin/skills/docx/scripts/templates/commentsExtensible.xml +3 -0
  84. package/vendor/packages/document-skills-plugin/skills/docx/scripts/templates/commentsIds.xml +3 -0
  85. package/vendor/packages/document-skills-plugin/skills/docx/scripts/templates/people.xml +3 -0
  86. package/vendor/packages/document-skills-plugin/skills/docx/scripts/utilities.py +374 -0
  87. package/vendor/packages/document-skills-plugin/skills/docx/setup.sh +177 -0
  88. package/vendor/packages/document-skills-plugin/skills/pdf/LICENSE.txt +13 -0
  89. package/vendor/packages/document-skills-plugin/skills/pdf/SKILL.md +919 -0
  90. package/vendor/packages/document-skills-plugin/skills/pdf/briefs/academic.md +1058 -0
  91. package/vendor/packages/document-skills-plugin/skills/pdf/briefs/creative.md +770 -0
  92. package/vendor/packages/document-skills-plugin/skills/pdf/briefs/poster.md +702 -0
  93. package/vendor/packages/document-skills-plugin/skills/pdf/briefs/process-advanced.md +284 -0
  94. package/vendor/packages/document-skills-plugin/skills/pdf/briefs/process.md +319 -0
  95. package/vendor/packages/document-skills-plugin/skills/pdf/briefs/report.md +1659 -0
  96. package/vendor/packages/document-skills-plugin/skills/pdf/configs/components.md +153 -0
  97. package/vendor/packages/document-skills-plugin/skills/pdf/configs/fonts.md +93 -0
  98. package/vendor/packages/document-skills-plugin/skills/pdf/configs/visual_framework.md +263 -0
  99. package/vendor/packages/document-skills-plugin/skills/pdf/references/resume-academic.tex +130 -0
  100. package/vendor/packages/document-skills-plugin/skills/pdf/references/resume-altacv.tex +190 -0
  101. package/vendor/packages/document-skills-plugin/skills/pdf/scripts/cover_validate.js +367 -0
  102. package/vendor/packages/document-skills-plugin/skills/pdf/scripts/design_engine.py +2816 -0
  103. package/vendor/packages/document-skills-plugin/skills/pdf/scripts/html2pdf-next.js +754 -0
  104. package/vendor/packages/document-skills-plugin/skills/pdf/scripts/html2poster.js +256 -0
  105. package/vendor/packages/document-skills-plugin/skills/pdf/scripts/pdf.py +2959 -0
  106. package/vendor/packages/document-skills-plugin/skills/pdf/scripts/pdf_qa.py +901 -0
  107. package/vendor/packages/document-skills-plugin/skills/pdf/scripts/poster_validate.py +1337 -0
  108. package/vendor/packages/document-skills-plugin/skills/pdf/scripts/setup.sh +269 -0
  109. package/vendor/packages/document-skills-plugin/skills/pdf/scripts/toc_validate.py +2075 -0
  110. package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/charts.md +320 -0
  111. package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/cover-backgrounds.md +384 -0
  112. package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/cover.md +1442 -0
  113. package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/fill-engine.md +527 -0
  114. package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/geometry.md +142 -0
  115. package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/overflow.md +630 -0
  116. package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/pagination.md +367 -0
  117. package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/palette.md +217 -0
  118. package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/typography.md +20 -0
  119. package/vendor/packages/ios-simulator-plugin/.mcp.json +15 -0
  120. package/vendor/packages/ios-simulator-plugin/.zcode-plugin/plugin.json +34 -0
  121. package/vendor/packages/ios-simulator-plugin/README.md +111 -0
  122. package/vendor/packages/ios-simulator-plugin/commands/ios-dev.md +11 -0
  123. package/vendor/packages/ios-simulator-plugin/dist/lib/path.d.ts +4 -0
  124. package/vendor/packages/ios-simulator-plugin/dist/lib/path.js +18 -0
  125. package/vendor/packages/ios-simulator-plugin/dist/lib/result.d.ts +5 -0
  126. package/vendor/packages/ios-simulator-plugin/dist/lib/result.js +20 -0
  127. package/vendor/packages/ios-simulator-plugin/dist/lib/run.d.ts +17 -0
  128. package/vendor/packages/ios-simulator-plugin/dist/lib/run.js +83 -0
  129. package/vendor/packages/ios-simulator-plugin/dist/mcp/server.d.ts +3 -0
  130. package/vendor/packages/ios-simulator-plugin/dist/mcp/server.js +46833 -0
  131. package/vendor/packages/ios-simulator-plugin/dist/providers/build.d.ts +112 -0
  132. package/vendor/packages/ios-simulator-plugin/dist/providers/build.js +99 -0
  133. package/vendor/packages/ios-simulator-plugin/dist/providers/logs.d.ts +21 -0
  134. package/vendor/packages/ios-simulator-plugin/dist/providers/logs.js +26 -0
  135. package/vendor/packages/ios-simulator-plugin/dist/providers/preflight.d.ts +12 -0
  136. package/vendor/packages/ios-simulator-plugin/dist/providers/preflight.js +62 -0
  137. package/vendor/packages/ios-simulator-plugin/dist/providers/project.d.ts +35 -0
  138. package/vendor/packages/ios-simulator-plugin/dist/providers/project.js +288 -0
  139. package/vendor/packages/ios-simulator-plugin/dist/providers/screenshot.d.ts +22 -0
  140. package/vendor/packages/ios-simulator-plugin/dist/providers/screenshot.js +30 -0
  141. package/vendor/packages/ios-simulator-plugin/dist/providers/sim.d.ts +60 -0
  142. package/vendor/packages/ios-simulator-plugin/dist/providers/sim.js +116 -0
  143. package/vendor/packages/ios-simulator-plugin/dist/providers/ui.d.ts +57 -0
  144. package/vendor/packages/ios-simulator-plugin/dist/providers/ui.js +67 -0
  145. package/vendor/packages/ios-simulator-plugin/hooks/hooks.json +3 -0
  146. package/vendor/packages/ios-simulator-plugin/package.json +35 -0
  147. package/vendor/packages/ios-simulator-plugin/skills/ios-dev/SKILL.md +45 -0
  148. package/vendor/packages/ios-simulator-plugin/templates/swiftui-app/README.md +5 -0
  149. package/vendor/packages/restore-legacy-sessions-plugin/.zcode-plugin/plugin.json +9 -0
  150. package/vendor/packages/restore-legacy-sessions-plugin/README.md +35 -0
  151. package/vendor/packages/restore-legacy-sessions-plugin/commands/restore-legacy-sessions.md +11 -0
  152. package/vendor/packages/restore-legacy-sessions-plugin/package.json +8 -0
  153. package/vendor/packages/restore-legacy-sessions-plugin/skills/restore-legacy-sessions/SKILL.md +92 -0
  154. package/vendor/packages/restore-legacy-sessions-plugin/skills/restore-legacy-sessions/scripts/restore-conversation.mjs +648 -0
  155. package/vendor/packages/restore-legacy-sessions-plugin/skills/restore-legacy-sessions/scripts/scan-legacy-sessions.mjs +422 -0
  156. package/vendor/packages/skill-creator-plugin/.zcode-plugin/plugin.json +8 -0
  157. package/vendor/packages/skill-creator-plugin/package.json +8 -0
  158. package/vendor/packages/skill-creator-plugin/skills/skill-creator/SKILL.md +159 -0
  159. package/vendor/packages/zcode-guide-plugin/.zcode-plugin/plugin.json +8 -0
  160. package/vendor/packages/zcode-guide-plugin/README.md +22 -0
  161. package/vendor/packages/zcode-guide-plugin/package.json +8 -0
  162. package/vendor/packages/zcode-guide-plugin/skills/diagnosing-commands/SKILL.md +66 -0
  163. package/vendor/packages/zcode-guide-plugin/skills/diagnosing-hooks/SKILL.md +66 -0
  164. package/vendor/packages/zcode-guide-plugin/skills/diagnosing-mcp/SKILL.md +97 -0
  165. package/vendor/packages/zcode-guide-plugin/skills/diagnosing-plugins/SKILL.md +59 -0
  166. package/vendor/packages/zcode-guide-plugin/skills/diagnosing-skills/SKILL.md +61 -0
  167. package/vendor/packages/zcode-guide-plugin/skills/zcode-configuration-guide/SKILL.md +99 -0
  168. package/vendor/zcode.cjs +2926 -0
  169. package/zcode-runtime.lock.json +8 -0
@@ -0,0 +1,2959 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ PDF Processing Toolkit — All-in-One CLI
4
+
5
+ Usage:
6
+ python3 pdf.py <command> [args...]
7
+
8
+ Commands:
9
+ env.check [--json] Check environment dependencies
10
+ env.fix Auto-install missing dependencies
11
+
12
+ extract.text <pdf> [-p pages]
13
+ extract.table <pdf> [-p pages]
14
+ extract.image <pdf> -o <dir>
15
+
16
+ pages.merge <pdf>... -o <out>
17
+ pages.split <pdf> -o <dir>
18
+ pages.rotate <pdf> <deg> -o <out> [-p pages]
19
+ pages.crop <pdf> <box> -o <out> [-p pages]
20
+
21
+ meta.get <pdf>
22
+ meta.set <pdf> -o <out> -d <json>
23
+ meta.brand <pdf>... [-o <out>] [-t title] [-q]
24
+
25
+ form.info <pdf>
26
+ form.fill <pdf> -o <out> -d <json>
27
+ form.detail <pdf> <output.json>
28
+ form.fill-legacy <pdf> <fields.json> <output.pdf>
29
+ form.annotate <pdf> <fields.json> <output.pdf>
30
+ form.render <pdf> <output_dir> [--max-dim N]
31
+ form.validate <page> <fields.json> <input_img> <output_img>
32
+ form.check-bbox <fields.json>
33
+
34
+ pages.clean <pdf> -o <out> Remove blank pages
35
+
36
+ convert.office <file> [-o <out>]
37
+ convert.html <file> [-o <out>] [--css <file>]
38
+ convert.blueprint <llm_response.txt> [-o <out>]
39
+ convert.latex <file> [--runs N] [--keep-logs]
40
+
41
+ palette.generate [--title "..."] [--mode minimal] [--format python|json|css]
42
+ palette.cascade [--title "..."] [--mode minimal] [--format summary|json|css|reportlab]
43
+
44
+ code.sanitize <file>
45
+ content.sanitize <file> [--apply] Fix content issues (CJK, encoding)
46
+
47
+ font.check <pdf>
48
+
49
+ toc.check <pdf>
50
+ """
51
+
52
+ from __future__ import annotations
53
+
54
+ import html
55
+ import json
56
+ import os
57
+ import re
58
+ import shutil
59
+ import subprocess
60
+ import sys
61
+ from dataclasses import dataclass
62
+ from datetime import datetime
63
+ from pathlib import Path
64
+ from typing import Any, Callable, Dict, List, Optional, Set, Tuple
65
+
66
+
67
+ # ═══════════════════════════════════════════════════════════════
68
+ # Section 0: Framework — Output, @cmd registry, CLI parser
69
+ # ═══════════════════════════════════════════════════════════════
70
+
71
+ class Output:
72
+ """Structured JSON output for all subcommands."""
73
+
74
+ @staticmethod
75
+ def success(data: dict):
76
+ payload = {"status": "success", "data": data}
77
+ sys.stdout.write(json.dumps(payload, ensure_ascii=False, indent=2) + "\n")
78
+ raise SystemExit(0)
79
+
80
+ @staticmethod
81
+ def error(error: str, message: str, hint: Optional[str] = None, code: int = 1):
82
+ payload = {"status": "error", "error": error, "message": message}
83
+ if hint is not None:
84
+ payload["hint"] = hint
85
+ sys.stderr.write(json.dumps(payload, ensure_ascii=False, indent=2) + "\n")
86
+ raise SystemExit(code)
87
+
88
+ @staticmethod
89
+ def check_file(filepath: str) -> Path:
90
+ target = Path(filepath)
91
+ if not target.exists():
92
+ Output.error("FileNotFound", f"File not found: {filepath}", code=2)
93
+ return target
94
+
95
+
96
+ # Command registry
97
+ _COMMANDS: Dict[str, Callable] = {}
98
+
99
+
100
+ def cmd(name: str):
101
+ """Decorator to register a CLI command under a dotted namespace."""
102
+ def decorator(fn: Callable) -> Callable:
103
+ _COMMANDS[name] = fn
104
+ return fn
105
+ return decorator
106
+
107
+
108
+ def _pop_flag(argv: list, short: str, long: str, needs_value: bool = True):
109
+ """Extract a flag (and optional value) from *argv* in-place."""
110
+ for idx, tok in enumerate(argv):
111
+ if tok in (short, long):
112
+ argv.pop(idx)
113
+ if needs_value:
114
+ if idx < len(argv):
115
+ return argv.pop(idx)
116
+ Output.error("MissingArg", f"Flag {long} requires a value")
117
+ return True
118
+ return None
119
+
120
+
121
+ def _load_json_arg(argv: list) -> dict:
122
+ """Read JSON from -d/--data string or -f/--file path."""
123
+ raw = _pop_flag(argv, "-d", "--data")
124
+ if raw is not None:
125
+ try:
126
+ return json.loads(raw)
127
+ except json.JSONDecodeError as exc:
128
+ Output.error("InvalidJSON", f"JSON parse error: {exc}")
129
+
130
+ fpath = _pop_flag(argv, "-f", "--file")
131
+ if fpath is not None:
132
+ try:
133
+ with open(fpath) as fh:
134
+ return json.load(fh)
135
+ except Exception as exc:
136
+ Output.error("FileError", f"Failed to read file: {exc}")
137
+
138
+ Output.error("MissingData", "Requires --data or --file argument")
139
+
140
+
141
+ def _resolve_page_indices(range_spec: Optional[str], page_count: int) -> List[int]:
142
+ """Turn a human-friendly range string (1-indexed) into a sorted list of 0-based indices."""
143
+ if not range_spec:
144
+ return list(range(page_count))
145
+ indices: Set[int] = set()
146
+ for segment in range_spec.split(","):
147
+ segment = segment.strip()
148
+ if "-" in segment:
149
+ lo, hi = segment.split("-", 1)
150
+ for i in range(int(lo) - 1, min(int(hi), page_count)):
151
+ indices.add(i)
152
+ else:
153
+ val = int(segment) - 1
154
+ if 0 <= val < page_count:
155
+ indices.add(val)
156
+ return sorted(indices)
157
+
158
+
159
+ _SCRIPT_DIR = Path(__file__).resolve().parent
160
+
161
+
162
+ # ═══════════════════════════════════════════════════════════════
163
+ # Section 1: env — environment diagnostics and auto-fix
164
+ # ═══════════════════════════════════════════════════════════════
165
+
166
+ def _probe_cmd(name: str, version_args: Optional[List[str]] = None) -> Tuple[str, str]:
167
+ """Check if a command exists and optionally get its version. Returns (status, detail)."""
168
+ path = shutil.which(name)
169
+ if path is None:
170
+ return ("missing", "")
171
+ if version_args is None:
172
+ return ("ok", "")
173
+ try:
174
+ result = subprocess.run(
175
+ [path] + version_args,
176
+ capture_output=True, text=True, timeout=10
177
+ )
178
+ ver = result.stdout.strip() or result.stderr.strip()
179
+ return ("ok", ver)
180
+ except Exception:
181
+ return ("ok", "")
182
+
183
+
184
+ def _probe_python_module(mod_name: str) -> Tuple[str, str]:
185
+ """Check if a Python module is importable and get its version."""
186
+ try:
187
+ result = subprocess.run(
188
+ [sys.executable, "-c", f"import {mod_name}; print(getattr({mod_name}, '__version__', 'installed'))"],
189
+ capture_output=True, text=True, timeout=10
190
+ )
191
+ if result.returncode == 0:
192
+ return ("ok", result.stdout.strip())
193
+ return ("missing", "")
194
+ except Exception:
195
+ return ("missing", "")
196
+
197
+
198
+ def _probe_node() -> Tuple[str, str]:
199
+ s, d = _probe_cmd("node", ["--version"])
200
+ if s == "ok" and d:
201
+ d = d.lstrip("v")
202
+ return (s, d)
203
+
204
+
205
+ def _probe_python() -> Tuple[str, str]:
206
+ try:
207
+ import platform
208
+ return ("ok", platform.python_version())
209
+ except Exception:
210
+ return ("ok", "")
211
+
212
+
213
+ def _probe_libreoffice() -> Tuple[str, str]:
214
+ candidates = [
215
+ "/Applications/LibreOffice.app/Contents/MacOS/soffice",
216
+ os.path.expanduser("~/Applications/LibreOffice.app/Contents/MacOS/soffice"),
217
+ "/usr/bin/soffice",
218
+ "/usr/local/bin/soffice",
219
+ "/usr/lib/libreoffice/program/soffice",
220
+ "/opt/libreoffice/program/soffice",
221
+ "/snap/bin/libreoffice.soffice",
222
+ ]
223
+ for c in candidates:
224
+ if Path(c).is_file():
225
+ return ("ok", "")
226
+ for alias in ("soffice", "libreoffice"):
227
+ if shutil.which(alias):
228
+ return ("ok", "")
229
+ return ("missing", "")
230
+
231
+
232
+ def _probe_tectonic() -> Tuple[str, str]:
233
+ home_bin = Path.home() / "tectonic"
234
+ if home_bin.exists() and os.access(home_bin, os.X_OK):
235
+ return ("ok", "")
236
+ tec_local = _SCRIPT_DIR / "tectonic"
237
+ if tec_local.exists() and os.access(tec_local, os.X_OK):
238
+ return ("ok", "")
239
+ if shutil.which("tectonic"):
240
+ return ("ok", "")
241
+ return ("missing", "")
242
+
243
+
244
+ def _probe_playwright_npm() -> Tuple[str, str]:
245
+ """Check if playwright npm package is installed."""
246
+ try:
247
+ result = subprocess.run(
248
+ ["node", "-e", "console.log(require('playwright/package.json').version)"],
249
+ capture_output=True, text=True, timeout=10
250
+ )
251
+ if result.returncode == 0 and result.stdout.strip():
252
+ return ("ok", result.stdout.strip())
253
+ except Exception:
254
+ pass
255
+ # Try global
256
+ try:
257
+ result = subprocess.run(
258
+ ["npm", "list", "-g", "playwright", "--depth=0"],
259
+ capture_output=True, text=True, timeout=15
260
+ )
261
+ import re as _re
262
+ m = _re.search(r"playwright@(\S+)", result.stdout)
263
+ if m:
264
+ return ("ok", m.group(1))
265
+ except Exception:
266
+ pass
267
+ return ("missing", "")
268
+
269
+
270
+ def _probe_chromium() -> Tuple[str, str]:
271
+ """Check if Playwright Chromium browser is installed."""
272
+ import platform as _platform
273
+ home = Path.home()
274
+ if _platform.system() == "Darwin":
275
+ cache_dir = home / "Library" / "Caches" / "ms-playwright"
276
+ else:
277
+ cache_dir = home / ".cache" / "ms-playwright"
278
+ if cache_dir.is_dir():
279
+ for entry in sorted(cache_dir.iterdir(), reverse=True):
280
+ if "chromium" in entry.name.lower():
281
+ return ("ok", entry.name)
282
+ return ("missing", "")
283
+
284
+
285
+ @cmd("env.check")
286
+ def env_check(argv: list):
287
+ """Check environment dependencies."""
288
+ use_json = _pop_flag(argv, "-j", "--json", needs_value=False)
289
+
290
+ s_node = _probe_node()
291
+ s_pw = _probe_playwright_npm()
292
+ s_cr = _probe_chromium()
293
+ s_py = _probe_python()
294
+ s_pike = _probe_python_module("pikepdf")
295
+ s_plumb = _probe_python_module("pdfplumber")
296
+ s_lo = _probe_libreoffice()
297
+ s_tec = _probe_tectonic()
298
+ s_pw_py = _probe_python_module("playwright")
299
+
300
+ if use_json:
301
+ report = {
302
+ "html_route": {
303
+ "node": s_node[0], "node_version": s_node[1],
304
+ "playwright": s_pw[0], "playwright_version": s_pw[1],
305
+ "chromium": s_cr[0], "chromium_detail": s_cr[1],
306
+ },
307
+ "process_route": {
308
+ "python3": s_py[0], "python3_version": s_py[1],
309
+ "pikepdf": s_pike[0], "pikepdf_version": s_pike[1],
310
+ "pdfplumber": s_plumb[0], "pdfplumber_version": s_plumb[1],
311
+ "playwright_python": s_pw_py[0], "playwright_python_version": s_pw_py[1],
312
+ },
313
+ "optional": {
314
+ "libreoffice": s_lo[0],
315
+ "tectonic": s_tec[0],
316
+ },
317
+ }
318
+ sys.stdout.write(json.dumps(report, ensure_ascii=False, indent=2) + "\n")
319
+ # Determine exit code
320
+ rc = 0
321
+ for v in [s_node, s_pw, s_cr, s_py, s_pike, s_plumb]:
322
+ if v[0] != "ok":
323
+ rc = 2
324
+ break
325
+ raise SystemExit(rc)
326
+
327
+ # Human-readable output
328
+ rc = 0
329
+
330
+ def show(name: str, status: Tuple[str, str], optional: bool = False):
331
+ nonlocal rc
332
+ s, d = status
333
+ if s == "ok":
334
+ detail = f" ({d})" if d else ""
335
+ print(f" \u2713 {name}{detail}")
336
+ elif optional:
337
+ print(f" \u25cb {name} (optional, not installed)")
338
+ else:
339
+ print(f" \u2717 {name} (missing)")
340
+ rc = 2
341
+
342
+ print("=== PDF Skill Environment ===\n")
343
+ print("--- HTML Route ---")
344
+ show("node", s_node)
345
+ show("playwright", s_pw)
346
+ show("chromium", s_cr)
347
+
348
+ print("\n--- Process Route ---")
349
+ show("python3", s_py)
350
+ show("pikepdf", s_pike)
351
+ show("pdfplumber", s_plumb)
352
+ if s_pw_py[0] == "ok":
353
+ print(f" (playwright-python: {s_pw_py[1]})")
354
+
355
+ print("\n--- Optional ---")
356
+ show("libreoffice", s_lo, optional=True)
357
+ show("tectonic", s_tec, optional=True)
358
+
359
+ print("\n=== Install Commands ===")
360
+ print(" Node.js: brew install node (macOS) / apt install nodejs (Ubuntu)")
361
+ print(" Playwright: npm install -g playwright && npx playwright install chromium")
362
+ print(" Python: brew install python3 (macOS) / apt install python3 (Ubuntu)")
363
+ print(" pikepdf: pip install pikepdf pdfplumber --user")
364
+ print(" LibreOffice: brew install --cask libreoffice (macOS)")
365
+ print(" Tectonic: curl -fsSL https://drop-sh.fullyjustified.net | sh")
366
+ raise SystemExit(rc)
367
+
368
+
369
+ @cmd("env.fix")
370
+ def env_fix(argv: list):
371
+ """Auto-install missing Python dependencies."""
372
+ modules = {
373
+ "pikepdf": "pikepdf",
374
+ "pdfplumber": "pdfplumber",
375
+ "pypdf": "pypdf",
376
+ "pdf2image": "pdf2image",
377
+ "PIL": "Pillow",
378
+ }
379
+ installed = []
380
+ for mod, pkg in modules.items():
381
+ s, _ = _probe_python_module(mod)
382
+ if s == "missing":
383
+ print(f"Installing {pkg}...")
384
+ for attempt in (
385
+ [sys.executable, "-m", "pip", "install", "-q", pkg],
386
+ [sys.executable, "-m", "pip", "install", "-q", "--user", pkg],
387
+ [sys.executable, "-m", "pip", "install", "-q", "--break-system-packages", pkg],
388
+ ):
389
+ result = subprocess.run(attempt, capture_output=True, text=True)
390
+ if result.returncode == 0:
391
+ installed.append(pkg)
392
+ break
393
+ else:
394
+ print(f" Failed to install {pkg}")
395
+
396
+ if installed:
397
+ print(f"\nInstalled: {', '.join(installed)}")
398
+ else:
399
+ print("All Python dependencies are already installed.")
400
+ raise SystemExit(0)
401
+
402
+
403
+ # ═══════════════════════════════════════════════════════════════
404
+ # Section 2: extract — text, tables, and embedded images
405
+ # ═══════════════════════════════════════════════════════════════
406
+
407
+ @cmd("extract.text")
408
+ def extract_text(argv: list):
409
+ """Pull plain text from selected pages."""
410
+ if not argv:
411
+ Output.error("MissingArg", "pdf path required")
412
+ pdf_path = argv.pop(0)
413
+ page_range = _pop_flag(argv, "-p", "--pages")
414
+
415
+ import pdfplumber
416
+ src = Output.check_file(pdf_path)
417
+ try:
418
+ doc = pdfplumber.open(src)
419
+ except Exception as exc:
420
+ Output.error("PDFError", f"Cannot open PDF: {exc}", code=3)
421
+
422
+ total_pages = len(doc.pages)
423
+ target_pages = _resolve_page_indices(page_range, total_pages)
424
+ char_total = 0
425
+ page_results = []
426
+
427
+ for pg_idx in target_pages:
428
+ content = doc.pages[pg_idx].extract_text() or ""
429
+ char_total += len(content)
430
+ page_results.append({"page": pg_idx + 1, "chars": len(content), "text": content})
431
+
432
+ doc.close()
433
+ Output.success({
434
+ "total_pages": total_pages,
435
+ "extracted_pages": len(target_pages),
436
+ "total_chars": char_total,
437
+ "pages": page_results,
438
+ })
439
+
440
+
441
+ @cmd("extract.table")
442
+ def extract_table(argv: list):
443
+ """Locate and return every table on selected pages."""
444
+ if not argv:
445
+ Output.error("MissingArg", "pdf path required")
446
+ pdf_path = argv.pop(0)
447
+ page_range = _pop_flag(argv, "-p", "--pages")
448
+
449
+ import pdfplumber
450
+ src = Output.check_file(pdf_path)
451
+ try:
452
+ doc = pdfplumber.open(src)
453
+ except Exception as exc:
454
+ Output.error("PDFError", f"Cannot open PDF: {exc}", code=3)
455
+
456
+ total_pages = len(doc.pages)
457
+ target_pages = _resolve_page_indices(page_range, total_pages)
458
+ collected = []
459
+
460
+ for pg_idx in target_pages:
461
+ for tbl_num, raw_table in enumerate(doc.pages[pg_idx].extract_tables()):
462
+ if not raw_table:
463
+ continue
464
+ sanitised = [
465
+ [(cell.strip() if cell else "") for cell in row]
466
+ for row in raw_table
467
+ ]
468
+ collected.append({
469
+ "page": pg_idx + 1,
470
+ "table_index": tbl_num,
471
+ "rows": len(sanitised),
472
+ "cols": len(sanitised[0]) if sanitised else 0,
473
+ "data": sanitised,
474
+ })
475
+
476
+ doc.close()
477
+ Output.success({
478
+ "total_pages": total_pages,
479
+ "extracted_pages": len(target_pages),
480
+ "total_tables": len(collected),
481
+ "tables": collected,
482
+ })
483
+
484
+
485
+ @cmd("extract.image")
486
+ def extract_image(argv: list):
487
+ """Save every embedded raster image to output dir."""
488
+ if not argv:
489
+ Output.error("MissingArg", "pdf path required")
490
+ pdf_path = argv.pop(0)
491
+ out_dir = _pop_flag(argv, "-o", "--output") or "."
492
+
493
+ import pikepdf
494
+ src = Output.check_file(pdf_path)
495
+ dest = Path(out_dir)
496
+ dest.mkdir(parents=True, exist_ok=True)
497
+
498
+ try:
499
+ doc = pikepdf.open(src)
500
+ except Exception as exc:
501
+ Output.error("PDFError", f"Cannot open PDF: {exc}", code=3)
502
+
503
+ saved = []
504
+ seq = 0
505
+ _EXT_MAP = {
506
+ "/DCTDecode": "jpg",
507
+ "/FlateDecode": "png",
508
+ "/JPXDecode": "jp2",
509
+ }
510
+
511
+ for page_no, pg in enumerate(doc.pages, 1):
512
+ res = pg.get("/Resources")
513
+ if res is None or "/XObject" not in res:
514
+ continue
515
+ for key, ref in res.XObject.items():
516
+ try:
517
+ img_obj = doc.get_object(ref.objgen)
518
+ if img_obj.get("/Subtype") != "/Image":
519
+ continue
520
+ seq += 1
521
+ w = int(img_obj.get("/Width", 0))
522
+ h = int(img_obj.get("/Height", 0))
523
+ filt = img_obj.get("/Filter")
524
+ ext = _EXT_MAP.get(str(filt) if filt else None, "bin")
525
+ fname = f"page{page_no}_img{seq}.{ext}"
526
+ out_file = dest / fname
527
+ out_file.write_bytes(img_obj.read_raw_bytes())
528
+ saved.append({
529
+ "page": page_no, "name": str(key), "file": str(out_file),
530
+ "width": w, "height": h, "format": ext,
531
+ })
532
+ except Exception:
533
+ continue
534
+
535
+ doc.close()
536
+ Output.success({"output_dir": str(dest), "total_images": len(saved), "images": saved})
537
+
538
+
539
+ # ═══════════════════════════════════════════════════════════════
540
+ # Section 3: pages — merge, split, rotate, crop
541
+ # ═══════════════════════════════════════════════════════════════
542
+
543
+ @cmd("pages.merge")
544
+ def pages_merge(argv: list):
545
+ """Concatenate several PDF files into one."""
546
+ out_path = _pop_flag(argv, "-o", "--output")
547
+ if out_path is None:
548
+ Output.error("MissingArg", "--output is required")
549
+ if not argv:
550
+ Output.error("MissingArg", "At least one PDF required")
551
+
552
+ import pikepdf
553
+ sources = [Output.check_file(p) for p in argv]
554
+ handles = []
555
+ try:
556
+ combined = pikepdf.new()
557
+ descriptions = []
558
+ for src in sources:
559
+ handle = pikepdf.open(src)
560
+ handles.append(handle)
561
+ n = len(handle.pages)
562
+ descriptions.append(f"{src.name} ({n} pages)")
563
+ for pg in handle.pages:
564
+ combined.pages.append(pg)
565
+ total = len(combined.pages)
566
+ combined.save(out_path)
567
+ combined.close()
568
+ except Exception as exc:
569
+ Output.error("MergeError", f"Merge failed: {exc}", code=4)
570
+ finally:
571
+ for h in handles:
572
+ try:
573
+ h.close()
574
+ except Exception:
575
+ pass
576
+
577
+ Output.success({"output": out_path, "total_pages": total, "sources": descriptions})
578
+
579
+
580
+ @cmd("pages.split")
581
+ def pages_split(argv: list):
582
+ """Write each page as a separate single-page PDF."""
583
+ if not argv:
584
+ Output.error("MissingArg", "pdf path required")
585
+ pdf_path = argv.pop(0)
586
+ out_dir = _pop_flag(argv, "-o", "--output") or "."
587
+
588
+ import pikepdf
589
+ src = Output.check_file(pdf_path)
590
+ dest = Path(out_dir)
591
+ dest.mkdir(parents=True, exist_ok=True)
592
+
593
+ try:
594
+ doc = pikepdf.open(src)
595
+ except Exception as exc:
596
+ Output.error("PDFError", f"Cannot open PDF: {exc}", code=3)
597
+
598
+ generated = []
599
+ base = src.stem
600
+ try:
601
+ for idx, pg in enumerate(doc.pages, 1):
602
+ fp = dest / f"{base}_page{idx:03d}.pdf"
603
+ single_doc = pikepdf.new()
604
+ single_doc.pages.append(pg)
605
+ single_doc.save(fp)
606
+ single_doc.close()
607
+ generated.append(str(fp))
608
+ doc.close()
609
+ except Exception as exc:
610
+ Output.error("SplitError", f"Split failed: {exc}", code=4)
611
+
612
+ Output.success({"output_dir": str(dest), "total_pages": len(generated), "files": generated})
613
+
614
+
615
+ @cmd("pages.rotate")
616
+ def pages_rotate(argv: list):
617
+ """Rotate selected pages by 90/180/270 degrees."""
618
+ if len(argv) < 2:
619
+ Output.error("MissingArg", "pdf path and degrees required")
620
+ pdf_path = argv.pop(0)
621
+ degrees = int(argv.pop(0))
622
+ out_path = _pop_flag(argv, "-o", "--output")
623
+ if out_path is None:
624
+ Output.error("MissingArg", "--output is required")
625
+ page_range = _pop_flag(argv, "-p", "--pages")
626
+
627
+ if degrees not in (90, 180, 270):
628
+ Output.error("InvalidDegrees", "Rotation angle must be 90, 180, or 270")
629
+
630
+ import pikepdf
631
+ src = Output.check_file(pdf_path)
632
+ try:
633
+ doc = pikepdf.open(src)
634
+ except Exception as exc:
635
+ Output.error("PDFError", f"Cannot open PDF: {exc}", code=3)
636
+
637
+ targets = _resolve_page_indices(page_range, len(doc.pages))
638
+ try:
639
+ for i in targets:
640
+ existing = int(doc.pages[i].get("/Rotate", 0))
641
+ doc.pages[i]["/Rotate"] = (existing + degrees) % 360
642
+ doc.save(out_path)
643
+ doc.close()
644
+ except Exception as exc:
645
+ Output.error("RotateError", f"Rotation failed: {exc}", code=4)
646
+
647
+ Output.success({"output": out_path, "degrees": degrees, "pages_rotated": len(targets)})
648
+
649
+
650
+ @cmd("pages.crop")
651
+ def pages_crop(argv: list):
652
+ """Set the media/crop box on selected pages. box = 'left,bottom,right,top' in pt."""
653
+ if len(argv) < 2:
654
+ Output.error("MissingArg", "pdf path and crop box required")
655
+ pdf_path = argv.pop(0)
656
+ box_str = argv.pop(0)
657
+ out_path = _pop_flag(argv, "-o", "--output")
658
+ if out_path is None:
659
+ Output.error("MissingArg", "--output is required")
660
+ page_range = _pop_flag(argv, "-p", "--pages")
661
+
662
+ try:
663
+ coords = [float(v.strip()) for v in box_str.split(",")]
664
+ assert len(coords) == 4
665
+ left, bottom, right, top = coords
666
+ except Exception:
667
+ Output.error("InvalidBox", "Invalid crop box format, should be: left,bottom,right,top",
668
+ hint="Example: 50,50,550,750")
669
+
670
+ import pikepdf
671
+ src = Output.check_file(pdf_path)
672
+ try:
673
+ doc = pikepdf.open(src)
674
+ except Exception as exc:
675
+ Output.error("PDFError", f"Cannot open PDF: {exc}", code=3)
676
+
677
+ targets = _resolve_page_indices(page_range, len(doc.pages))
678
+ try:
679
+ arr = pikepdf.Array([left, bottom, right, top])
680
+ for i in targets:
681
+ doc.pages[i].mediabox = arr
682
+ doc.pages[i].cropbox = arr
683
+ doc.save(out_path)
684
+ doc.close()
685
+ except Exception as exc:
686
+ Output.error("CropError", f"Crop failed: {exc}", code=4)
687
+
688
+ Output.success({
689
+ "output": out_path,
690
+ "box": {"left": left, "bottom": bottom, "right": right, "top": top},
691
+ "pages_cropped": len(targets),
692
+ })
693
+
694
+
695
+ @cmd("pages.clean")
696
+ def pages_clean(argv: list):
697
+ """Remove truly blank pages from a PDF.
698
+
699
+ A page is considered blank ONLY if it has exactly 0 text characters AND
700
+ 0 images. Pages with even a single character or a tiny image are kept.
701
+
702
+ Usage:
703
+ pdf.py pages.clean input.pdf -o output.pdf
704
+ """
705
+ if not argv:
706
+ Output.error("MissingArg", "PDF path required")
707
+ pdf_path = argv.pop(0)
708
+ out_path = _pop_flag(argv, "-o", "--output")
709
+ if out_path is None:
710
+ Output.error("MissingArg", "--output is required")
711
+ # --threshold is accepted but ignored (kept for backward compat)
712
+ _pop_flag(argv, "-t", "--threshold")
713
+
714
+ src = Output.check_file(pdf_path)
715
+
716
+ # Phase 1: Detect blank pages using pdfplumber (text extraction)
717
+ try:
718
+ import pdfplumber
719
+ except ImportError:
720
+ Output.error("DependencyMissing", "pdfplumber required: pip install pdfplumber")
721
+
722
+ blank_indices = [] # 0-indexed
723
+ total_pages = 0
724
+ try:
725
+ pdf = pdfplumber.open(str(src))
726
+ total_pages = len(pdf.pages)
727
+ for i, page in enumerate(pdf.pages):
728
+ text = (page.extract_text() or "").strip()
729
+ # Check for ANY images on the page (no size filter)
730
+ images = page.images if hasattr(page, 'images') else []
731
+ # A page is blank ONLY if it has exactly 0 characters AND 0 images
732
+ if len(text) == 0 and len(images) == 0:
733
+ blank_indices.append(i)
734
+ pdf.close()
735
+ except Exception as exc:
736
+ Output.error("PDFError", f"Cannot analyze PDF: {exc}", code=3)
737
+
738
+ if not blank_indices:
739
+ Output.success({
740
+ "output": str(src),
741
+ "total_pages": total_pages,
742
+ "blank_pages_removed": 0,
743
+ "message": "No blank pages found",
744
+ })
745
+ return
746
+
747
+ # Phase 2: Remove blank pages using pikepdf
748
+ try:
749
+ import pikepdf
750
+ except ImportError:
751
+ Output.error("DependencyMissing", "pikepdf required: pip install pikepdf")
752
+
753
+ try:
754
+ doc = pikepdf.open(str(src))
755
+ # Remove in reverse order to preserve indices
756
+ for idx in sorted(blank_indices, reverse=True):
757
+ if idx < len(doc.pages):
758
+ del doc.pages[idx]
759
+ doc.save(out_path)
760
+ doc.close()
761
+ except Exception as exc:
762
+ Output.error("PDFError", f"Cannot remove blank pages: {exc}", code=4)
763
+
764
+ remaining = total_pages - len(blank_indices)
765
+ Output.success({
766
+ "output": out_path,
767
+ "total_pages": total_pages,
768
+ "blank_pages_removed": len(blank_indices),
769
+ "blank_page_numbers": [i + 1 for i in blank_indices], # 1-indexed for humans
770
+ "remaining_pages": remaining,
771
+ })
772
+
773
+
774
+ # ═══════════════════════════════════════════════════════════════
775
+ # Section 4: meta — metadata reading, writing, and branding
776
+ # ═══════════════════════════════════════════════════════════════
777
+
778
+ _XMP_MAPPING = {
779
+ "Title": "dc:title",
780
+ "Author": "dc:creator",
781
+ "Subject": "dc:description",
782
+ "Keywords": "pdf:Keywords",
783
+ "Creator": "xmp:CreatorTool",
784
+ "Producer": "pdf:Producer",
785
+ }
786
+ _ACCEPTED_KEYS = set(_XMP_MAPPING.keys())
787
+
788
+
789
+ @cmd("meta.get")
790
+ def meta_get(argv: list):
791
+ """Read document information and metadata."""
792
+ if not argv:
793
+ Output.error("MissingArg", "pdf path required")
794
+
795
+ import pikepdf
796
+ src = Output.check_file(argv[0])
797
+ try:
798
+ doc = pikepdf.open(src)
799
+ except Exception as exc:
800
+ Output.error("PDFError", f"Cannot open PDF: {exc}", code=3)
801
+
802
+ record: dict = {
803
+ "pages": len(doc.pages),
804
+ "pdf_version": str(doc.pdf_version),
805
+ }
806
+
807
+ if doc.pages:
808
+ mb = doc.pages[0].mediabox
809
+ record["page_size"] = {
810
+ "width": float(mb[2] - mb[0]),
811
+ "height": float(mb[3] - mb[1]),
812
+ "unit": "pt",
813
+ }
814
+
815
+ kv_pairs = {}
816
+ if doc.docinfo:
817
+ for k in doc.docinfo.keys():
818
+ try:
819
+ kv_pairs[str(k).lstrip("/")] = str(doc.docinfo[k])
820
+ except Exception:
821
+ pass
822
+ record["metadata"] = kv_pairs
823
+ record["encrypted"] = doc.is_encrypted
824
+ record["has_form"] = "/AcroForm" in doc.Root
825
+ record["has_outlines"] = "/Outlines" in doc.Root
826
+
827
+ doc.close()
828
+ Output.success(record)
829
+
830
+
831
+ @cmd("meta.set")
832
+ def meta_set(argv: list):
833
+ """Update XMP + legacy docinfo metadata fields."""
834
+ if not argv:
835
+ Output.error("MissingArg", "pdf path required")
836
+ pdf_path = argv.pop(0)
837
+ out_path = _pop_flag(argv, "-o", "--output")
838
+ if out_path is None:
839
+ Output.error("MissingArg", "--output is required")
840
+ data = _load_json_arg(argv)
841
+
842
+ import pikepdf
843
+ src = Output.check_file(pdf_path)
844
+ try:
845
+ doc = pikepdf.open(src)
846
+ except Exception as exc:
847
+ Output.error("PDFError", f"Cannot open PDF: {exc}", code=3)
848
+
849
+ # XMP layer
850
+ with doc.open_metadata() as xmp:
851
+ for raw_key, raw_val in data.items():
852
+ norm = raw_key.title()
853
+ xmp_key = _XMP_MAPPING.get(norm)
854
+ if xmp_key is None:
855
+ continue
856
+ try:
857
+ xmp[xmp_key] = str(raw_val)
858
+ except Exception:
859
+ pass
860
+
861
+ # Legacy docinfo layer
862
+ if not doc.docinfo:
863
+ doc.docinfo = pikepdf.Dictionary()
864
+ for raw_key, raw_val in data.items():
865
+ norm = raw_key.title()
866
+ if norm in _ACCEPTED_KEYS:
867
+ doc.docinfo[pikepdf.Name(f"/{norm}")] = pikepdf.String(str(raw_val))
868
+
869
+ doc.docinfo[pikepdf.Name("/ModDate")] = pikepdf.String(
870
+ datetime.now().strftime("D:%Y%m%d%H%M%S")
871
+ )
872
+
873
+ try:
874
+ doc.save(out_path)
875
+ doc.close()
876
+ except Exception as exc:
877
+ Output.error("SaveError", f"Save failed: {exc}", code=4)
878
+
879
+ Output.success({"output": out_path, "updated_fields": list(data.keys())})
880
+
881
+
882
+ @cmd("meta.brand")
883
+ def meta_brand(argv: list):
884
+ """Add Z.ai branding metadata to PDF documents."""
885
+ output_path = _pop_flag(argv, "-o", "--output")
886
+ custom_title = _pop_flag(argv, "-t", "--title")
887
+ quiet = _pop_flag(argv, "-q", "--quiet", needs_value=False)
888
+
889
+ if not argv:
890
+ Output.error("MissingArg", "At least one PDF file required")
891
+
892
+ # Check if output is specified for multiple files
893
+ if output_path and len(argv) > 1:
894
+ Output.error("InvalidArg", "--output can only be used with a single input file")
895
+
896
+ from pypdf import PdfReader, PdfWriter
897
+
898
+ for input_path in argv:
899
+ if not os.path.exists(input_path):
900
+ print(f"Error: Input file not found: {input_path}", file=sys.stderr)
901
+ continue
902
+
903
+ try:
904
+ reader = PdfReader(input_path)
905
+ except Exception as e:
906
+ print(f"Error: Cannot open PDF: {e}", file=sys.stderr)
907
+ continue
908
+
909
+ writer = PdfWriter()
910
+ for page in reader.pages:
911
+ writer.add_page(page)
912
+
913
+ # Determine title
914
+ if custom_title:
915
+ title = custom_title
916
+ else:
917
+ original_meta = reader.metadata
918
+ if original_meta and original_meta.title and original_meta.title not in ('(anonymous)', 'unspecified', None):
919
+ title = original_meta.title
920
+ else:
921
+ title = os.path.splitext(os.path.basename(input_path))[0]
922
+
923
+ writer.add_metadata({
924
+ '/Title': title,
925
+ '/Author': 'Z.ai',
926
+ '/Creator': 'Z.ai',
927
+ '/Producer': 'http://z.ai',
928
+ })
929
+
930
+ # Write output
931
+ out = output_path if (len(argv) == 1 and output_path) else input_path
932
+ try:
933
+ with open(out, "wb") as f:
934
+ writer.write(f)
935
+ except Exception as e:
936
+ print(f"Error: Cannot write output file: {e}", file=sys.stderr)
937
+ continue
938
+
939
+ if not quiet:
940
+ print(f"\u2713 Updated metadata for: {os.path.basename(input_path)}")
941
+ print(f" Title: {title}")
942
+ print(f" Author: Z.ai")
943
+ print(f" Creator: Z.ai")
944
+ print(f" Producer: http://z.ai")
945
+ if out != input_path:
946
+ print(f" Output: {out}")
947
+
948
+ raise SystemExit(0)
949
+
950
+
951
+ # ═══════════════════════════════════════════════════════════════
952
+ # Section 5: form — inspection, filling, annotation, rendering
953
+ # ═══════════════════════════════════════════════════════════════
954
+
955
+ # --- form.info (pikepdf-based) ---
956
+
957
+ _FIELD_TYPE_MAP = {
958
+ "/Tx": "text",
959
+ "/Sig": "signature",
960
+ }
961
+
962
+
963
+ def _classify_field(node) -> str:
964
+ """Map a PDF field type token to a human label."""
965
+ ft = str(node.get("/FT", ""))
966
+ if ft in _FIELD_TYPE_MAP:
967
+ return _FIELD_TYPE_MAP[ft]
968
+ flags = int(node.get("/Ff", 0))
969
+ if ft == "/Btn":
970
+ return "radio" if (flags & (1 << 15)) else "checkbox"
971
+ if ft == "/Ch":
972
+ return "dropdown" if (flags & (1 << 17)) else "listbox"
973
+ return "unknown"
974
+
975
+
976
+ def _extra_props(node, kind: str) -> dict:
977
+ """Gather type-specific metadata (options, checked value, etc.)."""
978
+ props: dict = {}
979
+ if kind == "checkbox":
980
+ ap = node.get("/AP")
981
+ if ap and "/N" in ap:
982
+ states = [str(s) for s in ap["/N"].keys()]
983
+ props["states"] = states
984
+ props["checked_value"] = next((s for s in states if s != "/Off"), states[0] if states else None)
985
+ elif kind in ("dropdown", "listbox"):
986
+ raw_opts = node.get("/Opt")
987
+ if raw_opts:
988
+ props["options"] = [
989
+ {"value": str(item[0]), "label": str(item[1])} if isinstance(item, list) and len(item) >= 2
990
+ else {"value": str(item), "label": str(item)}
991
+ for item in raw_opts
992
+ ]
993
+ elif kind == "radio":
994
+ kids = node.get("/Kids")
995
+ if kids:
996
+ radio_vals = []
997
+ for child in kids:
998
+ ap = child.get("/AP")
999
+ if ap and "/N" in ap:
1000
+ radio_vals.extend(str(k) for k in ap["/N"].keys() if str(k) != "/Off")
1001
+ if radio_vals:
1002
+ props["options"] = radio_vals
1003
+ return props
1004
+
1005
+
1006
+ def _current_value(node):
1007
+ v = node.get("/V")
1008
+ return str(v) if v is not None else None
1009
+
1010
+
1011
+ def _gather_fields(doc) -> list:
1012
+ """Walk the AcroForm field tree iteratively and return a flat list."""
1013
+ if "/AcroForm" not in doc.Root:
1014
+ return []
1015
+ acro = doc.Root.AcroForm
1016
+ if "/Fields" not in acro:
1017
+ return []
1018
+
1019
+ page_lookup = {pg.objgen: idx for idx, pg in enumerate(doc.pages)}
1020
+ results = []
1021
+ stack = [(field, "") for field in reversed(list(acro.Fields))]
1022
+
1023
+ while stack:
1024
+ node, parent_path = stack.pop()
1025
+ name = str(node.get("/T", ""))
1026
+ full = f"{parent_path}.{name}" if parent_path else name
1027
+
1028
+ kids = node.get("/Kids")
1029
+ if kids and any("/T" in k for k in kids):
1030
+ for kid in reversed(list(kids)):
1031
+ stack.append((kid, full))
1032
+ continue
1033
+
1034
+ kind = _classify_field(node)
1035
+ if kind == "unknown":
1036
+ continue
1037
+
1038
+ entry = {"id": full, "type": kind}
1039
+ val = _current_value(node)
1040
+ if val:
1041
+ entry["current_value"] = val
1042
+ entry.update(_extra_props(node, kind))
1043
+
1044
+ page_ref = node.get("/P")
1045
+ if page_ref and hasattr(page_ref, "objgen"):
1046
+ pg_num = page_lookup.get(page_ref.objgen)
1047
+ if pg_num is not None:
1048
+ entry["page"] = pg_num + 1
1049
+
1050
+ results.append(entry)
1051
+
1052
+ return results
1053
+
1054
+
1055
+ @cmd("form.info")
1056
+ def form_info(argv: list):
1057
+ """Return structured JSON describing every form field (pikepdf + check_fillable)."""
1058
+ if not argv:
1059
+ Output.error("MissingArg", "pdf path required")
1060
+
1061
+ import pikepdf
1062
+ src = Output.check_file(argv[0])
1063
+ try:
1064
+ doc = pikepdf.open(src)
1065
+ except Exception as exc:
1066
+ Output.error("PDFError", f"Cannot open PDF: {exc}", code=3)
1067
+
1068
+ fields = _gather_fields(doc)
1069
+ if not fields:
1070
+ Output.success({"has_fields": False, "count": 0, "fields": [], "hint": "This PDF has no fillable form fields"})
1071
+ Output.success({"has_fields": True, "count": len(fields), "fields": fields})
1072
+
1073
+
1074
+ @cmd("form.fill")
1075
+ def form_fill(argv: list):
1076
+ """Write values into a fillable PDF (pikepdf version)."""
1077
+ if not argv:
1078
+ Output.error("MissingArg", "pdf path required")
1079
+ pdf_path = argv.pop(0)
1080
+ out_path = _pop_flag(argv, "-o", "--output")
1081
+ if out_path is None:
1082
+ Output.error("MissingArg", "--output is required")
1083
+ data = _load_json_arg(argv)
1084
+
1085
+ import pikepdf
1086
+ src = Output.check_file(pdf_path)
1087
+ try:
1088
+ doc = pikepdf.open(src)
1089
+ except Exception as exc:
1090
+ Output.error("PDFError", f"Cannot open PDF: {exc}", code=3)
1091
+
1092
+ if "/AcroForm" not in doc.Root or "/Fields" not in doc.Root.AcroForm:
1093
+ Output.error("NoForm", "This PDF has no form fields")
1094
+
1095
+ known = {f["id"]: f for f in _gather_fields(doc)}
1096
+
1097
+ # Validation
1098
+ issues = []
1099
+ for fid, fval in data.items():
1100
+ if fid not in known:
1101
+ issues.append(f"Field not found: {fid}")
1102
+ continue
1103
+ fmeta = known[fid]
1104
+ ftype = fmeta["type"]
1105
+ if ftype == "checkbox" and "states" in fmeta:
1106
+ ok_vals = fmeta["states"]
1107
+ if fval not in ok_vals and f"/{fval}" not in ok_vals and fval not in ("true", "True", "false", "False", "1", "0"):
1108
+ issues.append(f"Invalid value for field {fid}, options: {ok_vals} or true/false")
1109
+ if ftype in ("dropdown", "listbox") and "options" in fmeta:
1110
+ ok_vals = [o["value"] for o in fmeta["options"]]
1111
+ if fval not in ok_vals:
1112
+ issues.append(f"Invalid value for field {fid}, options: {ok_vals}")
1113
+ if issues:
1114
+ Output.error("ValidationError", "Field validation failed", hint="; ".join(issues))
1115
+
1116
+ # Fill
1117
+ written = 0
1118
+
1119
+ def _apply(node, parent_path=""):
1120
+ nonlocal written
1121
+ name = str(node.get("/T", ""))
1122
+ full = f"{parent_path}.{name}" if parent_path else name
1123
+ kids = node.get("/Kids")
1124
+ if kids and any("/T" in k for k in kids):
1125
+ for kid in kids:
1126
+ _apply(kid, full)
1127
+ return
1128
+ if full not in data:
1129
+ return
1130
+ val = data[full]
1131
+ kind = _classify_field(node)
1132
+ if kind == "checkbox":
1133
+ if val in ("true", "True", "1", True):
1134
+ ap = node.get("/AP")
1135
+ if ap and "/N" in ap:
1136
+ checked_name = next((str(k) for k in ap["/N"].keys() if str(k) != "/Off"), "/Yes")
1137
+ if not checked_name.startswith("/"):
1138
+ checked_name = f"/{checked_name}"
1139
+ node["/V"] = pikepdf.Name(checked_name)
1140
+ node["/AS"] = pikepdf.Name(checked_name)
1141
+ else:
1142
+ node["/V"] = pikepdf.Name("/Off")
1143
+ node["/AS"] = pikepdf.Name("/Off")
1144
+ else:
1145
+ node["/V"] = pikepdf.String(str(val))
1146
+ written += 1
1147
+
1148
+ for field in doc.Root.AcroForm.Fields:
1149
+ _apply(field)
1150
+
1151
+ acro = doc.Root.AcroForm
1152
+ if "/NeedAppearances" not in acro:
1153
+ acro["/NeedAppearances"] = True
1154
+
1155
+ try:
1156
+ doc.save(out_path)
1157
+ except Exception as exc:
1158
+ Output.error("SaveError", f"Save failed: {exc}", code=4)
1159
+
1160
+ Output.success({"output": out_path, "fields_filled": written, "fields_requested": len(data)})
1161
+
1162
+
1163
+ # --- form.detail (pypdf-based detailed field extraction) ---
1164
+
1165
+ def _get_full_annotation_field_id(annotation):
1166
+ """Build dotted field ID by walking parent chain."""
1167
+ components = []
1168
+ while annotation:
1169
+ field_name = annotation.get('/T')
1170
+ if field_name:
1171
+ components.append(field_name)
1172
+ annotation = annotation.get('/Parent')
1173
+ return ".".join(reversed(components)) if components else None
1174
+
1175
+
1176
+ def _make_field_dict(field, field_id):
1177
+ field_dict = {"field_id": field_id}
1178
+ ft = field.get('/FT')
1179
+ if ft == "/Tx":
1180
+ field_dict["type"] = "text"
1181
+ elif ft == "/Btn":
1182
+ field_dict["type"] = "checkbox"
1183
+ states = field.get("/_States_", [])
1184
+ if len(states) == 2:
1185
+ if "/Off" in states:
1186
+ field_dict["checked_value"] = states[0] if states[0] != "/Off" else states[1]
1187
+ field_dict["unchecked_value"] = "/Off"
1188
+ else:
1189
+ print(f"Unexpected state values for checkbox `${field_id}`. Its checked and unchecked values may not be correct; if you're trying to check it, visually verify the results.")
1190
+ field_dict["checked_value"] = states[0]
1191
+ field_dict["unchecked_value"] = states[1]
1192
+ elif ft == "/Ch":
1193
+ field_dict["type"] = "choice"
1194
+ states = field.get("/_States_", [])
1195
+ field_dict["choice_options"] = [{
1196
+ "value": state[0],
1197
+ "text": state[1],
1198
+ } for state in states]
1199
+ else:
1200
+ field_dict["type"] = f"unknown ({ft})"
1201
+ return field_dict
1202
+
1203
+
1204
+ def _get_field_info(reader) -> list:
1205
+ """Extract detailed field info from a PdfReader, including radio group aggregation."""
1206
+ fields = reader.get_fields()
1207
+
1208
+ field_info_by_id = {}
1209
+ possible_radio_names: Set[str] = set()
1210
+
1211
+ for field_id, field in fields.items():
1212
+ if field.get("/Kids"):
1213
+ if field.get("/FT") == "/Btn":
1214
+ possible_radio_names.add(field_id)
1215
+ continue
1216
+ field_info_by_id[field_id] = _make_field_dict(field, field_id)
1217
+
1218
+ radio_fields_by_id: Dict[str, dict] = {}
1219
+
1220
+ for page_index, page in enumerate(reader.pages):
1221
+ annotations = page.get('/Annots', [])
1222
+ for ann in annotations:
1223
+ field_id = _get_full_annotation_field_id(ann)
1224
+ if field_id in field_info_by_id:
1225
+ field_info_by_id[field_id]["page"] = page_index + 1
1226
+ field_info_by_id[field_id]["rect"] = ann.get('/Rect')
1227
+ elif field_id in possible_radio_names:
1228
+ try:
1229
+ on_values = [v for v in ann["/AP"]["/N"] if v != "/Off"]
1230
+ except KeyError:
1231
+ continue
1232
+ if len(on_values) == 1:
1233
+ rect = ann.get("/Rect")
1234
+ if field_id not in radio_fields_by_id:
1235
+ radio_fields_by_id[field_id] = {
1236
+ "field_id": field_id,
1237
+ "type": "radio_group",
1238
+ "page": page_index + 1,
1239
+ "radio_options": [],
1240
+ }
1241
+ radio_fields_by_id[field_id]["radio_options"].append({
1242
+ "value": on_values[0],
1243
+ "rect": rect,
1244
+ })
1245
+
1246
+ # Filter fields without location
1247
+ fields_with_location = []
1248
+ for field_info in field_info_by_id.values():
1249
+ if "page" in field_info:
1250
+ fields_with_location.append(field_info)
1251
+ else:
1252
+ print(f"Unable to determine location for field id: {field_info.get('field_id')}, ignoring")
1253
+
1254
+ # Sort by page number, then Y position (flipped), then X
1255
+ def sort_key(f):
1256
+ if "radio_options" in f:
1257
+ rect = f["radio_options"][0]["rect"] or [0, 0, 0, 0]
1258
+ else:
1259
+ rect = f.get("rect") or [0, 0, 0, 0]
1260
+ adjusted_position = [-rect[1], rect[0]]
1261
+ return [f.get("page"), adjusted_position]
1262
+
1263
+ sorted_fields = fields_with_location + list(radio_fields_by_id.values())
1264
+ sorted_fields.sort(key=sort_key)
1265
+
1266
+ return sorted_fields
1267
+
1268
+
1269
+ @cmd("form.detail")
1270
+ def form_detail(argv: list):
1271
+ """Extract detailed field info (pypdf version) to JSON."""
1272
+ if len(argv) < 2:
1273
+ Output.error("MissingArg", "Usage: form.detail <pdf> <output.json>")
1274
+ pdf_path = argv[0]
1275
+ json_output_path = argv[1]
1276
+
1277
+ from pypdf import PdfReader
1278
+ reader = PdfReader(pdf_path)
1279
+ field_info = _get_field_info(reader)
1280
+ with open(json_output_path, "w") as f:
1281
+ json.dump(field_info, f, indent=2)
1282
+ print(f"Wrote {len(field_info)} fields to {json_output_path}")
1283
+ raise SystemExit(0)
1284
+
1285
+
1286
+ # --- form.fill-legacy (pypdf version with monkeypatch) ---
1287
+
1288
+ def _validation_error_for_field_value(field_info, field_value):
1289
+ field_type = field_info["type"]
1290
+ field_id = field_info["field_id"]
1291
+ if field_type == "checkbox":
1292
+ checked_val = field_info["checked_value"]
1293
+ unchecked_val = field_info["unchecked_value"]
1294
+ if field_value != checked_val and field_value != unchecked_val:
1295
+ return f'ERROR: Invalid value "{field_value}" for checkbox field "{field_id}". The checked value is "{checked_val}" and the unchecked value is "{unchecked_val}"'
1296
+ elif field_type == "radio_group":
1297
+ option_values = [opt["value"] for opt in field_info["radio_options"]]
1298
+ if field_value not in option_values:
1299
+ return f'ERROR: Invalid value "{field_value}" for radio group field "{field_id}". Valid values are: {option_values}'
1300
+ elif field_type == "choice":
1301
+ choice_values = [opt["value"] for opt in field_info["choice_options"]]
1302
+ if field_value not in choice_values:
1303
+ return f'ERROR: Invalid value "{field_value}" for choice field "{field_id}". Valid values are: {choice_values}'
1304
+ return None
1305
+
1306
+
1307
+ def _monkeypatch_pypdf_method():
1308
+ """
1309
+ Workaround for pypdf bug with selection list fields.
1310
+ pypdf's get_inherited returns a list of two-element lists for /Opt fields
1311
+ in selection lists, causing join() to throw TypeError. We patch it to
1312
+ return just the value strings.
1313
+ """
1314
+ from pypdf.generic import DictionaryObject
1315
+ from pypdf.constants import FieldDictionaryAttributes
1316
+
1317
+ original_get_inherited = DictionaryObject.get_inherited
1318
+
1319
+ def patched_get_inherited(self, key: str, default=None):
1320
+ result = original_get_inherited(self, key, default)
1321
+ if key == FieldDictionaryAttributes.Opt:
1322
+ if isinstance(result, list) and all(isinstance(v, list) and len(v) == 2 for v in result):
1323
+ result = [r[0] for r in result]
1324
+ return result
1325
+
1326
+ DictionaryObject.get_inherited = patched_get_inherited
1327
+
1328
+
1329
+ @cmd("form.fill-legacy")
1330
+ def form_fill_legacy(argv: list):
1331
+ """Fill fillable form fields (pypdf version with monkeypatch)."""
1332
+ if len(argv) < 3:
1333
+ Output.error("MissingArg", "Usage: form.fill-legacy <pdf> <fields.json> <output.pdf>")
1334
+ input_pdf = argv[0]
1335
+ fields_json = argv[1]
1336
+ output_pdf = argv[2]
1337
+
1338
+ from pypdf import PdfReader, PdfWriter
1339
+
1340
+ _monkeypatch_pypdf_method()
1341
+
1342
+ with open(fields_json) as f:
1343
+ fields = json.load(f)
1344
+
1345
+ # Group by page number
1346
+ fields_by_page: Dict[int, dict] = {}
1347
+ for field in fields:
1348
+ if "value" in field:
1349
+ field_id = field["field_id"]
1350
+ page = field["page"]
1351
+ if page not in fields_by_page:
1352
+ fields_by_page[page] = {}
1353
+ fields_by_page[page][field_id] = field["value"]
1354
+
1355
+ reader = PdfReader(input_pdf)
1356
+
1357
+ has_error = False
1358
+ field_info = _get_field_info(reader)
1359
+ fields_by_ids = {f["field_id"]: f for f in field_info}
1360
+ for field in fields:
1361
+ existing_field = fields_by_ids.get(field["field_id"])
1362
+ if not existing_field:
1363
+ has_error = True
1364
+ print(f"ERROR: `{field['field_id']}` is not a valid field ID")
1365
+ elif field["page"] != existing_field["page"]:
1366
+ has_error = True
1367
+ print(f"ERROR: Incorrect page number for `{field['field_id']}` (got {field['page']}, expected {existing_field['page']})")
1368
+ else:
1369
+ if "value" in field:
1370
+ err = _validation_error_for_field_value(existing_field, field["value"])
1371
+ if err:
1372
+ print(err)
1373
+ has_error = True
1374
+ if has_error:
1375
+ raise SystemExit(1)
1376
+
1377
+ writer = PdfWriter(clone_from=reader)
1378
+ for page, field_values in fields_by_page.items():
1379
+ writer.update_page_form_field_values(writer.pages[page - 1], field_values, auto_regenerate=False)
1380
+
1381
+ writer.set_need_appearances_writer(True)
1382
+
1383
+ with open(output_pdf, "wb") as f:
1384
+ writer.write(f)
1385
+
1386
+ print(f"Filled {len(fields_by_page)} page(s) in {output_pdf}")
1387
+ raise SystemExit(0)
1388
+
1389
+
1390
+ # --- form.annotate (annotation-based filling with coordinate transform) ---
1391
+
1392
+ def _transform_coordinates(bbox, image_width, image_height, pdf_width, pdf_height):
1393
+ """Transform bounding box from image coordinates to PDF coordinates."""
1394
+ x_scale = pdf_width / image_width
1395
+ y_scale = pdf_height / image_height
1396
+
1397
+ left = bbox[0] * x_scale
1398
+ right = bbox[2] * x_scale
1399
+
1400
+ # Flip Y coordinates for PDF
1401
+ top = pdf_height - (bbox[1] * y_scale)
1402
+ bottom = pdf_height - (bbox[3] * y_scale)
1403
+
1404
+ return left, bottom, right, top
1405
+
1406
+
1407
+ def _normalise_fields_json(raw: dict) -> dict:
1408
+ """Accept both the current sheet-based schema and the legacy flat schema.
1409
+
1410
+ Current (v2) schema uses ``sheet[].pg/dims/regions[]`` with nested
1411
+ ``label.bbox``, ``target.bbox``, ``ink{}``.
1412
+
1413
+ Legacy (v1) schema uses ``pages[]`` + ``form_fields[]`` with flat keys
1414
+ like ``entry_bounding_box``, ``label_bounding_box``, ``entry_text{}``.
1415
+
1416
+ Returns a normalised dict in the **v2** internal format used by all
1417
+ downstream functions.
1418
+ """
1419
+ # Already v2
1420
+ if "sheet" in raw:
1421
+ return raw
1422
+
1423
+ # Convert legacy → v2
1424
+ pages_lut = {p["page_number"]: p for p in raw.get("pages", [])}
1425
+ sheets: dict = {} # pg -> sheet entry
1426
+
1427
+ for f in raw.get("form_fields", []):
1428
+ pg = f["page_number"]
1429
+ if pg not in sheets:
1430
+ pi = pages_lut.get(pg, {})
1431
+ sheets[pg] = {
1432
+ "pg": pg,
1433
+ "dims": [pi.get("image_width", 0), pi.get("image_height", 0)],
1434
+ "regions": [],
1435
+ }
1436
+ et = f.get("entry_text", {})
1437
+ region = {
1438
+ "id": f.get("field_label", f.get("description", "")),
1439
+ "hint": f.get("description", ""),
1440
+ "label": {"tag": f.get("field_label", ""), "bbox": f.get("label_bounding_box", [0, 0, 0, 0])},
1441
+ "target": {"bbox": f.get("entry_bounding_box", [0, 0, 0, 0])},
1442
+ "ink": {},
1443
+ }
1444
+ if isinstance(et, dict) and et.get("text"):
1445
+ region["ink"]["value"] = et["text"]
1446
+ if "font_size" in et:
1447
+ region["ink"]["size"] = et["font_size"]
1448
+ if "font_color" in et:
1449
+ region["ink"]["color"] = et["font_color"]
1450
+ if "font" in et:
1451
+ region["ink"]["font"] = et["font"]
1452
+ sheets[pg]["regions"].append(region)
1453
+
1454
+ return {"sheet": list(sheets.values())}
1455
+
1456
+
1457
+ @cmd("form.annotate")
1458
+ def form_annotate(argv: list):
1459
+ """Fill a PDF by adding text annotations (FreeText) defined in fields.json."""
1460
+ if len(argv) < 3:
1461
+ Output.error("MissingArg", "Usage: form.annotate <pdf> <fields.json> <output.pdf>")
1462
+ input_pdf = argv[0]
1463
+ fields_json_path = argv[1]
1464
+ output_pdf = argv[2]
1465
+
1466
+ from pypdf import PdfReader, PdfWriter
1467
+ from pypdf.annotations import FreeText
1468
+
1469
+ with open(fields_json_path, "r") as f:
1470
+ fields_data = _normalise_fields_json(json.load(f))
1471
+
1472
+ reader = PdfReader(input_pdf)
1473
+ writer = PdfWriter()
1474
+ writer.append(reader)
1475
+
1476
+ # Get PDF dimensions for each page
1477
+ pdf_dimensions = {}
1478
+ for i, page in enumerate(reader.pages):
1479
+ mediabox = page.mediabox
1480
+ pdf_dimensions[i + 1] = [mediabox.width, mediabox.height]
1481
+
1482
+ annotations = []
1483
+ for page_entry in fields_data["sheet"]:
1484
+ pg = page_entry["pg"]
1485
+ image_width, image_height = page_entry["dims"]
1486
+ pdf_width, pdf_height = pdf_dimensions[pg]
1487
+
1488
+ for region in page_entry["regions"]:
1489
+ ink = region.get("ink", {})
1490
+ text = ink.get("value", "")
1491
+ if not text:
1492
+ continue
1493
+
1494
+ transformed_box = _transform_coordinates(
1495
+ region["target"]["bbox"],
1496
+ image_width, image_height,
1497
+ pdf_width, pdf_height
1498
+ )
1499
+
1500
+ font_name = ink.get("font", "Arial")
1501
+ font_size = str(ink.get("size", 14)) + "pt"
1502
+ font_color = ink.get("color", "000000")
1503
+
1504
+ annotation = FreeText(
1505
+ text=text,
1506
+ rect=transformed_box,
1507
+ font=font_name,
1508
+ font_size=font_size,
1509
+ font_color=font_color,
1510
+ border_color=None,
1511
+ background_color=None,
1512
+ )
1513
+ annotations.append(annotation)
1514
+ writer.add_annotation(page_number=pg - 1, annotation=annotation)
1515
+
1516
+ with open(output_pdf, "wb") as output:
1517
+ writer.write(output)
1518
+
1519
+ print(f"Successfully filled PDF form and saved to {output_pdf}")
1520
+ print(f"Added {len(annotations)} text annotations")
1521
+ raise SystemExit(0)
1522
+
1523
+
1524
+ # --- form.render (PDF to PNG images) ---
1525
+
1526
+ @cmd("form.render")
1527
+ def form_render(argv: list):
1528
+ """Convert each page of a PDF to a PNG image."""
1529
+ if len(argv) < 2:
1530
+ Output.error("MissingArg", "Usage: form.render <pdf> <output_dir> [--max-dim N]")
1531
+ pdf_path = argv.pop(0)
1532
+ output_dir = argv.pop(0)
1533
+ max_dim_str = _pop_flag(argv, "-m", "--max-dim")
1534
+ max_dim = int(max_dim_str) if max_dim_str else 1000
1535
+
1536
+ from pdf2image import convert_from_path
1537
+
1538
+ os.makedirs(output_dir, exist_ok=True)
1539
+ images = convert_from_path(pdf_path, dpi=200)
1540
+
1541
+ for i, image in enumerate(images):
1542
+ width, height = image.size
1543
+ if width > max_dim or height > max_dim:
1544
+ scale_factor = min(max_dim / width, max_dim / height)
1545
+ new_width = int(width * scale_factor)
1546
+ new_height = int(height * scale_factor)
1547
+ image = image.resize((new_width, new_height))
1548
+
1549
+ image_path = os.path.join(output_dir, f"page_{i+1}.png")
1550
+ image.save(image_path)
1551
+ print(f"Saved page {i+1} as {image_path} (size: {image.size})")
1552
+
1553
+ print(f"Converted {len(images)} pages to PNG images")
1554
+ raise SystemExit(0)
1555
+
1556
+
1557
+ # --- form.validate (bounding box validation image) ---
1558
+
1559
+ @cmd("form.validate")
1560
+ def form_validate(argv: list):
1561
+ """Create validation images with bounding box rectangles."""
1562
+ if len(argv) < 4:
1563
+ Output.error("MissingArg", "Usage: form.validate <page> <fields.json> <input_img> <output_img>")
1564
+ page_number = int(argv[0])
1565
+ fields_json_path = argv[1]
1566
+ input_path = argv[2]
1567
+ output_path = argv[3]
1568
+
1569
+ from PIL import Image, ImageDraw
1570
+
1571
+ with open(fields_json_path, 'r') as f:
1572
+ data = _normalise_fields_json(json.load(f))
1573
+
1574
+ img = Image.open(input_path)
1575
+ draw = ImageDraw.Draw(img)
1576
+ num_boxes = 0
1577
+
1578
+ for page_entry in data["sheet"]:
1579
+ if page_entry["pg"] != page_number:
1580
+ continue
1581
+ for region in page_entry["regions"]:
1582
+ target_box = region["target"]["bbox"]
1583
+ label_box = region["label"]["bbox"]
1584
+ draw.rectangle(target_box, outline='red', width=2)
1585
+ draw.rectangle(label_box, outline='blue', width=2)
1586
+ num_boxes += 2
1587
+
1588
+ img.save(output_path)
1589
+ print(f"Created validation image at {output_path} with {num_boxes} bounding boxes")
1590
+ raise SystemExit(0)
1591
+
1592
+
1593
+ # --- form.check-bbox (bounding box overlap detection) ---
1594
+
1595
+ @dataclass
1596
+ class _RectAndField:
1597
+ rect: list
1598
+ rect_type: str
1599
+ field: dict
1600
+
1601
+
1602
+ def get_bounding_box_messages(fields_json_stream) -> List[str]:
1603
+ """Check for overlapping bounding boxes. Returns list of messages (max 20)."""
1604
+ messages = []
1605
+ raw = json.load(fields_json_stream)
1606
+ data = _normalise_fields_json(raw)
1607
+
1608
+ total_regions = sum(len(pe["regions"]) for pe in data["sheet"])
1609
+ messages.append(f"Read {total_regions} regions across {len(data['sheet'])} page(s)")
1610
+
1611
+ def rects_intersect(r1, r2):
1612
+ disjoint_horizontal = r1[0] >= r2[2] or r1[2] <= r2[0]
1613
+ disjoint_vertical = r1[1] >= r2[3] or r1[3] <= r2[1]
1614
+ return not (disjoint_horizontal or disjoint_vertical)
1615
+
1616
+ has_error = False
1617
+
1618
+ for page_entry in data["sheet"]:
1619
+ pg = page_entry["pg"]
1620
+ # Collect all rects on this page
1621
+ rects_and_regions = []
1622
+ for region in page_entry["regions"]:
1623
+ rects_and_regions.append(_RectAndField(region["label"]["bbox"], "label", region))
1624
+ rects_and_regions.append(_RectAndField(region["target"]["bbox"], "target", region))
1625
+
1626
+ for i, ri in enumerate(rects_and_regions):
1627
+ for j in range(i + 1, len(rects_and_regions)):
1628
+ rj = rects_and_regions[j]
1629
+ if rects_intersect(ri.rect, rj.rect):
1630
+ has_error = True
1631
+ rid = ri.field.get("id", ri.field.get("hint", "?"))
1632
+ rjd = rj.field.get("id", rj.field.get("hint", "?"))
1633
+ if ri.field is rj.field:
1634
+ messages.append(f"FAILURE: pg {pg} — label/target overlap for `{rid}` ({ri.rect}, {rj.rect})")
1635
+ else:
1636
+ messages.append(f"FAILURE: pg {pg} — {ri.rect_type} of `{rid}` ({ri.rect}) overlaps {rj.rect_type} of `{rjd}` ({rj.rect})")
1637
+ if len(messages) >= 20:
1638
+ messages.append("Aborting further checks; fix bounding boxes and try again")
1639
+ return messages
1640
+
1641
+ # Height check for target rects
1642
+ if ri.rect_type == "target":
1643
+ ink = ri.field.get("ink", {})
1644
+ if ink.get("value"):
1645
+ font_size = ink.get("size", 14)
1646
+ entry_height = ri.rect[3] - ri.rect[1]
1647
+ if entry_height < font_size:
1648
+ has_error = True
1649
+ rid = ri.field.get("id", ri.field.get("hint", "?"))
1650
+ messages.append(f"FAILURE: pg {pg} — target box height ({entry_height}) for `{rid}` is shorter than font size ({font_size}). Increase box height or decrease ink.size.")
1651
+ if len(messages) >= 20:
1652
+ messages.append("Aborting further checks; fix bounding boxes and try again")
1653
+ return messages
1654
+
1655
+ if not has_error:
1656
+ messages.append("SUCCESS: All bounding boxes are valid")
1657
+ return messages
1658
+
1659
+
1660
+ @cmd("form.check-bbox")
1661
+ def form_check_bbox(argv: list):
1662
+ """Check bounding boxes in fields.json for overlaps."""
1663
+ if not argv:
1664
+ Output.error("MissingArg", "Usage: form.check-bbox <fields.json>")
1665
+ with open(argv[0]) as f:
1666
+ messages = get_bounding_box_messages(f)
1667
+ for msg in messages:
1668
+ print(msg)
1669
+ raise SystemExit(0)
1670
+
1671
+
1672
+ # ═══════════════════════════════════════════════════════════════
1673
+ # Section 6: convert — office, HTML, and LaTeX
1674
+ # ═══════════════════════════════════════════════════════════════
1675
+
1676
+ _CONVERTIBLE_EXTENSIONS = frozenset({
1677
+ ".docx", ".doc", ".odt", ".rtf",
1678
+ ".pptx", ".ppt", ".odp",
1679
+ ".xlsx", ".xls", ".ods", ".csv",
1680
+ ".txt", ".html", ".htm",
1681
+ })
1682
+
1683
+ _SOFFICE_CANDIDATES = [
1684
+ "/Applications/LibreOffice.app/Contents/MacOS/soffice",
1685
+ os.path.expanduser("~/Applications/LibreOffice.app/Contents/MacOS/soffice"),
1686
+ "/usr/bin/soffice",
1687
+ "/usr/local/bin/soffice",
1688
+ "/usr/lib/libreoffice/program/soffice",
1689
+ "/opt/libreoffice/program/soffice",
1690
+ "/snap/bin/libreoffice.soffice",
1691
+ ]
1692
+
1693
+
1694
+ def _locate_soffice() -> Optional[str]:
1695
+ """Search for a working soffice binary."""
1696
+ for candidate in _SOFFICE_CANDIDATES:
1697
+ if Path(candidate).is_file():
1698
+ return candidate
1699
+ for alias in ("soffice", "libreoffice"):
1700
+ found = shutil.which(alias)
1701
+ if found:
1702
+ return found
1703
+ return None
1704
+
1705
+
1706
+ @cmd("convert.office")
1707
+ def convert_office(argv: list):
1708
+ """Convert an office document to PDF via LibreOffice."""
1709
+ if not argv:
1710
+ Output.error("MissingArg", "input file required")
1711
+ src_path = argv.pop(0)
1712
+ out_path = _pop_flag(argv, "-o", "--output")
1713
+
1714
+ src = Output.check_file(src_path)
1715
+ ext = src.suffix.lower()
1716
+
1717
+ if ext not in _CONVERTIBLE_EXTENSIONS:
1718
+ Output.error(
1719
+ "UnsupportedFormat",
1720
+ f"Unsupported format: {ext}",
1721
+ hint=f"Supported formats: {', '.join(sorted(_CONVERTIBLE_EXTENSIONS))}",
1722
+ )
1723
+
1724
+ binary = _locate_soffice()
1725
+ if binary is None:
1726
+ Output.error(
1727
+ "DependencyMissing",
1728
+ "LibreOffice not found",
1729
+ hint="Please install LibreOffice: https://www.libreoffice.org/download/",
1730
+ )
1731
+
1732
+ target_dir = Path(out_path).parent if out_path else src.parent
1733
+ target_dir.mkdir(parents=True, exist_ok=True)
1734
+
1735
+ cmd_list = [binary, "--headless", "--convert-to", "pdf", "--outdir", str(target_dir), str(src)]
1736
+
1737
+ try:
1738
+ proc = subprocess.run(cmd_list, capture_output=True, text=True, timeout=120)
1739
+ if proc.returncode != 0:
1740
+ Output.error("ConvertError", f"Conversion failed: {proc.stderr.strip() or 'Unknown error'}", code=4)
1741
+ except subprocess.TimeoutExpired:
1742
+ Output.error("Timeout", "Conversion timeout (>120s)", code=4)
1743
+ except Exception as exc:
1744
+ Output.error("ConvertError", f"Conversion failed: {exc}", code=4)
1745
+
1746
+ auto_name = target_dir / f"{src.stem}.pdf"
1747
+ final = Path(out_path) if out_path else auto_name
1748
+
1749
+ if out_path and final.name != auto_name.name and auto_name.exists():
1750
+ auto_name.rename(final)
1751
+
1752
+ if not final.exists():
1753
+ Output.error("ConvertError", "Converted PDF file was not generated", code=4)
1754
+
1755
+ Output.success({"input": str(src), "output": str(final), "format": ext})
1756
+
1757
+
1758
+ @cmd("convert.html")
1759
+ def convert_html(argv: list):
1760
+ """Convert HTML to PDF via node html2pdf.js."""
1761
+ if not argv:
1762
+ Output.error("MissingArg", "input file required")
1763
+
1764
+ js_path = _SCRIPT_DIR / "html2pdf.js"
1765
+ if not js_path.exists():
1766
+ Output.error("DependencyMissing", "html2pdf.js not found in scripts directory")
1767
+
1768
+ node_path = shutil.which("node")
1769
+ if not node_path:
1770
+ Output.error("DependencyMissing", "node not found in PATH")
1771
+
1772
+ cmd_list = [node_path, str(js_path)] + argv
1773
+ try:
1774
+ proc = subprocess.run(cmd_list, timeout=180)
1775
+ raise SystemExit(proc.returncode)
1776
+ except subprocess.TimeoutExpired:
1777
+ Output.error("Timeout", "HTML conversion timeout (>180s)", code=4)
1778
+ except SystemExit:
1779
+ raise
1780
+ except Exception as exc:
1781
+ Output.error("ConvertError", f"HTML conversion failed: {exc}", code=4)
1782
+
1783
+
1784
+ # --- convert.latex (tectonic wrapper with log filtering + PDF stats) ---
1785
+
1786
+ _NOISE_RE = re.compile(
1787
+ r"^note: (?:"
1788
+ r'"version 2" Tectonic'
1789
+ r"|Running TeX"
1790
+ r"|Rerunning TeX because"
1791
+ r"|Running xdvipdfmx"
1792
+ r"|downloading "
1793
+ r"|Skipped writing .* intermediate files"
1794
+ r")"
1795
+ )
1796
+
1797
+
1798
+ def _find_tectonic() -> Optional[str]:
1799
+ """Locate the tectonic binary.
1800
+
1801
+ Search order:
1802
+ 1. scripts/ dir (bundled binary — macOS arm64 only)
1803
+ 2. ~/tectonic (user-placed binary)
1804
+ 3. System PATH (package-manager install)
1805
+
1806
+ NOTE: The bundled binary at scripts/tectonic is a macOS arm64 (Apple
1807
+ Silicon) Mach-O executable. It will NOT run on Linux or Windows.
1808
+ On other platforms, install tectonic via the system package manager
1809
+ or download the correct binary — see the error message in
1810
+ convert_latex() for instructions.
1811
+ """
1812
+ local_bin = _SCRIPT_DIR / "tectonic"
1813
+ if local_bin.exists() and os.access(local_bin, os.X_OK):
1814
+ return str(local_bin)
1815
+ home_bin = Path.home() / "tectonic"
1816
+ if home_bin.exists() and os.access(home_bin, os.X_OK):
1817
+ return str(home_bin)
1818
+ system_bin = shutil.which("tectonic")
1819
+ return system_bin
1820
+
1821
+
1822
+ def _human_size(nbytes: int) -> str:
1823
+ for unit in ("B", "KB", "MB", "GB"):
1824
+ if nbytes < 1024:
1825
+ return f"{nbytes:.2f} {unit}"
1826
+ nbytes /= 1024
1827
+ return f"{nbytes:.2f} TB"
1828
+
1829
+
1830
+ def _pdf_stats(pdf_file: Path):
1831
+ """Return (pages, word_count, image_count) or Nones."""
1832
+ try:
1833
+ from pypdf import PdfReader
1834
+ except ImportError:
1835
+ for attempt in (
1836
+ [sys.executable, "-m", "pip", "install", "-q", "pypdf"],
1837
+ [sys.executable, "-m", "pip", "install", "-q", "--break-system-packages", "pypdf"],
1838
+ [sys.executable, "-m", "pip", "install", "-q", "--user", "pypdf"],
1839
+ ):
1840
+ if subprocess.run(attempt, check=False, capture_output=True).returncode == 0:
1841
+ break
1842
+ try:
1843
+ from pypdf import PdfReader
1844
+ except ImportError:
1845
+ return None, None, None
1846
+
1847
+ try:
1848
+ reader = PdfReader(str(pdf_file))
1849
+ n_pages = len(reader.pages)
1850
+ all_text = "".join(p.extract_text() or "" for p in reader.pages)
1851
+ n_words = len([w for w in all_text.split() if w.strip()])
1852
+ n_images = 0
1853
+ for pg in reader.pages:
1854
+ xobj = pg.get("/Resources", {}).get("/XObject")
1855
+ if xobj:
1856
+ obj = xobj.get_object()
1857
+ n_images += sum(1 for k in obj if obj[k].get("/Subtype") == "/Image")
1858
+ return n_pages, n_words, n_images
1859
+ except Exception as exc:
1860
+ print(f"Error extracting PDF info: {exc}", file=sys.stderr)
1861
+ return None, None, None
1862
+
1863
+
1864
+ def _classify_lines(lines):
1865
+ """Bucket raw output into errors / warnings / layout issues."""
1866
+ errors, warnings, layout, pdf_note = [], [], [], None
1867
+ for raw in lines:
1868
+ ln = raw.rstrip()
1869
+ if not ln:
1870
+ continue
1871
+ if _NOISE_RE.match(ln):
1872
+ if ln.startswith("note: Writing"):
1873
+ pdf_note = ln
1874
+ continue
1875
+ if ln.startswith("error:"):
1876
+ errors.append(ln)
1877
+ elif ln.startswith("warning:"):
1878
+ warnings.append(ln)
1879
+ elif re.search(r"(Overfull|Underfull) \\[hv]box", ln) or re.search(r"(Font shape|Missing character)", ln):
1880
+ layout.append(ln)
1881
+ return errors, warnings, layout, pdf_note
1882
+
1883
+
1884
+ def _parse_writing_note(note: Optional[str]):
1885
+ m = re.search(r"Writing `(.+?)` \((.+?)\)", note or "")
1886
+ return (m.group(1), m.group(2)) if m else (None, None)
1887
+
1888
+
1889
+ @cmd("convert.blueprint")
1890
+ def convert_blueprint(argv: list):
1891
+ """
1892
+ [Auto-Pipeline] Extract JSON blueprint from LLM markdown response,
1893
+ compile it to HTML via design_engine, and render to PDF.
1894
+ """
1895
+ if not argv:
1896
+ Output.error("MissingArg", "Usage: convert.blueprint <llm_response_file.md> [-o <out.pdf>]")
1897
+
1898
+ input_file = argv.pop(0)
1899
+ out_pdf = _pop_flag(argv, "-o", "--output") or "output.pdf"
1900
+
1901
+ src = Output.check_file(input_file)
1902
+ content = src.read_text(encoding="utf-8")
1903
+
1904
+ # 1. Smart JSON extraction (regardless of whether LLM wrapped in Markdown code blocks)
1905
+ match = re.search(r'```(?:json)?\s*(\{.*?\})\s*```', content, re.DOTALL)
1906
+ if match:
1907
+ json_str = match.group(1)
1908
+ else:
1909
+ # Fallback: Assume the file is pure JSON
1910
+ json_str = content
1911
+
1912
+ try:
1913
+ # Validate JSON format
1914
+ parsed_json = json.loads(json_str)
1915
+ except json.JSONDecodeError as e:
1916
+ Output.error("InvalidJSON", f"Failed to parse JSON blueprint: {e}", hint="Ensure the LLM output is valid JSON without trailing commas.")
1917
+
1918
+ # 1b. Encoding integrity check — detect corrupted characters before rendering
1919
+ corruption_found = False
1920
+ def _scan_for_corruption(obj, path=""):
1921
+ """Recursively scan JSON values for encoding corruption markers."""
1922
+ nonlocal corruption_found
1923
+ if isinstance(obj, str):
1924
+ for i, ch in enumerate(obj):
1925
+ if ch == '\ufffd': # Unicode replacement character
1926
+ context = obj[max(0, i-10):i+10]
1927
+ print(f" \u26a0\ufe0f U+FFFD at {path}[{i}]: ...{context}...")
1928
+ corruption_found = True
1929
+ elif '\ud800' <= ch <= '\udfff': # Lone surrogate
1930
+ print(f" \u26a0\ufe0f Lone surrogate U+{ord(ch):04X} at {path}[{i}]")
1931
+ corruption_found = True
1932
+ elif isinstance(obj, dict):
1933
+ for k, v in obj.items():
1934
+ _scan_for_corruption(v, f"{path}.{k}")
1935
+ elif isinstance(obj, list):
1936
+ for idx, v in enumerate(obj):
1937
+ _scan_for_corruption(v, f"{path}[{idx}]")
1938
+
1939
+ _scan_for_corruption(parsed_json)
1940
+ if corruption_found:
1941
+ print("\n\u26a0\ufe0f WARNING: Corrupted characters detected in blueprint content!")
1942
+ print(" These will render as \ufffd (replacement character) in the PDF.")
1943
+ print(" Consider fixing the source text and re-running.\n")
1944
+
1945
+ # Also sanitize: replace U+FFFD with empty string to avoid visible corruption
1946
+ json_str_clean = json_str.replace('\ufffd', '')
1947
+ if json_str_clean != json_str:
1948
+ json_str = json_str_clean
1949
+ print(" \u2139\ufe0f Auto-removed U+FFFD characters from blueprint.")
1950
+
1951
+ # 2. Save clean JSON blueprint and temp HTML path
1952
+ blueprint_path = src.parent / f"{src.stem}_pure_blueprint.json"
1953
+ html_path = src.parent / f"{src.stem}_rendered.html"
1954
+ blueprint_path.write_text(json_str, encoding="utf-8")
1955
+
1956
+ # 3. Call design_engine.py to compile HTML
1957
+ engine_script = _SCRIPT_DIR / "design_engine.py"
1958
+ print("🎨 [1/2] Compiling JSON Blueprint to Art-Directed HTML...", flush=True)
1959
+ try:
1960
+ subprocess.run([
1961
+ sys.executable, str(engine_script), "compile",
1962
+ "--blueprint", str(blueprint_path),
1963
+ "--output", str(html_path)
1964
+ ], check=True)
1965
+ except subprocess.CalledProcessError:
1966
+ Output.error("CompileError", "design_engine.py failed to compile the blueprint.", code=4)
1967
+
1968
+ # 4. Call html2pdf.js to render PDF
1969
+ print("📄 [2/2] Rendering HTML to High-Res PDF via Playwright...", flush=True)
1970
+ js_script = _SCRIPT_DIR / "html2pdf.js"
1971
+ node_path = shutil.which("node")
1972
+ if not node_path:
1973
+ Output.error("DependencyMissing", "node not found in PATH")
1974
+
1975
+ try:
1976
+ subprocess.run([
1977
+ node_path, str(js_script), str(html_path), "-o", out_pdf,
1978
+ "--width", "720px", "--height", "960px"
1979
+ ], check=True)
1980
+ except subprocess.CalledProcessError:
1981
+ Output.error("RenderError", "html2pdf.js failed to render the PDF.", code=4)
1982
+
1983
+ print(f"\n🎉 Success! Masterpiece generated at: {out_pdf}")
1984
+ raise SystemExit(0)
1985
+
1986
+
1987
+ @cmd("convert.latex")
1988
+ def convert_latex(argv: list):
1989
+ """Compile LaTeX file via tectonic, filter logs, report PDF stats."""
1990
+ if not argv:
1991
+ Output.error("MissingArg", "tex file required")
1992
+ tex_file = argv.pop(0)
1993
+ runs_str = _pop_flag(argv, "-r", "--runs")
1994
+ runs = int(runs_str) if runs_str else 1
1995
+ keep_logs = _pop_flag(argv, "-k", "--keep-logs", needs_value=False)
1996
+
1997
+ tex = Path(tex_file)
1998
+ if not tex.exists():
1999
+ print(f"\u2717 Error: File not found {tex_file}")
2000
+ raise SystemExit(1)
2001
+
2002
+ print(f"Compiling {tex.name}...", flush=True)
2003
+ if runs > 1:
2004
+ print(f"Running {runs} passes (for cross-references)", flush=True)
2005
+
2006
+ tectonic = _find_tectonic()
2007
+ if tectonic is None:
2008
+ import platform
2009
+ print("\n\u2717 Error: tectonic command not found")
2010
+ print()
2011
+ print("The bundled scripts/tectonic binary is macOS arm64 only.")
2012
+ print("Install tectonic for your platform:\n")
2013
+ sys_name = platform.system()
2014
+ if sys_name == "Darwin":
2015
+ print(" macOS (Homebrew): brew install tectonic")
2016
+ print(" macOS (binary): curl -sSL https://drop-sh.fullyjustified.net | sh")
2017
+ print(" mv tectonic ~/tectonic && chmod +x ~/tectonic")
2018
+ elif sys_name == "Linux":
2019
+ print(" Debian/Ubuntu: apt install tectonic (if available)")
2020
+ print(" Arch Linux: pacman -S tectonic")
2021
+ print(" Conda: conda install -c conda-forge tectonic")
2022
+ print(" Binary download: curl -sSL https://drop-sh.fullyjustified.net | sh")
2023
+ print(" mv tectonic ~/tectonic && chmod +x ~/tectonic")
2024
+ elif sys_name == "Windows":
2025
+ print(" Windows (scoop): scoop install tectonic")
2026
+ print(" Windows (choco): choco install tectonic")
2027
+ print(" Conda: conda install -c conda-forge tectonic")
2028
+ else:
2029
+ print(" See: https://tectonic-typesetting.github.io/")
2030
+ print()
2031
+ print("After installing, verify with: tectonic --version")
2032
+ print()
2033
+ print("Quick check — is tectonic already installed somewhere?")
2034
+ print(f" which tectonic: {shutil.which('tectonic') or 'not found'}")
2035
+ print(f" ~/tectonic: {'exists' if (Path.home() / 'tectonic').exists() else 'not found'}")
2036
+ print(f" scripts/tectonic: {'exists' if (_SCRIPT_DIR / 'tectonic').exists() else 'not found'}")
2037
+ print(f" Platform: {sys_name} {platform.machine()}")
2038
+ raise SystemExit(1)
2039
+
2040
+ all_lines = []
2041
+ ok = False
2042
+ for _ in range(runs):
2043
+ try:
2044
+ proc = subprocess.run(
2045
+ [tectonic, "-X", "compile", str(tex)],
2046
+ capture_output=True, text=True, timeout=120,
2047
+ )
2048
+ all_lines.extend((proc.stdout + proc.stderr).splitlines())
2049
+ ok = proc.returncode == 0
2050
+ if not ok:
2051
+ break
2052
+ except subprocess.TimeoutExpired:
2053
+ print("\n\u2717 Error: Compilation timeout (>2 minutes)")
2054
+ raise SystemExit(1)
2055
+ except Exception as exc:
2056
+ print(f"\n\u2717 Error: {exc}")
2057
+ raise SystemExit(1)
2058
+
2059
+ if keep_logs:
2060
+ print("\n" + "=" * 50 + "\nFull logs:\n" + "=" * 50)
2061
+ for ln in all_lines:
2062
+ print(ln)
2063
+ print("=" * 50 + "\n")
2064
+
2065
+ errors, warnings, layout, pdf_note = _classify_lines(all_lines)
2066
+ noted_name, noted_size = _parse_writing_note(pdf_note)
2067
+ pdf_name = noted_name or (tex.stem + ".pdf")
2068
+ pdf_path = tex.parent / pdf_name
2069
+
2070
+ print()
2071
+ if ok:
2072
+ tag = "\u2713 Compilation successful" + (" (with warnings)" if warnings or layout else "")
2073
+ print(tag)
2074
+ else:
2075
+ print("\u2717 Compilation failed")
2076
+
2077
+ if ok and pdf_path.exists():
2078
+ print("\n========================\nPDF Information\n========================")
2079
+ print(f"File: {pdf_name}")
2080
+ print(f"Size: {noted_size or _human_size(pdf_path.stat().st_size)}")
2081
+ pages, words, images = _pdf_stats(pdf_path)
2082
+ if pages is not None:
2083
+ print(f"Pages: {pages}")
2084
+ if words is not None:
2085
+ print(f"Words: ~{words:,}")
2086
+ if images is not None:
2087
+ print(f"Images: {images}")
2088
+
2089
+ if layout:
2090
+ print(f"\n========================\nLayout Issues ({len(layout)})\n========================")
2091
+ for ln in layout:
2092
+ print(ln)
2093
+
2094
+ if warnings:
2095
+ print(f"\n========================\nWarnings ({len(warnings)})\n========================")
2096
+ for ln in warnings:
2097
+ print(ln.replace("warning: ", "", 1))
2098
+
2099
+ if errors:
2100
+ print("\n========================\nErrors\n========================")
2101
+ for ln in errors:
2102
+ print(ln.replace("error: ", "", 1))
2103
+
2104
+ if ok and (layout or warnings):
2105
+ print()
2106
+ print("<system-reminder>")
2107
+ print(f"Detected {len(layout)} layout issues and {len(warnings)} warnings.")
2108
+ print("These issues affect PDF typesetting quality and must be fixed.")
2109
+ print("Do not dismiss with 'warnings don't affect output'. Fix all issues.")
2110
+ print("</system-reminder>")
2111
+
2112
+ raise SystemExit(0 if ok else 1)
2113
+
2114
+
2115
+ # ═══════════════════════════════════════════════════════════════
2116
+ # Section 7b: Font Fallback Engine — automatic <font> wrapping
2117
+ # ═══════════════════════════════════════════════════════════════
2118
+
2119
+ # -- Data structures --------------------------------------------------------
2120
+
2121
+ FONT_FALLBACK_CHAIN: Dict[str, List[str]] = {
2122
+ "Times New Roman": ["SimHei"],
2123
+ "Calibri": ["SimHei"],
2124
+ "DejaVuSans": ["SimHei"],
2125
+ "SimHei": ["Times New Roman"],
2126
+ "Microsoft YaHei": ["Times New Roman"],
2127
+ }
2128
+
2129
+ FONT_PREFER_RANGES: Dict[str, List[Tuple[int, int, str]]] = {
2130
+ "SimHei": [
2131
+ (0x0400, 0x04FF, "Times New Roman"), # Cyrillic
2132
+ (0x0500, 0x052F, "Times New Roman"), # Cyrillic Supplement
2133
+ ],
2134
+ "Microsoft YaHei": [
2135
+ (0x0400, 0x04FF, "Times New Roman"),
2136
+ (0x0500, 0x052F, "Times New Roman"),
2137
+ ],
2138
+ }
2139
+
2140
+ # -- Content sanitization (runtime, pre-font-fallback) ----------------------
2141
+
2142
+ # Control: global switches
2143
+ CONTENT_SANITIZE_ENABLED = True # master switch
2144
+ CONTENT_SANITIZE_STRIP_ZW = True # zero-width chars (disable for Thai/Burmese/Hindi)
2145
+
2146
+ # Fullwidth ASCII → halfwidth (letters + digits only, NOT punctuation)
2147
+ _FULLWIDTH_OFFSET = 0xFEE0 # ord('A') - ord('A') == 0xFEE0
2148
+
2149
+ # Ligature decomposition (U+FB00–FB06)
2150
+ _LIGATURE_MAP: Dict[int, str] = {
2151
+ 0xFB00: "ff",
2152
+ 0xFB01: "fi",
2153
+ 0xFB02: "fl",
2154
+ 0xFB03: "ffi",
2155
+ 0xFB04: "ffl",
2156
+ 0xFB05: "st", # ſt long s t
2157
+ 0xFB06: "st", # st st
2158
+ }
2159
+
2160
+ # Zero-width / invisible characters to strip
2161
+ _ZERO_WIDTH_CHARS: Set[int] = {
2162
+ 0x200B, # ZERO WIDTH SPACE
2163
+ 0x200C, # ZERO WIDTH NON-JOINER
2164
+ 0x200D, # ZERO WIDTH JOINER
2165
+ 0x2060, # WORD JOINER
2166
+ 0xFEFF, # BOM (when not at file start)
2167
+ 0x034F, # COMBINING GRAPHEME JOINER
2168
+ }
2169
+
2170
+ # Bidirectional control characters to strip
2171
+ _BIDI_CONTROLS: Set[int] = {
2172
+ 0x200E, 0x200F, # LRM, RLM
2173
+ 0x202A, 0x202B, 0x202C, # LRE, RLE, PDF
2174
+ 0x202D, 0x202E, # LRO, RLO
2175
+ 0x2066, 0x2067, 0x2068, 0x2069, # LRI, RLI, FSI, PDI
2176
+ }
2177
+
2178
+ # Variation selectors to strip (emoji style modifiers)
2179
+ _VARIATION_SELECTORS: Set[int] = set(range(0xFE00, 0xFE10)) # U+FE00–FE0F
2180
+
2181
+ # Unicode noncharacters (should never appear in text interchange)
2182
+ _NONCHARACTERS: Set[int] = set(range(0xFDD0, 0xFDF0)) # U+FDD0–FDEF
2183
+ _NONCHARACTERS.add(0xFFFE)
2184
+ _NONCHARACTERS.add(0xFFFF)
2185
+
2186
+ _content_sanitize_warnings: List[str] = []
2187
+
2188
+
2189
+ def content_sanitize(text: str, dry_run: bool = False) -> str:
2190
+ """Sanitize Paragraph text content before font fallback.
2191
+
2192
+ Removes/replaces characters that should not appear in final PDF output.
2193
+ Designed for CJK + Latin content. Does NOT touch XML/HTML tags in the text.
2194
+
2195
+ Args:
2196
+ text: Raw Paragraph text (may contain ReportLab XML tags like <b>, <font>).
2197
+ dry_run: If True, collect warnings but still return cleaned text.
2198
+
2199
+ Returns:
2200
+ Cleaned text string.
2201
+ """
2202
+ if not CONTENT_SANITIZE_ENABLED or not text:
2203
+ return text
2204
+
2205
+ global _content_sanitize_warnings
2206
+ if dry_run:
2207
+ _content_sanitize_warnings = []
2208
+
2209
+ # Split text into tags and plain-text segments to avoid mangling XML tags
2210
+ # e.g. '<font name="SimHei">你好</font>' → ['', '<font name="SimHei">', '你好', '</font>', '']
2211
+ parts = _TAG_RE.split(text)
2212
+ tags = _TAG_RE.findall(text)
2213
+
2214
+ out_pieces: List[str] = []
2215
+
2216
+ for i, part in enumerate(parts):
2217
+ if part:
2218
+ # Process plain text segment character by character
2219
+ cleaned: List[str] = []
2220
+ for ch in part:
2221
+ code = ord(ch)
2222
+ replacement = _sanitize_one_char(ch, code, dry_run)
2223
+ if replacement is not None:
2224
+ cleaned.append(replacement)
2225
+ # else: character was deleted
2226
+ out_pieces.append("".join(cleaned))
2227
+
2228
+ # Append the tag that follows this part (if any)
2229
+ if i < len(tags):
2230
+ out_pieces.append(tags[i]) # tags pass through untouched
2231
+
2232
+ return "".join(out_pieces)
2233
+
2234
+
2235
+ def _sanitize_one_char(ch: str, code: int, dry_run: bool) -> Optional[str]:
2236
+ """Process a single character. Returns replacement string, or None to delete."""
2237
+
2238
+ # --- DELETE: ASCII control characters (except \t \n \r) ---
2239
+ if code <= 0x1F and ch not in '\t\n\r':
2240
+ if dry_run:
2241
+ _content_sanitize_warnings.append(
2242
+ f"DELETE control char U+{code:04X} ({repr(ch)})")
2243
+ return None
2244
+
2245
+ # --- DELETE: U+007F DEL ---
2246
+ if code == 0x7F:
2247
+ return None
2248
+
2249
+ # --- DELETE: zero-width characters ---
2250
+ if CONTENT_SANITIZE_STRIP_ZW and code in _ZERO_WIDTH_CHARS:
2251
+ if dry_run:
2252
+ _content_sanitize_warnings.append(
2253
+ f"DELETE zero-width U+{code:04X}")
2254
+ return None
2255
+
2256
+ # --- DELETE: bidirectional controls ---
2257
+ if code in _BIDI_CONTROLS:
2258
+ if dry_run:
2259
+ _content_sanitize_warnings.append(
2260
+ f"DELETE bidi control U+{code:04X}")
2261
+ return None
2262
+
2263
+ # --- DELETE: variation selectors ---
2264
+ if code in _VARIATION_SELECTORS:
2265
+ if dry_run:
2266
+ _content_sanitize_warnings.append(
2267
+ f"DELETE variation selector U+{code:04X}")
2268
+ return None
2269
+
2270
+ # --- DELETE: Unicode noncharacters ---
2271
+ if code in _NONCHARACTERS:
2272
+ return None
2273
+
2274
+ # --- REPLACE: U+FFFD replacement character → '?' ---
2275
+ if code == 0xFFFD:
2276
+ if dry_run:
2277
+ _content_sanitize_warnings.append(
2278
+ "REPLACE U+FFFD (upstream encoding error) → '?'")
2279
+ return '?'
2280
+
2281
+ # --- REPLACE: fullwidth ASCII letters/digits → halfwidth ---
2282
+ # U+FF21–FF3A = A–Z, U+FF41–FF5A = a–z, U+FF10–FF19 = 0–9
2283
+ if 0xFF21 <= code <= 0xFF3A or 0xFF41 <= code <= 0xFF5A or 0xFF10 <= code <= 0xFF19:
2284
+ halfwidth = chr(code - _FULLWIDTH_OFFSET)
2285
+ if dry_run:
2286
+ _content_sanitize_warnings.append(
2287
+ f"REPLACE fullwidth '{ch}' → halfwidth '{halfwidth}'")
2288
+ return halfwidth
2289
+
2290
+ # --- REPLACE: ligatures → decomposed ---
2291
+ if code in _LIGATURE_MAP:
2292
+ decomposed = _LIGATURE_MAP[code]
2293
+ if dry_run:
2294
+ _content_sanitize_warnings.append(
2295
+ f"REPLACE ligature '{ch}' (U+{code:04X}) → '{decomposed}'")
2296
+ return decomposed
2297
+
2298
+ # --- REPLACE: line/paragraph separators → newline ---
2299
+ if code == 0x2028 or code == 0x2029:
2300
+ if dry_run:
2301
+ _content_sanitize_warnings.append(
2302
+ f"REPLACE U+{code:04X} separator → '\\n'")
2303
+ return '\n'
2304
+
2305
+ # --- REPLACE: soft hyphen → empty (invisible, ReportLab ignores it) ---
2306
+ if code == 0x00AD:
2307
+ return None
2308
+
2309
+ # --- WARN (pass through): Private Use Area ---
2310
+ if 0xE000 <= code <= 0xF8FF:
2311
+ if dry_run:
2312
+ _content_sanitize_warnings.append(
2313
+ f"WARN Private Use Area U+{code:04X} (passed through)")
2314
+ return ch
2315
+
2316
+ # --- PASS: everything else ---
2317
+ return ch
2318
+
2319
+
2320
+ # -- Glyph detection -------------------------------------------------------
2321
+
2322
+ _glyph_cache: Dict[Tuple[str, int], bool] = {}
2323
+
2324
+
2325
+ def _has_glyph(font_name: str, code: int) -> bool:
2326
+ """Check whether *font_name* has a real glyph outline for *code*."""
2327
+ key = (font_name, code)
2328
+ if key in _glyph_cache:
2329
+ return _glyph_cache[key]
2330
+ try:
2331
+ from reportlab.pdfbase import pdfmetrics
2332
+ font = pdfmetrics.getFont(font_name)
2333
+ result = code in font.face.charToGlyph
2334
+ except Exception:
2335
+ result = False
2336
+ _glyph_cache[key] = result
2337
+ return result
2338
+
2339
+
2340
+ def _best_font_for_char(code: int, base_font: str) -> str:
2341
+ """Return the best registered font for a single character *code*."""
2342
+ # ASCII fast path
2343
+ if code < 128:
2344
+ return base_font
2345
+
2346
+ # Aesthetic preference ranges
2347
+ for rng_start, rng_end, preferred in FONT_PREFER_RANGES.get(base_font, []):
2348
+ if rng_start <= code <= rng_end:
2349
+ if _has_glyph(preferred, code):
2350
+ return preferred
2351
+
2352
+ # Base font has the glyph — use it
2353
+ if _has_glyph(base_font, code):
2354
+ return base_font
2355
+
2356
+ # Walk the fallback chain
2357
+ for fb in FONT_FALLBACK_CHAIN.get(base_font, []):
2358
+ if _has_glyph(fb, code):
2359
+ return fb
2360
+
2361
+ # Nothing found — return base_font (will render □ but won't crash)
2362
+ return base_font
2363
+
2364
+
2365
+ # -- Text-level automatic <font> wrapping ----------------------------------
2366
+
2367
+ _TAG_RE = re.compile(r"</?[a-zA-Z][^>]*>")
2368
+
2369
+
2370
+ def font_fallback(text: str, base_font: str) -> str:
2371
+ """Wrap characters that *base_font* cannot render with ``<font>`` tags.
2372
+
2373
+ Preserves existing XML tags (``<b>``, ``<font>``, ``<super>``, etc.).
2374
+ Returns the transformed markup string.
2375
+ """
2376
+ if not text:
2377
+ return text
2378
+
2379
+ # Split text into (plain, tag, plain, tag, ...) segments
2380
+ parts = _TAG_RE.split(text)
2381
+ tags = _TAG_RE.findall(text)
2382
+
2383
+ # Track <font> nesting so we don't re-wrap inside explicit <font>
2384
+ font_stack: List[str] = []
2385
+ out_pieces: List[str] = []
2386
+
2387
+ for i, part in enumerate(parts):
2388
+ # Determine effective font at this point
2389
+ effective = font_stack[-1] if font_stack else base_font
2390
+
2391
+ if part:
2392
+ # Process plain text character by character
2393
+ runs: List[Tuple[str, List[str]]] = []
2394
+ for ch in part:
2395
+ code = ord(ch)
2396
+ best = _best_font_for_char(code, effective)
2397
+ if runs and runs[-1][0] == best:
2398
+ runs[-1][1].append(ch)
2399
+ else:
2400
+ runs.append((best, [ch]))
2401
+
2402
+ for font_name, chars in runs:
2403
+ segment = "".join(chars)
2404
+ if font_name == effective:
2405
+ out_pieces.append(segment)
2406
+ else:
2407
+ out_pieces.append(f'<font name="{font_name}">{segment}</font>')
2408
+
2409
+ # Append the tag that follows this part (if any)
2410
+ if i < len(tags):
2411
+ tag = tags[i]
2412
+ out_pieces.append(tag)
2413
+ # Track font stack
2414
+ tag_lower = tag.lower()
2415
+ if tag_lower.startswith("<font "):
2416
+ # Extract font name from tag
2417
+ m = re.search(r'name\s*=\s*["\']([^"\']+)["\']', tag, re.IGNORECASE)
2418
+ if m:
2419
+ font_stack.append(m.group(1))
2420
+ else:
2421
+ font_stack.append(effective)
2422
+ elif tag_lower == "</font>" and font_stack:
2423
+ font_stack.pop()
2424
+
2425
+ return "".join(out_pieces)
2426
+
2427
+
2428
+ # -- Monkey-patch installer -------------------------------------------------
2429
+
2430
+ _fallback_installed = False
2431
+
2432
+
2433
+ def install_font_fallback():
2434
+ """Monkey-patch ``Paragraph.__init__`` so every Paragraph automatically
2435
+ runs ``font_fallback()`` on its text. Idempotent — safe to call
2436
+ multiple times.
2437
+ """
2438
+ global _fallback_installed
2439
+ if _fallback_installed:
2440
+ return
2441
+
2442
+ try:
2443
+ from reportlab.platypus import Paragraph as _Para
2444
+ except ImportError:
2445
+ return
2446
+
2447
+ _orig_init = _Para.__init__
2448
+
2449
+ def _patched_init(self, text, style=None, *args, **kwargs):
2450
+ if isinstance(text, str):
2451
+ text = content_sanitize(text) # Layer 1: clean dangerous chars
2452
+ if style is not None:
2453
+ base_font = getattr(style, "fontName", None)
2454
+ if base_font and base_font in FONT_FALLBACK_CHAIN:
2455
+ text = font_fallback(text, base_font) # Layer 2: font selection
2456
+ _orig_init(self, text, style, *args, **kwargs)
2457
+
2458
+ _Para.__init__ = _patched_init
2459
+ _fallback_installed = True
2460
+
2461
+
2462
+ # -- Post-generation glyph check -------------------------------------------
2463
+
2464
+ def check_missing_glyphs(pdf_path: str) -> List[Dict[str, Any]]:
2465
+ """Scan a PDF for .notdef glyphs, control chars, and other problematic characters.
2466
+
2467
+ Returns a list of dicts with keys: page, position, context, kind.
2468
+ Empty list means no issues found.
2469
+ """
2470
+ try:
2471
+ import fitz # PyMuPDF
2472
+ except ImportError:
2473
+ print("Warning: PyMuPDF (fitz) not installed — cannot check missing glyphs", file=sys.stderr)
2474
+ return []
2475
+
2476
+ issues: List[Dict[str, Any]] = []
2477
+ doc = fitz.open(pdf_path)
2478
+
2479
+ for page_num in range(len(doc)):
2480
+ page = doc[page_num]
2481
+ text = page.get_text()
2482
+ for i, ch in enumerate(text):
2483
+ code = ord(ch)
2484
+ kind: Optional[str] = None
2485
+
2486
+ if ch == "\x00":
2487
+ kind = "notdef"
2488
+ elif code <= 0x1F and ch not in '\t\n\r':
2489
+ kind = "control_char"
2490
+ elif code == 0x7F:
2491
+ kind = "control_char"
2492
+ elif code == 0xFFFD:
2493
+ kind = "replacement_char"
2494
+ elif code in _ZERO_WIDTH_CHARS:
2495
+ kind = "zero_width"
2496
+ elif code in _BIDI_CONTROLS:
2497
+ kind = "bidi_control"
2498
+ elif code in _VARIATION_SELECTORS:
2499
+ kind = "variation_selector"
2500
+ elif 0xE000 <= code <= 0xF8FF:
2501
+ kind = "private_use_area"
2502
+
2503
+ if kind:
2504
+ start = max(0, i - 10)
2505
+ end = min(len(text), i + 11)
2506
+ context = text[start:end].replace("\x00", "□")
2507
+ issues.append({
2508
+ "page": page_num + 1,
2509
+ "position": i,
2510
+ "char": f"U+{code:04X}",
2511
+ "kind": kind,
2512
+ "context": context,
2513
+ })
2514
+
2515
+ doc.close()
2516
+
2517
+ # Deduplicate by page + context + kind
2518
+ seen: Set[str] = set()
2519
+ unique: List[Dict[str, Any]] = []
2520
+ for item in issues:
2521
+ key = f"{item['page']}:{item['kind']}:{item['context']}"
2522
+ if key not in seen:
2523
+ seen.add(key)
2524
+ unique.append(item)
2525
+
2526
+ return unique
2527
+
2528
+
2529
+ @cmd("font.check")
2530
+ def font_check(argv: list):
2531
+ """Scan a PDF for missing glyphs (□ boxes) after generation."""
2532
+ if not argv:
2533
+ Output.error("MissingArg", "Usage: font.check <pdf_file>")
2534
+ pdf_path = argv[0]
2535
+ if not os.path.isfile(pdf_path):
2536
+ Output.error("FileNotFound", f"File not found: {pdf_path}")
2537
+
2538
+ issues = check_missing_glyphs(pdf_path)
2539
+
2540
+ # Group by kind for better reporting
2541
+ by_kind: Dict[str, List[Dict]] = {}
2542
+ for item in issues:
2543
+ k = item.get("kind", "unknown")
2544
+ by_kind.setdefault(k, []).append(item)
2545
+
2546
+ result: Dict[str, Any] = {
2547
+ "status": "issues_found" if issues else "ok",
2548
+ "total_issues": len(issues),
2549
+ "by_kind": {k: len(v) for k, v in by_kind.items()},
2550
+ }
2551
+ if issues:
2552
+ result["issues"] = issues[:30] # cap output
2553
+ result["hints"] = {
2554
+ "notdef": "Missing glyphs (□). Fix: call install_font_fallback() after font registration.",
2555
+ "control_char": "Control characters leaked into PDF. Fix: content_sanitize() should strip these.",
2556
+ "replacement_char": "U+FFFD found — upstream encoding error corrupted original character.",
2557
+ "zero_width": "Zero-width chars found. Usually harmless visually but affect text search/copy.",
2558
+ "bidi_control": "Bidirectional control chars found. May cause text direction issues.",
2559
+ "variation_selector": "Variation selectors found. No visual effect in ReportLab but shouldn't be here.",
2560
+ "private_use_area": "Private Use Area chars found. Will render as □ unless a custom font covers them.",
2561
+ }
2562
+
2563
+ print(json.dumps(result, ensure_ascii=False, indent=2))
2564
+ raise SystemExit(0 if not issues else 1)
2565
+
2566
+
2567
+ @cmd("toc.check")
2568
+ def toc_check(argv: list):
2569
+ """Validate TOC quality in a PDF file (page numbers, entries, links)."""
2570
+ if not argv:
2571
+ Output.error("MissingArg", "Usage: toc.check <pdf_file>")
2572
+ pdf_path = argv[0]
2573
+ if not os.path.isfile(pdf_path):
2574
+ Output.error("FileNotFound", f"File not found: {pdf_path}")
2575
+
2576
+ # Locate toc_validate.py relative to this script
2577
+ script_dir = Path(__file__).resolve().parent
2578
+ toc_script = script_dir / "toc_validate.py"
2579
+ if not toc_script.exists():
2580
+ # Try parent's scripts dir
2581
+ candidate = script_dir.parent / "scripts" / "toc_validate.py"
2582
+ if candidate.exists():
2583
+ toc_script = candidate
2584
+
2585
+ if toc_script.exists():
2586
+ # Import and call directly
2587
+ import importlib.util
2588
+ spec = importlib.util.spec_from_file_location("toc_validate", str(toc_script))
2589
+ toc_mod = importlib.util.module_from_spec(spec)
2590
+ spec.loader.exec_module(toc_mod)
2591
+ result = toc_mod.check_pdf(pdf_path)
2592
+ else:
2593
+ Output.error("ScriptNotFound",
2594
+ "toc_validate.py not found. Expected at: " + str(script_dir / "toc_validate.py"))
2595
+
2596
+ # Add remediation hints per error code
2597
+ remediation = {
2598
+ "TOC_ALL_SAME_PAGE": "ReportLab: use multiBuild() instead of build() with TocDocTemplate",
2599
+ "TOC_NO_ENTRIES": "Check afterFlowable() notifies TOC correctly; ensure headings have bookmark_name/bookmark_level attributes",
2600
+ "TOC_PAGES_INVALID": "TOC entry references a page beyond document total — regenerate TOC",
2601
+ "TOC_NOT_FOUND": "Document has many pages but no TOC detected in first 5 pages",
2602
+ "TOC_LINKS_MISSING": "TOC entries exist but no clickable links — check bookmark_key in afterFlowable()",
2603
+ "TOC_ON_FIRST_PAGE": "Add a cover page before the TOC, or insert PageBreak() between cover and TOC. Expected: Cover(p1) → TOC(p2) → Content(p3+)",
2604
+ }
2605
+ for err in result.get("errors", []):
2606
+ code = err.get("code", "")
2607
+ if code in remediation:
2608
+ err["fix"] = remediation[code]
2609
+ for warn in result.get("warnings", []):
2610
+ code = warn.get("code", "")
2611
+ if code in remediation:
2612
+ warn["fix"] = remediation[code]
2613
+
2614
+ print(json.dumps(result, ensure_ascii=False, indent=2))
2615
+ raise SystemExit(0 if result.get("pass", False) else 1)
2616
+
2617
+
2618
+ # ═══════════════════════════════════════════════════════════════
2619
+ # Section 7: code — sanitization pipeline for PDF generation code
2620
+ # ═══════════════════════════════════════════════════════════════
2621
+
2622
+ # --- Step 0: restore literal unicode escapes/entities to real chars ---
2623
+ _RE_UNICODE_ESC = re.compile(r"(\\u[0-9a-fA-F]{4})|(\\U[0-9a-fA-F]{8})|(\\x[0-9a-fA-F]{2})")
2624
+
2625
+
2626
+ def _restore_escapes(s: str) -> str:
2627
+ # HTML entities: &#179; &#x2264; &alpha; ...
2628
+ s = html.unescape(s)
2629
+
2630
+ # Literal backslash escapes: "\\u00B3" -> "³"
2631
+ def _dec(m: re.Match) -> str:
2632
+ esc = m.group(0)
2633
+ try:
2634
+ if esc.startswith("\\u") or esc.startswith("\\U"):
2635
+ return chr(int(esc[2:], 16))
2636
+ if esc.startswith("\\x"):
2637
+ return chr(int(esc[2:], 16))
2638
+ except Exception:
2639
+ return esc
2640
+ return esc
2641
+
2642
+ return _RE_UNICODE_ESC.sub(_dec, s)
2643
+
2644
+
2645
+ # --- Step 1: superscripts/subscripts -> <super>/<sub> ---
2646
+ _SUPERSCRIPT_MAP: Dict[str, str] = {
2647
+ "\u2070": "0", "\u00b9": "1", "\u00b2": "2", "\u00b3": "3", "\u2074": "4",
2648
+ "\u2075": "5", "\u2076": "6", "\u2077": "7", "\u2078": "8", "\u2079": "9",
2649
+ "\u207a": "+", "\u207b": "-", "\u207c": "=", "\u207d": "(", "\u207e": ")",
2650
+ "\u207f": "n", "\u1da6": "i",
2651
+ }
2652
+
2653
+ _SUBSCRIPT_MAP: Dict[str, str] = {
2654
+ "\u2080": "0", "\u2081": "1", "\u2082": "2", "\u2083": "3", "\u2084": "4",
2655
+ "\u2085": "5", "\u2086": "6", "\u2087": "7", "\u2088": "8", "\u2089": "9",
2656
+ "\u208a": "+", "\u208b": "-", "\u208c": "=", "\u208d": "(", "\u208e": ")",
2657
+ "\u2090": "a", "\u2091": "e", "\u2095": "h", "\u1d62": "i", "\u2c7c": "j",
2658
+ "\u2096": "k", "\u2097": "l", "\u2098": "m", "\u2099": "n", "\u2092": "o",
2659
+ "\u209a": "p", "\u1d63": "r", "\u209b": "s", "\u209c": "t", "\u1d64": "u",
2660
+ "\u1d65": "v", "\u2093": "x",
2661
+ }
2662
+
2663
+
2664
+ def _replace_super_sub(s: str) -> str:
2665
+ out = []
2666
+ for ch in s:
2667
+ if ch in _SUPERSCRIPT_MAP:
2668
+ out.append(f"<super>{_SUPERSCRIPT_MAP[ch]}</super>")
2669
+ elif ch in _SUBSCRIPT_MAP:
2670
+ out.append(f"<sub>{_SUBSCRIPT_MAP[ch]}</sub>")
2671
+ else:
2672
+ out.append(ch)
2673
+ return "".join(out)
2674
+
2675
+
2676
+ # --- Step 2: symbol fallback for SimHei (protect tags, then replace) ---
2677
+ _SYMBOL_FALLBACK: Dict[str, str] = {
2678
+ # Currently empty - enable entries as needed for fonts missing specific glyphs
2679
+ }
2680
+
2681
+
2682
+ def _fallback_symbols(s: str) -> str:
2683
+ # Protect <super>/<sub> tags from being modified
2684
+ placeholders: Dict[str, str] = {}
2685
+
2686
+ def _protect_tag(m: re.Match) -> str:
2687
+ key = f"@@TAG{len(placeholders)}@@"
2688
+ placeholders[key] = m.group(0)
2689
+ return key
2690
+
2691
+ protected = re.sub(r"</?super>|</?sub>", _protect_tag, s)
2692
+
2693
+ # Replace symbols
2694
+ protected = "".join(_SYMBOL_FALLBACK.get(ch, ch) for ch in protected)
2695
+
2696
+ # Restore tags
2697
+ for k, v in placeholders.items():
2698
+ protected = protected.replace(k, v)
2699
+
2700
+ return protected
2701
+
2702
+
2703
+ def sanitize_code(text: str) -> str:
2704
+ """
2705
+ Full sanitization pipeline for PDF generation code.
2706
+ - Restore unicode escapes/entities to real characters
2707
+ - Replace superscript/subscript unicode with <super>/<sub>
2708
+ - Replace other risky symbols with ASCII/text fallbacks
2709
+ """
2710
+ s = _restore_escapes(text)
2711
+ s = _replace_super_sub(s)
2712
+ s = _fallback_symbols(s)
2713
+ return s
2714
+
2715
+
2716
+ @cmd("palette.generate")
2717
+ def palette_generate(argv: list):
2718
+ """Generate a color palette via design_engine.py and output as Python-ready ReportLab code.
2719
+
2720
+ Usage:
2721
+ pdf.py palette.generate [--title "document title"] [--mode minimal|dark|pastel|jewel|light] [--harmony auto|complementary|...] [--format python|json|css]
2722
+
2723
+ If --title is provided, intent is auto-derived from the title.
2724
+ Output formats:
2725
+ python (default): Ready-to-paste ReportLab color variables
2726
+ json: Raw palette JSON
2727
+ css: CSS custom properties
2728
+ """
2729
+ title = _pop_flag(argv, "--title", "-t") or ""
2730
+ mode = _pop_flag(argv, "--mode", "-m") or "minimal"
2731
+ harmony = _pop_flag(argv, "--harmony", "--harmony") or "auto"
2732
+ fmt = _pop_flag(argv, "--format", "-f") or "python"
2733
+ seed_str = _pop_flag(argv, "--seed", "--seed")
2734
+ seed = int(seed_str) if seed_str else None
2735
+
2736
+ engine_script = _SCRIPT_DIR / "design_engine.py"
2737
+ if not engine_script.exists():
2738
+ Output.error("DependencyMissing", f"design_engine.py not found at {engine_script}")
2739
+
2740
+ # Import design_engine dynamically
2741
+ import importlib.util
2742
+ spec = importlib.util.spec_from_file_location("design_engine", str(engine_script))
2743
+ de = importlib.util.module_from_spec(spec)
2744
+ spec.loader.exec_module(de)
2745
+
2746
+ # Auto-derive intent from title
2747
+ if title:
2748
+ intent = de.derive_intent(title)
2749
+ else:
2750
+ intent = "neutral"
2751
+
2752
+ palette = de.generate_color_palette(intent, mode, harmony=harmony, seed=seed)
2753
+ violations = de.audit_palette(palette)
2754
+
2755
+ if fmt == "json":
2756
+ print(json.dumps(palette, indent=2, ensure_ascii=False))
2757
+ elif fmt == "css":
2758
+ print(de.palette_to_css(palette))
2759
+ else:
2760
+ # Python format: ready-to-paste ReportLab code
2761
+ print("# \u2501\u2501 Color Palette (auto-generated by pdf.py palette.generate) \u2501\u2501")
2762
+ print(f"# Intent: {intent} | Mode: {mode} | Harmony: {palette['meta']['harmony']}")
2763
+ print(f"# Contrast: text:bg={palette['meta']['contrast']['text_on_bg']} | accent:bg={palette['meta']['contrast']['accent_on_bg']}")
2764
+ print("from reportlab.lib import colors")
2765
+ print(f"ACCENT = colors.HexColor('{palette['accent']}')")
2766
+ print(f"TEXT_PRIMARY = colors.HexColor('{palette['text']}')")
2767
+ print(f"TEXT_MUTED = colors.HexColor('{palette['muted']}')")
2768
+ print(f"BG_SURFACE = colors.HexColor('{palette['mid']}')")
2769
+ print(f"BG_PAGE = colors.HexColor('{palette['bg']}')")
2770
+ print(f"SURFACE_RGBA = '{palette['surface']}' # For CSS/HTML elements")
2771
+ print("")
2772
+ print("# ReportLab table colors")
2773
+ print("TABLE_HEADER_COLOR = ACCENT")
2774
+ print("TABLE_HEADER_TEXT = colors.white")
2775
+ print("TABLE_ROW_EVEN = colors.white")
2776
+ print("TABLE_ROW_ODD = BG_SURFACE")
2777
+
2778
+ if violations:
2779
+ print(f"\n# \u26a0\ufe0f Palette audit warnings:", file=sys.stderr)
2780
+ for v in violations:
2781
+ print(f"# - {v}", file=sys.stderr)
2782
+
2783
+ raise SystemExit(0)
2784
+
2785
+
2786
+ @cmd("palette.cascade")
2787
+ def palette_cascade(argv: list):
2788
+ """Generate a role-based cascade palette (area ∝ 1/saturation).
2789
+
2790
+ Usage:
2791
+ pdf.py palette.cascade [--title "document title"] [--mode minimal|dark|pastel|jewel|light] [--harmony auto|...] [--format summary|json|css|reportlab]
2792
+
2793
+ The cascade palette produces 12 named roles + 4 semantic colors, each assigned
2794
+ to a size tier (XL/L/M/S/XS) with saturation caps enforced per tier.
2795
+ Cover, body, and chart colors all pull from the same palette — no color drift.
2796
+ """
2797
+ title = _pop_flag(argv, "--title", "-t") or ""
2798
+ mode = _pop_flag(argv, "--mode", "-m") or "minimal"
2799
+ harmony = _pop_flag(argv, "--harmony", "--harmony") or "auto"
2800
+ fmt = _pop_flag(argv, "--format", "-f") or "summary"
2801
+ seed_str = _pop_flag(argv, "--seed", "--seed")
2802
+ seed = int(seed_str) if seed_str else None
2803
+
2804
+ engine_script = _SCRIPT_DIR / "design_engine.py"
2805
+ if not engine_script.exists():
2806
+ Output.error("DependencyMissing", f"design_engine.py not found at {engine_script}")
2807
+
2808
+ import importlib.util
2809
+ spec = importlib.util.spec_from_file_location("design_engine", str(engine_script))
2810
+ de = importlib.util.module_from_spec(spec)
2811
+ spec.loader.exec_module(de)
2812
+
2813
+ if title:
2814
+ intent = de.derive_intent(title)
2815
+ else:
2816
+ intent = "neutral"
2817
+
2818
+ cascade = de.generate_cascade_palette(intent, mode, harmony=harmony, seed=seed)
2819
+
2820
+ if fmt == "json":
2821
+ print(json.dumps(cascade, indent=2, ensure_ascii=False, default=str))
2822
+ elif fmt == "css":
2823
+ print(cascade["css"])
2824
+ elif fmt == "reportlab":
2825
+ print(cascade["reportlab"])
2826
+ else:
2827
+ # Summary format
2828
+ meta = cascade["meta"]
2829
+ print(f"🎨 Cascade Palette | Intent: {meta['intent']} | Mode: {meta['mode']} | Harmony: {meta['harmony']}")
2830
+ print(f" Base hue: {meta['base_hue']}° | Accent hue: {meta['accent_hue']}° | Secondary: {meta['secondary_hue']}°")
2831
+ print(f" Contrast: text:bg={meta['contrast']['text_on_bg']} | accent:bg={meta['contrast']['accent_on_bg']}")
2832
+ print()
2833
+ print(" TIER | ROLE | HEX | S | USAGE")
2834
+ print(" ────── | ────────────────── | ─────── | ────── | ────────────")
2835
+ for name, info in cascade["roles"].items():
2836
+ tier = info['tier'].upper().ljust(6)
2837
+ nm = name.ljust(18)
2838
+ hx = info['hex'].ljust(7)
2839
+ s_val = f"{info['hsl'][1]:.3f}".ljust(6)
2840
+ print(f" {tier} | {nm} | {hx} | {s_val} | {info['usage']}")
2841
+ print()
2842
+ print(" Semantic:")
2843
+ for name, info in cascade["semantic"].items():
2844
+ print(f" {name}: {info['hex']} (S={info['hsl'][1]:.3f})")
2845
+ if meta["audit"]:
2846
+ print(f"\n ⚠️ Violations:")
2847
+ for v in meta["audit"]:
2848
+ print(f" - {v}")
2849
+ else:
2850
+ print(f"\n ✅ All tier constraints pass")
2851
+
2852
+ raise SystemExit(0)
2853
+
2854
+
2855
+ @cmd("code.sanitize")
2856
+ def code_sanitize(argv: list):
2857
+ """Sanitize Unicode in a Python script for PDF generation."""
2858
+ if not argv:
2859
+ Output.error("MissingArg", "Usage: code.sanitize <target_script.py>")
2860
+ target = argv[0]
2861
+ with open(target, "r", encoding="utf-8") as f:
2862
+ code = f.read()
2863
+ sanitized = sanitize_code(code)
2864
+ with open(target, "w", encoding="utf-8") as f:
2865
+ f.write(sanitized)
2866
+ print(f"Sanitized: {target}")
2867
+ raise SystemExit(0)
2868
+
2869
+
2870
+ @cmd("content.sanitize")
2871
+ def content_sanitize_cli(argv: list):
2872
+ """Sanitize content text for PDF rendering (dry-run report).
2873
+
2874
+ Reads a text file and reports what content_sanitize() would change.
2875
+ Useful for debugging before PDF generation.
2876
+
2877
+ Usage:
2878
+ pdf.py content.sanitize <text_file>
2879
+ pdf.py content.sanitize <text_file> --apply
2880
+ """
2881
+ if not argv:
2882
+ Output.error("MissingArg", "Usage: content.sanitize <text_file> [--apply]")
2883
+ target = argv[0]
2884
+ apply_flag = "--apply" in argv
2885
+
2886
+ with open(target, "r", encoding="utf-8") as f:
2887
+ raw = f.read()
2888
+
2889
+ global _content_sanitize_warnings
2890
+ _content_sanitize_warnings = []
2891
+ cleaned = content_sanitize(raw, dry_run=True)
2892
+
2893
+ changes = len(_content_sanitize_warnings)
2894
+ result: Dict[str, Any] = {
2895
+ "file": target,
2896
+ "original_chars": len(raw),
2897
+ "cleaned_chars": len(cleaned),
2898
+ "changes": changes,
2899
+ "details": _content_sanitize_warnings[:50],
2900
+ }
2901
+
2902
+ if apply_flag and changes > 0:
2903
+ with open(target, "w", encoding="utf-8") as f:
2904
+ f.write(cleaned)
2905
+ result["applied"] = True
2906
+ else:
2907
+ result["applied"] = False
2908
+ if changes > 0:
2909
+ result["hint"] = "Run with --apply to write cleaned text back to file."
2910
+
2911
+ print(json.dumps(result, ensure_ascii=False, indent=2))
2912
+ raise SystemExit(0)
2913
+
2914
+
2915
+ # ═══════════════════════════════════════════════════════════════
2916
+ # Section 8: CLI dispatcher
2917
+ # ═══════════════════════════════════════════════════════════════
2918
+
2919
+ def _usage():
2920
+ sys.stdout.write(__doc__.strip() + "\n")
2921
+ raise SystemExit(0)
2922
+
2923
+
2924
+ def main():
2925
+ tokens = sys.argv[1:]
2926
+ if not tokens or tokens[0] in ("-h", "--help"):
2927
+ _usage()
2928
+
2929
+ cmd_name = tokens.pop(0)
2930
+
2931
+ # Direct match
2932
+ handler = _COMMANDS.get(cmd_name)
2933
+ if handler is not None:
2934
+ handler(tokens)
2935
+ return
2936
+
2937
+ # Two-word match (e.g., "extract text" -> "extract.text")
2938
+ if tokens:
2939
+ compound = f"{cmd_name}.{tokens[0]}"
2940
+ handler = _COMMANDS.get(compound)
2941
+ if handler is not None:
2942
+ tokens.pop(0)
2943
+ handler(tokens)
2944
+ return
2945
+
2946
+ # List commands in group
2947
+ group_cmds = [k for k in _COMMANDS if k.startswith(cmd_name + ".")]
2948
+ if group_cmds:
2949
+ print(f"Available commands in '{cmd_name}':")
2950
+ for c in sorted(group_cmds):
2951
+ print(f" {c}")
2952
+ raise SystemExit(0)
2953
+
2954
+ print(f"Unknown command: {cmd_name}\n")
2955
+ _usage()
2956
+
2957
+
2958
+ if __name__ == "__main__":
2959
+ main()