-
Notifications
You must be signed in to change notification settings - Fork 23
/
md2pdf.py
51 lines (39 loc) · 1.39 KB
/
md2pdf.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Convierte un archivo markdown en pdf según el estilo de la DNDIP"""
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import with_statement
import os
import sys
import markdown2
import pdfkit
import shutil
def main(input_paths_str, output_path):
# lee los htmls a convertir en PDF
input_paths = input_paths_str.split(",")
htmls = []
for input_path in input_paths:
with open(input_path) as input_file:
htmls.append(markdown2.markdown(
input_file.read(),
extras=["fenced_code", "codehilite", "admonition"]))
print("Hay {} documentos".format(len(htmls)))
# guarda html
with open(output_path.replace(".pdf", ".html"), "wb") as output_html:
# aplica el estilo al principio
html = "\n".join(htmls)
html_with_style = """
<link rel="stylesheet" href="pdf.css" type="text/css"/>
""" + html
# escribe el html
output_html.write(html_with_style.encode("utf-8"))
shutil.copyfile(
"docs/css/pdf.css",
os.path.join(os.path.dirname(output_path), "pdf.css")
)
# guarda pdf
pdfkit.from_string(html, output_path, options={"encoding": "utf8"},
css="docs/css/pdf.css")
if __name__ == '__main__':
main(sys.argv[1], sys.argv[2])