Repository navigation
Expand file tree
/
Copy pathprint_html.py
More file actions
76 lines (68 loc) · 2.56 KB
/
Copy pathprint_html.py
File metadata and controls
76 lines (68 loc) · 2.56 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
73
74
75
76
#!/usr/bin/env python3
"""Print the rendered resume page (dev/_site/index.html) to PDF with headless Chrome/Chromium.
python dev/scripts/print_html.py # A5, zoom 30 % -> dev/build/resume-html-A5-30pct.pdf
python dev/scripts/print_html.py --paper A4 --zoom 100 -o out.pdf
Chrome has no command line switch for paper size or scale, so both are injected as CSS into a
copy of the page (dev/_site/_print.html): `@page { size: <paper> }` and `html { zoom: <zoom>% }`.
At 30 % the layout width is the paper width / 0.3, so one A5 page holds what a few A4 pages hold:
useful to judge the overall layout, page breaks and alignment (the PDF stays vector: zoom in).
Run `make preview` first (the Makefile target `htmlpdf` does).
"""
import argparse
import os
import re
import shutil
import subprocess
import sys
root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
site = os.path.join(root, "dev", "_site")
ap = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
ap.add_argument("--paper", default="A5", help="A3, A4, A5, letter, ... (CSS @page size)")
ap.add_argument("--zoom", type=float, default=30, help="percent, 30 = 0.3")
ap.add_argument("--margin", default="6mm")
ap.add_argument(
"-o", "--out", default=os.path.join(root, "dev", "build", "resume-html-A5-30pct.pdf")
)
args = ap.parse_args()
chrome = next(
(
c
for c in ("google-chrome", "google-chrome-stable", "chromium", "chromium-browser")
if shutil.which(c)
),
None,
)
if not chrome:
sys.exit("print_html.py: no Chrome/Chromium found")
src = os.path.join(site, "index.html")
if not os.path.exists(src):
sys.exit("print_html.py: dev/_site/index.html missing, run `make preview` first")
page = open(src, encoding="utf-8").read()
style = (
f'<style id="print-override">@page{{size:{args.paper};margin:{args.margin}}}'
f"html{{zoom:{args.zoom / 100:g}}}</style>"
)
page = re.sub(
r"</head>", style + "</head>", page, count=1
) # after resume.css: wins over its A4 @page
tmp = os.path.join(site, "_print.html")
open(tmp, "w", encoding="utf-8").write(page)
os.makedirs(os.path.dirname(args.out), exist_ok=True)
subprocess.run(
[
chrome,
"--headless=new",
"--no-sandbox",
"--disable-gpu",
"--no-pdf-header-footer",
f"--print-to-pdf={args.out}",
"file://" + tmp,
],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
os.remove(tmp)
print("wrote", args.out, f"({args.paper}, zoom {args.zoom:g}%)")