-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_pdfs.py
89 lines (83 loc) · 2.78 KB
/
generate_pdfs.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import markdown
from pathlib import Path
from weasyprint import HTML, CSS
from weasyprint.fonts import FontConfiguration
TEMPLATE = """<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="referrer" content="no-referrer" />
<meta name="referrer" content="unsafe-url" />
<meta name="referrer" content="origin" />
<meta name="referrer" content="no-referrer-when-downgrade" />
<meta name="referrer" content="origin-when-cross-origin" />
<title>Page Title</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
font-family: Helvetica,Arial,sans-serif;
}
code, pre {
font-family: monospace;
}
table {
padding: 0;
}
table tr {
border-top: 1px solid #cccccc;
background-color: white;
margin: 0;
padding: 0;
}
table tr:nth-child(2n) {
background-color: #f8f8f8;
}
table tr th {
font-weight: bold;
border: 1px solid #cccccc;
text-align: left;
margin: 0;
padding: 6px 13px;
}
table tr td {
border: 1px solid #cccccc;
text-align: left;
margin: 0;
padding: 6px 13px;
}
table tr th :first-child, table tr td :first-child {
margin-top: 0;
}
table tr th :last-child, table tr td :last-child {
margin-bottom: 0;
}
</style>
</head>
<body>
<div class="container">
{{content}}
</div>
</body>
</html>
"""
if __name__ == "__main__":
paths = []
excluded = ['links.md', 'README.md']
for path in Path('.').rglob('*.md'):
if (path.name not in excluded):
paths.append(path)
print(path)
for path in paths:
with open(path, encoding='utf8') as file:
# Generate basic HTML from original markdown
extensions = ['extra', 'smarty', 'tables']
html = markdown.markdown(file.read(), extensions=extensions, output_format='html5')
doc = TEMPLATE.replace('{{content}}', html)
# Use weasyprint to generate quality html to use in PDF generator
font_config = FontConfiguration()
html = HTML(string=doc)
css = CSS(string='@page { size: A4; margin: 2cm }')
# Generated PDF files will be stored separatedly but with the same folder structure
filepath = str(path).replace('.md', '.pdf').replace('Markdown', 'PDF')
Path(filepath).parents[0].mkdir(parents=True, exist_ok=True)
html.write_pdf(filepath, stylesheets=[css], font_config=font_config,)