render pdf without download link

This commit is contained in:
2026-02-03 12:52:09 -07:00
parent 906db94fdb
commit 902daebe1f
2 changed files with 24 additions and 21 deletions
+23 -16
View File
@@ -2,7 +2,9 @@ from pathlib import Path
from mdtohtml import mdtohtml from mdtohtml import mdtohtml
from htmltopdf import htmltopdf from htmltopdf import htmltopdf
from jinja2 import Environment, FileSystemLoader
j2_env = Environment(loader=FileSystemLoader('templates'))
srcdir = Path("markdown") srcdir = Path("markdown")
destdir = Path("publish") destdir = Path("publish")
@@ -17,33 +19,38 @@ 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():
destdir.mkdir() destdir.mkdir()
# process through mdtohtml # process filelist
for infile in filelist: for infile in filelist:
# create filenames
outfile = destdir / infile.name outfile = destdir / infile.name
outfile = outfile.with_suffix(".html") outfile = outfile.with_suffix(".html")
htmlfiles.append(outfile) outfilepdf = outfile.with_suffix(".pdf")
print(f"processing '{infile}' to '{outfile}'")
# md -> html
print(f"processing '{infile}' to html")
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) rawhtml = mdtohtml(mdsrc)
# html -> resume.template
print("processing rawhtml through resume.template")
template = j2_env.get_template('resume.template')
pdfhtml = template.render(content=rawhtml, download="")
print(f"processing html to '{outfilepdf}'")
htmltopdf(pdfhtml, outfilepdf)
print(f"processing pdfhtml to '{outfile}'")
download_template = j2_env.get_template('download.template')
download_bar = download_template.render(pdffile=outfilepdf.name)
pubhtml = template.render(content=rawhtml, download=download_bar)
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(pubhtml)
# 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()
+1 -5
View File
@@ -1,11 +1,7 @@
from markdown_it import MarkdownIt from markdown_it import MarkdownIt
from jinja2 import Environment, FileSystemLoader
md = MarkdownIt("commonmark").enable('table') md = MarkdownIt("commonmark").enable('table')
j2_env = Environment(loader=FileSystemLoader('templates'))
template = j2_env.get_template('resume.template')
def mdtohtml(mdsrc): def mdtohtml(mdsrc):
rawhtml = md.render(mdsrc) rawhtml = md.render(mdsrc)
return template.render(content=rawhtml) return rawhtml