create pdf

This commit is contained in:
2026-01-25 22:28:17 -07:00
parent cca6e351ca
commit 2240310818
2 changed files with 17 additions and 1 deletions
+4
View File
@@ -0,0 +1,4 @@
from weasyprint import HTML
def htmltopdf(html, file):
HTML(string=html).write_pdf(file)
+13 -1
View File
@@ -1,6 +1,7 @@
from pathlib import Path from pathlib import Path
from mdtohtml import mdtohtml from mdtohtml import mdtohtml
from htmltopdf import htmltopdf
md = """ md = """
#This is a header #This is a header
@@ -30,6 +31,7 @@ def main():
if not srcdir.is_dir(): if not srcdir.is_dir():
return f'Error: "{srcdir}" is not a directory or does not exist' return f'Error: "{srcdir}" is not a directory or does not exist'
filelist = list(srcdir.glob('*.md')) filelist = list(srcdir.glob('*.md'))
htmlfiles = []
# mk destdir if not exist # mk destdir if not exist
if not destdir.is_dir(): if not destdir.is_dir():
@@ -39,13 +41,23 @@ def main():
for infile in filelist: for infile in filelist:
outfile = destdir / infile.name outfile = destdir / infile.name
outfile = outfile.with_suffix(".html") 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: with infile.open(mode='r', encoding='utf-8') as f:
mdsrc = f.read() mdsrc = f.read()
html = mdtohtml(mdsrc) html = mdtohtml(mdsrc)
with outfile.open(mode='w', encoding='utf-8') as f: with outfile.open(mode='w', encoding='utf-8') as f:
f.write(html) 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__": if __name__ == "__main__":
main() main()