upload
This commit is contained in:
Executable
+53
@@ -0,0 +1,53 @@
|
||||
#!/usr/bin/env python3
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "renderer"))
|
||||
|
||||
from app.renderer import render_documents
|
||||
from app.validation import validate_report
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(description="Render a report with a locally installed Chromium.")
|
||||
parser.add_argument("input", type=Path)
|
||||
parser.add_argument("output", type=Path)
|
||||
parser.add_argument("--chromium", default="chromium")
|
||||
args = parser.parse_args()
|
||||
|
||||
payload = json.loads(args.input.read_text(encoding="utf-8"))
|
||||
errors = validate_report(payload)
|
||||
if errors:
|
||||
print(json.dumps(errors, ensure_ascii=False, indent=2), file=sys.stderr)
|
||||
return 2
|
||||
|
||||
html, _ = render_documents(payload)
|
||||
args.output.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
html_path = Path(tmp) / "index.html"
|
||||
html_path.write_text(html, encoding="utf-8")
|
||||
subprocess.run([
|
||||
args.chromium,
|
||||
"--headless",
|
||||
"--no-sandbox",
|
||||
"--disable-gpu",
|
||||
"--disable-dev-shm-usage",
|
||||
f"--print-to-pdf={args.output.resolve()}",
|
||||
"--print-to-pdf-no-header",
|
||||
html_path.resolve().as_uri(),
|
||||
], check=True)
|
||||
|
||||
print(args.output)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user