-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild_pdf.py
75 lines (61 loc) · 1.96 KB
/
build_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
68
69
70
71
72
73
74
75
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import pdfkit
from glob import glob
from markdown import Markdown
""" Paths """
APP_PATH = os.path.dirname(os.path.realpath(__file__))
CHAPTERS_PATH = os.path.join(APP_PATH, 'markdown/*.md')
HTML_PATH = os.path.join(APP_PATH, 'html')
PDF_FILENAME = 'Kingpin.pdf'
if not os.path.isdir(HTML_PATH):
os.mkdir(HTML_PATH)
""" Markdown to HTML """
CHAPTERS = sorted([os.path.join(APP_PATH, f) for f in glob(CHAPTERS_PATH)])
TEMPLATE = """<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="utf-8">
<title></title>
<link href="../static/style.css" rel="stylesheet" />
</head>
<body>
%s
</body>
</html>
"""
extension_configs = {
'markdown.extensions.smarty': {
'substitutions': {
'left-single-quote': '‚', # sb is not a typo!
'right-single-quote': '‘',
'left-double-quote': '«',
'right-double-quote': '»'
}
}
}
md = Markdown(output_format="html5", extensions=['markdown.extensions.smarty'], extension_configs=extension_configs)
for chapter in CHAPTERS:
html = md.reset().convert(open(chapter, 'r').read())
outhtml = TEMPLATE % html
filename = os.path.splitext(os.path.basename(chapter))[0] + '.html'
filepath = os.path.join(HTML_PATH, filename)
with open(filepath, 'w') as f:
f.write(outhtml)
""" HTML to PDF """
htmlfiles = [os.path.join(HTML_PATH, f) for f in os.listdir(HTML_PATH) if f.endswith('.html')]
options = {
'enable-local-file-access': None,
'page-size': 'B5', # A5
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
'encoding': 'UTF-8',
'no-outline': None,
'no-background': None,
}
# https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf
pdfkit.from_file(htmlfiles, PDF_FILENAME, options=options, cover='static/cover.html', verbose=True)
print('PDF file {} was generated.'.format(PDF_FILENAME))