From 2240310818c5dfc88dfc839b4d863cf0552300c2 Mon Sep 17 00:00:00 2001 From: Eric Phillips Date: Sun, 25 Jan 2026 22:28:17 -0700 Subject: [PATCH] create pdf --- src/htmltopdf.py | 4 ++++ src/main.py | 14 +++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 src/htmltopdf.py diff --git a/src/htmltopdf.py b/src/htmltopdf.py new file mode 100644 index 0000000..f6dbf4d --- /dev/null +++ b/src/htmltopdf.py @@ -0,0 +1,4 @@ +from weasyprint import HTML + +def htmltopdf(html, file): + HTML(string=html).write_pdf(file) diff --git a/src/main.py b/src/main.py index 3ef1fa3..af9c89a 100644 --- a/src/main.py +++ b/src/main.py @@ -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()