create pdf
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
from weasyprint import HTML
|
||||
|
||||
def htmltopdf(html, file):
|
||||
HTML(string=html).write_pdf(file)
|
||||
+13
-1
@@ -1,6 +1,7 @@
|
||||
from pathlib import Path
|
||||
|
||||
from mdtohtml import mdtohtml
|
||||
from htmltopdf import htmltopdf
|
||||
|
||||
md = """
|
||||
#This is a header
|
||||
@@ -30,6 +31,7 @@ def main():
|
||||
if not srcdir.is_dir():
|
||||
return f'Error: "{srcdir}" is not a directory or does not exist'
|
||||
filelist = list(srcdir.glob('*.md'))
|
||||
htmlfiles = []
|
||||
|
||||
# mk destdir if not exist
|
||||
if not destdir.is_dir():
|
||||
@@ -39,13 +41,23 @@ def main():
|
||||
for infile in filelist:
|
||||
outfile = destdir / infile.name
|
||||
outfile = outfile.with_suffix(".html")
|
||||
print(f" processing '{infile}' to '{outfile}")
|
||||
htmlfiles.append(outfile)
|
||||
print(f" processing '{infile}' to '{outfile}'")
|
||||
with infile.open(mode='r', encoding='utf-8') as f:
|
||||
mdsrc = f.read()
|
||||
html = mdtohtml(mdsrc)
|
||||
with outfile.open(mode='w', encoding='utf-8') as f:
|
||||
f.write(html)
|
||||
|
||||
# process html to pdf
|
||||
for infile in htmlfiles:
|
||||
outfile = destdir / infile.name
|
||||
outfile = outfile.with_suffix(".pdf")
|
||||
print(f"processing '{infile}' to '{outfile}'")
|
||||
with infile.open(mode='r', encoding='utf-8') as f:
|
||||
htmlsrc = f.read()
|
||||
htmltopdf(htmlsrc, outfile)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user