Skip to content

Instantly share code, notes, and snippets.

@centum
Created October 3, 2022 12:54
Show Gist options
  • Save centum/8dd2df0896eed84830144769983bd51a to your computer and use it in GitHub Desktop.
Save centum/8dd2df0896eed84830144769983bd51a to your computer and use it in GitHub Desktop.
import os
from datetime import datetime
import fitz
IMAGES_DIR = "./"
file_list = [f.path for f in os.scandir(IMAGES_DIR) if f.is_file() and f.path.split(".")[-1] == "jpeg"]
doc = fitz.open()
for m in file_list:
with fitz.open(stream=open(m, "rb").read(), filetype="jpeg") as img:
rect = img[0].rect
pdfbytes = img.convert_to_pdf()
img_pdf = fitz.open("pdf", pdfbytes)
page = doc.new_page(width=rect.width, height=rect.height)
page.show_pdf_page(rect, img_pdf, 0)
metadata = doc.metadata
dt = datetime.utcnow().strftime("D:%Y%m%d%H%M%S")
metadata.update({
'title': f'My Title',
'author': 'user',
'subject': 'create pdf',
'keywords': 'example, create, pdf',
'creator': f'user',
'producer': 'fitz',
'creationDate': dt,
'modDate': dt,
})
doc.set_metadata(metadata)
pdf_bin = doc.write()
with open("compilation.pdf", "wb") as f:
f.write(pdf_bin)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment