Skip to content

Commit

Permalink
✅ Add merge_jpg_to_pdf method to CreatePDF class
Browse files Browse the repository at this point in the history
  • Loading branch information
NTGNguyen committed Feb 19, 2024
1 parent d44df6e commit 7d6dc4e
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/modules/create_pdf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Merge the images of books into PDF files"""


import os
import img2pdf
from .link_parse import LinkFile, Link


Expand All @@ -13,3 +14,25 @@ class CreatePDF:

def __init__(self, links: list[Link]) -> None:
self.links = links

@staticmethod
def merge_jpg_to_pdf(directory, book_name) -> None:
"""Merge all JPG images in a directory into a single PDF.
Args:
directory (str): The directory containing the JPG images.
output_filename (str): The filename of the output PDF.
Returns:
None
"""
jpg_files = [f for f in os.listdir(directory) if f.endswith('.jpg')]
jpg_files.sort()
jpg_files = [os.path.join(directory, f) for f in jpg_files]
if jpg_files:
pdf_bytes = img2pdf.convert(jpg_files)
if pdf_bytes is not None:
with open(book_name, 'wb') as f:
f.write(pdf_bytes)
else:
print("No JPG images found in the directory.")

0 comments on commit 7d6dc4e

Please sign in to comment.