-
Notifications
You must be signed in to change notification settings - Fork 650
/
pdf.py
67 lines (57 loc) · 2.02 KB
/
pdf.py
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
"""Commands to facilitate conversion to PDF."""
from pathlib import Path
import asyncio
from .utils import _error
def html_to_pdf(html_file, pdf_file):
"""
Convert arbitrary HTML file to PDF using pyppeteer.
Parameters
----------
html_file : str
A path to an HTML file to convert to PDF
pdf_file : str
A path to an output PDF file that will be created
"""
asyncio.get_event_loop().run_until_complete(_html_to_pdf(html_file, pdf_file))
async def _html_to_pdf(html_file, pdf_file):
try:
from pyppeteer import launch
except ImportError:
_error(
"Generating PDF from book HTML requires the pyppeteer package. "
"Install it first.",
ImportError,
)
browser = await launch(args=["--no-sandbox"])
page = await browser.newPage()
# Absolute path is needed
html_file = Path(html_file).resolve()
# Waiting for networkidle0 seems to let mathjax render
await page.goto(f"file:///{html_file}", {"waitUntil": ["networkidle0"]})
# Give it *some* margins to make it look a little prettier
# I just made these up
page_margins = {"left": "0in", "right": "0in", "top": ".5in", "bottom": ".5in"}
await page.pdf({"path": pdf_file, "margin": page_margins})
await browser.close()
def update_latex_documents(latex_documents, latexoverrides):
"""
Apply latexoverrides from _config.yml to latex_documents tuple
"""
latexdocs_tuple = (
"startdocname",
"targetname",
"title",
"author",
"theme",
"toctree_only",
)
updated_latexdocs = []
for loc, item in enumerate(latexdocs_tuple):
# the last element toctree_only seems optionally included
if loc >= len(latex_documents):
break
if item in latexoverrides["latex_documents"].keys():
updated_latexdocs.append(latexoverrides["latex_documents"][item])
else:
updated_latexdocs.append(latex_documents[loc])
return tuple(updated_latexdocs)