backend improvements

- move_page
- merge_all_single_pages
- constructor with optional parameter
This commit is contained in:
Niklas Müller 2024-01-22 16:47:35 +01:00
parent f4b8029593
commit 6b74b89029
6 changed files with 300 additions and 38 deletions

View file

@ -1,15 +1,16 @@
from pypdf import PdfReader, PdfWriter
import uuid
import os
import shutil
import traceback
import glob
import datetime as dt
import logging
import sys
from pdf_util.pdf_util import pdf_util
base_path = "/app/projects/"
# Setup Logging
logging.basicConfig(
@ -23,26 +24,74 @@ logging.basicConfig(
]
)
base_path = "/app/projects/"
class pdf_project_manager:
def __init__(self):
self.uuid = str(uuid.uuid4())
self.pdf_init = False
self.project_name = ""
def __init__(self, uuid4=None):
if uuid4 is not None:
self.uuid = uuid4
else:
self.uuid = str(uuid.uuid4())
try:
self.pdf_init = os.path.isfile('/app/projects/' + self.uuid + '/complete.pdf')
except Exception as e:
logging.warning("Error looking up file: " + str(e))
logging.warning("Stacktrace: " + str(traceback.format_exc()))
self.pdf_init = False
os.makedirs(base_path + self.uuid, exist_ok=True)
self.pdf_handler = None
def merge_all_single_pages(self):
listing = glob.glob(base_path + self.uuid + '/splitted/*.pdf')
listing.sort()
shutil.copyfile(listing.pop(0), base_path + self.uuid + "/complete.pdf")
for pdf_file in listing:
print(pdf_file)
pdf_util(base_path + self.uuid + "/complete.pdf").merge_pdf_with_and_location(pdf_file, base_path + self.uuid + "/tmp_complete.pdf")
shutil.copyfile(base_path + self.uuid + "/tmp_complete.pdf", base_path + self.uuid + "/complete.pdf")
os.remove(base_path + self.uuid + "/tmp_complete.pdf")
"""
def add_pdf(self, pdf_path):
if self.pdf_init:
if not self.pdf_init:
shutil.copyfile(pdf_path, base_path + self.uuid + "/complete.pdf")
self.pdf_handler = pdf_util(base_path + self.uuid + "/complete.pdf")
self.pdf_init = True
else:
shutil.copyfile(pdf_path, base_path + self.uuid + "/tmp.pdf")
pdf_util(base_path + self.uuid + "/complete.pdf").merge_pdf_with_and_location(base_path + self.uuid + "/tmp.pdf", base_path + self.uuid + "/tmp_complete.pdf")
shutil.copyfile(base_path + self.uuid + "/tmp_complete.pdf", base_path + self.uuid + "/complete.pdf")
os.remove(base_path + self.uuid + "/tmp_complete.pdf")
os.remove(base_path + self.uuid + "/tmp.pdf")
self.pdf_handler = pdf_util(base_path + self.uuid + "/complete.pdf")
# Splitt files in all single Pages in Subdirectory...
"""
self.pdf_handler.split_pdf_with_location(base_path + self.uuid + '/splitted/', True, True)
def move_page(self, from_location, to_location):
try:
if from_location <= 0 or to_location <= 0:
raise ValueError("Pagenumber smaller/equal Zero")
if from_location < to_location:
shutil.move(base_path + self.uuid + '/splitted/' + str(from_location).zfill(4) + '.pdf', base_path + self.uuid + '/splitted/tmp.pdf')
for num in range(from_location, to_location):
print(num)
shutil.move(base_path + self.uuid + '/splitted/' + str(num + 1).zfill(4) + '.pdf', base_path + self.uuid + '/splitted/' + str(num).zfill(4) + '.pdf')
shutil.move(base_path + self.uuid + '/splitted/tmp.pdf', base_path + self.uuid + '/splitted/' + str(to_location).zfill(4) + '.pdf')
elif from_location > to_location:
shutil.move(base_path + self.uuid + '/splitted/' + str(from_location).zfill(4) + '.pdf', base_path + self.uuid + '/splitted/tmp.pdf')
for num in reversed(range(to_location, from_location)):
print(num)
print("move: " + str(num).zfill(4) + " | to: " + str(num + 1).zfill(4))
shutil.move(base_path + self.uuid + '/splitted/' + str(num).zfill(4) + '.pdf', base_path + self.uuid + '/splitted/' + str(num + 1).zfill(4) + '.pdf')
shutil.move(base_path + self.uuid + '/splitted/tmp.pdf', base_path + self.uuid + '/splitted/' + str(to_location).zfill(4) + '.pdf')
else:
raise ValueError("from_location and to_location are the same")
self.merge_all_single_pages()
except Exception as e:
logging.error("Error while moving page: " + str(e))
logging.error("Stacktrace: " + str(traceback.format_exc()))

View file

@ -24,22 +24,24 @@ class pdf_util:
self.file_name = os.path.basename(file_path)
self.file_name_wo_extension = os.path.splitext(os.path.basename(file_path))[0]
def split_pdf_with_location(self, output_filepath, no_names=False):
def split_pdf_with_location(self, output_filepath, no_names=False, int_padding=False):
out_filenames = []
os.makedirs(os.path.dirname(output_filepath), exist_ok=True)
with open(self.file_path, 'rb') as pdf_file:
pdf_reader = PdfReader(pdf_file)
num_pages = len(pdf_reader.pages)
for page_num in range(num_pages):
writer = PdfWriter()
writer.add_page(pdf_reader.pages[page_num])
str_page_num = str(page_num + 1)
if int_padding:
str_page_num = str_page_num.zfill(4)
if no_names:
out_filename = os.path.dirname(output_filepath) + '/' + str(page_num + 1) + '.pdf'
out_filename = os.path.dirname(output_filepath) + '/' + str_page_num + '.pdf'
else:
out_filename = os.path.dirname(output_filepath) + '/' + self.file_name_wo_extension + '_' + str(page_num + 1) + '.pdf'
out_filename = os.path.dirname(output_filepath) + '/' + self.file_name_wo_extension + '_' + str_page_num + '.pdf'
with open(out_filename, 'wb') as outfile:
writer.write(outfile)
@ -48,10 +50,9 @@ class pdf_util:
return out_filenames
# Deprecate when pdf_project_manager takes effect
def split_pdf(self):
def split_pdf(self, int_padding=False):
os.makedirs(os.path.dirname(self.file_path) + "/split_pdf", exist_ok=True)
return self.split_pdf_with_location(os.path.dirname(self.file_path) + "/split_pdf/", False)
return self.split_pdf_with_location(os.path.dirname(self.file_path) + "/split_pdf/", False, int_padding)
def merge_pdf_with_and_location(self, merge_file_path, output_filepath):
os.makedirs(os.path.dirname(output_filepath), exist_ok=True)
@ -74,5 +75,3 @@ class pdf_util:
def merge_pdf_with(self, merge_file_path, merged_name="merged"):
os.makedirs(os.path.dirname(self.file_path) + "/merge_pdf", exist_ok=True)
return self.merge_pdf_with_and_location(merge_file_path, os.path.dirname(self.file_path) + "/merge_pdf" + '/merger.pdf')