diff --git a/backend/app/tools/FusionPdf.py b/backend/app/tools/FusionPdf.py index 9c79a71..63bcd09 100644 --- a/backend/app/tools/FusionPdf.py +++ b/backend/app/tools/FusionPdf.py @@ -2,9 +2,24 @@ import os from PyPDF2 import PdfFileReader, PdfFileMerger -def fusion_fichiers(chemin_des_pdf, chemin_merge_pdf, nom_merge_pdf): - pdf_files = [f for f in os.listdir(chemin_des_pdf) if f.endswith("pdf")] +def fusion_fichiers(chemin_merge_pdf, nom_merge_pdf, liste_de_pdf): + """ + Permet de fusionner un ensemble de pdf + :param chemin_merge_pdf: chemin ou l'on souhaite fusioner l'ensemble des pdf + :param nom_merge_pdf: nom que l'on souhaite donner au fichier pdf final + :param liste_de_pdf: liste de pdf avec leur chemin inclu + :return: + """ merger = PdfFileMerger() - for filename in pdf_files: - merger.append(PdfFileReader(os.path.join(chemin_des_pdf, filename), "rb")) + for filename in liste_de_pdf: + merger.append(PdfFileReader(os.path.join(filename), "rb")) merger.write(os.path.join(chemin_merge_pdf, nom_merge_pdf)) + + +def get_pdf_from_directory(chemin_des_pdf): + """ + Permet de récuperer l'ensemble des pdf d'un chemin + :param chemin_des_pdf: + :return: + """ + return [f for f in os.listdir(chemin_des_pdf) if f.endswith("pdf")] diff --git a/backend/tests/tools/PyPDF2/TestFusionFichiers.py b/backend/tests/tools/PyPDF2/TestFusionFichiers.py index e69de29..0fb6737 100644 --- a/backend/tests/tools/PyPDF2/TestFusionFichiers.py +++ b/backend/tests/tools/PyPDF2/TestFusionFichiers.py @@ -0,0 +1,21 @@ +import os +import unittest + +from pathlib import Path + +from builtins import print + +from app.tools.FusionPdf import fusion_fichiers, get_pdf_from_directory + + +class TestFusionTestCase(unittest.TestCase): + def setUp(self): + self.datadir = os.path.join(os.path.dirname(__file__)) + self.list_pdf = [self.datadir + "/page1.pdf", self.datadir + "/page2.pdf"] + + def test_fusion(self): + fusion_fichiers(self.datadir, "testFusion.pdf", self.list_pdf) + print(self.datadir) + self.assertTrue(Path(self.datadir + "/testFusion.pdf").is_file(), "Pdf fusionne inexistant") + self.assertTrue(len(get_pdf_from_directory(self.datadir)) > 0, "pdf non trouve") + os.remove(self.datadir + "/testFusion.pdf") diff --git a/backend/tests/tools/PyPDF2/page1.pdf b/backend/tests/tools/PyPDF2/page1.pdf new file mode 100644 index 0000000..6b4e9cc Binary files /dev/null and b/backend/tests/tools/PyPDF2/page1.pdf differ diff --git a/backend/tests/tools/PyPDF2/page2.pdf b/backend/tests/tools/PyPDF2/page2.pdf new file mode 100644 index 0000000..e984709 Binary files /dev/null and b/backend/tests/tools/PyPDF2/page2.pdf differ diff --git a/backend/tests/tools/pdfjinja/TestInsertTemplate.py b/backend/tests/tools/pdfjinja/TestInsertTemplate.py index 36e68f6..68911c2 100644 --- a/backend/tests/tools/pdfjinja/TestInsertTemplate.py +++ b/backend/tests/tools/pdfjinja/TestInsertTemplate.py @@ -8,29 +8,28 @@ from pdfjinja import PdfJinja from app.tools.InsertTemplate import remplir_template -class insertTemplateTestCase(unittest.TestCase): - +class InsertTemplateTestCase(unittest.TestCase): datadir = os.path.join(os.path.dirname(__file__)) def setUp(self): pdffile = os.path.join(self.datadir, "sample.pdf") self.data = { - 'firstName': 'Renan', - 'lastName': 'Husson', - 'address': { - 'street': '24 rue de la pommes', - 'apt': 'C317', - 'city': 'TOULOUSE', - 'zipcode': 31000 - }, - 'universite':'Jean Jaures', - 'spirit': 'Panda', - 'evil': True, - 'language': { - 'french': True, - 'esperento': True - } -} + 'firstName': 'Renan', + 'lastName': 'Husson', + 'address': { + 'street': '24 rue de la pommes', + 'apt': 'C317', + 'city': 'TOULOUSE', + 'zipcode': 31000 + }, + 'universite': 'Jean Jaures', + 'spirit': 'Panda', + 'evil': True, + 'language': { + 'french': True, + 'esperento': True + } + } self.pdfjinja = PdfJinja(pdffile) def tearDown(self): @@ -44,9 +43,9 @@ class insertTemplateTestCase(unittest.TestCase): output.write(outfile) outfile.seek(0) self.assertTrue(len(outfile.read()) > 0, "Output PDF is not empty.") - self.assertTrue(Path(self.datadir+"/output.pdf").is_file(),"Pdf généré inexistant") - os.remove(self.datadir+"/output.pdf") + self.assertTrue(Path(self.datadir + "/output.pdf").is_file(), "Pdf généré inexistant") + os.remove(self.datadir + "/output.pdf") if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main()