-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.py
More file actions
72 lines (60 loc) · 2.45 KB
/
Copy pathconvert.py
File metadata and controls
72 lines (60 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
"""Container entrypoint: render a PPTX to per-slide PNGs in /out.
Drives LibreOffice headless to produce a PDF, then pdftoppm to
rasterize each page.
"""
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
# soffice.bin directly: the soffice launcher is a shell script and the
# runtime image has no shell.
SOFFICE = "/usr/lib/libreoffice/program/soffice.bin"
def main() -> int:
if len(sys.argv) < 2:
print("usage: convert.py /in/deck.pptx [dpi]", file=sys.stderr)
return 1
src = Path(sys.argv[1])
dpi = sys.argv[2] if len(sys.argv) > 2 else "150"
with tempfile.TemporaryDirectory() as tmp:
# UserInstallation gives LibreOffice a writable profile dir so
# headless runs work regardless of the container user/HOME.
# Start from the pre-warmed template the build stage baked in,
# so soffice skips its first-run init (and its exit-81 relaunch).
profile = Path(tmp) / "profile"
template = Path("/opt/lo-profile")
if template.is_dir():
shutil.copytree(template, profile)
cmd = [
SOFFICE,
"--headless",
f"-env:UserInstallation=file://{profile}",
"--convert-to", "pdf",
"--outdir", tmp,
str(src),
]
result = subprocess.run(cmd, stdout=subprocess.DEVNULL)
if result.returncode == 81:
# 81 is EXITHELPER_NORMAL_RESTART, not an error: soffice.bin
# exits after first-run profile init and expects a relaunch
# (normally done by oosplash, which the shell-less image
# bypasses). Fresh profile per run means this happens every
# run; the relaunch does the actual conversion.
result = subprocess.run(cmd, stdout=subprocess.DEVNULL)
if result.returncode != 0:
print(f"error: soffice exited {result.returncode}", file=sys.stderr)
return 1
pdf = Path(tmp) / f"{src.stem}.pdf"
if not pdf.exists():
print(f"error: LibreOffice produced no PDF for {src}", file=sys.stderr)
return 1
subprocess.run(
["pdftoppm", "-png", "-r", dpi, str(pdf), f"/out/{src.stem}"],
check=True,
)
pages = sorted(Path("/out").glob(f"{src.stem}-*.png"))
print(f"wrote {len(pages)} slide image(s) to /out ({src.stem}-*.png)")
return 0
if __name__ == "__main__":
sys.exit(main())