Skip to content

Instantly share code, notes, and snippets.

@vanatteveldt
Created December 20, 2023 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vanatteveldt/0258c29b6be9dc86ca2d33d202630a23 to your computer and use it in GitHub Desktop.
Save vanatteveldt/0258c29b6be9dc86ca2d33d202630a23 to your computer and use it in GitHub Desktop.
import subprocess
from pathlib import Path
from PyPDF2 import PdfReader, PdfWriter
outPdf=PdfWriter()
for inf in Path.cwd().glob("*.html"):
pdff = inf.with_suffix(".pdf")
if not pdff.exists():
print(f"*1* {inf} -> {pdff}")
subprocess.check_call(["wkhtmltopdf", str(inf), str(pdff)])
with open(pdff, 'rb') as input:
pdf=PdfReader(input)
outPdf.append(pdf)
if len(pdf.pages) % 2:
outPdf.add_blank_page()
outPdf.write("out.pdf")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment