Skip to content

Commit

Permalink
Create PDF files.
Browse files Browse the repository at this point in the history
In addition to image files, PDF files will also be created.
  • Loading branch information
LukeShortCloud committed Apr 27, 2020
1 parent 12bc0cf commit 32e835e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[DESIGN]
# Maximum object variables.
max-attributes=10
max-attributes=15
# Maximum local variables.
# Default: 15
max-locals=16
26 changes: 25 additions & 1 deletion cgc/cgc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from os.path import basename, exists, isdir, join
from math import ceil
from hashlib import sha512
import img2pdf
# Image processing library.
from PIL import Image
import pkg_resources
Expand Down Expand Up @@ -38,8 +39,10 @@ def __init__(self, tmp_dest_dir=join(tempfile.gettempdir(), "cgc"),
self.tmp_dir_individual = join(self.tmp_dest_dir, "individual")
self.tmp_dir_horizontal = join(self.tmp_dest_dir, "horizontal")
self.tmp_dir_vertical = join(self.tmp_dest_dir, "vertical")
self.tmp_dir_pdfs = join(self.tmp_dest_dir, "pdfs")
self.cgc_managed_dirs = [self.tmp_dest_dir, self.tmp_dir_individual,
self.tmp_dir_horizontal, self.tmp_dir_vertical]
self.tmp_dir_horizontal, self.tmp_dir_vertical,
self.tmp_dir_pdfs]
self.queue = Queue()

if not exists(self.tmp_dest_dir):
Expand Down Expand Up @@ -456,6 +459,24 @@ def convert_batch_append(self, append_method):

return True

def convert_to_pdf(self):
"""Convert all images from the horizontal directory into PDFs.
Args:
None
"""
images = listdir(self.tmp_dir_horizontal)
count = 0

for image_name in images:
pdf_data = img2pdf.convert(join(self.tmp_dir_horizontal, image_name))
file = open(join(self.tmp_dir_pdfs, str(count) + ".pdf"), "wb")
file.write(pdf_data)
file.close()
count += 1

return True

def convert_batch_append_all(self):
"""Merge all individual cards into a printable set. The cards are
assumed to have already had their density changed and have been
Expand All @@ -478,4 +499,7 @@ def convert_batch_append_all(self):
if not self.convert_batch_append(append_method="horizontal"):
return False

if not self.convert_to_pdf():
return False

return True
4 changes: 4 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def test_convert_batch_append_all(self):
self.cgc.convert_batch_append_all()
listdir_vertical = listdir(self.cgc.tmp_dir_vertical)
listdir_horizontal = listdir(self.cgc.tmp_dir_horizontal)
listdir_pdfs = listdir(self.cgc.tmp_dir_pdfs)

# There should be 3 vertical images (each with 1, 4, and 4 images).
if len(listdir_vertical) != 3:
Expand All @@ -161,6 +162,9 @@ def test_convert_batch_append_all(self):
elif return_status == False:
self.assertTrue(False)

if len(listdir_pdfs) != 2:
self.assertTrue(False)

def tearDown(self):
rmtree(self.cards_source_dir)
rmtree(self.cgc.tmp_dest_dir)
Expand Down

0 comments on commit 32e835e

Please sign in to comment.