From b2da426cc19b54298635597ba5145697a501eea0 Mon Sep 17 00:00:00 2001 From: Ludy Date: Sat, 11 Jan 2025 22:06:46 +0100 Subject: [PATCH] change from pypdf2 to pypdf (#2666) # Description addition to #2636 ## Checklist - [x] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [x] I have performed a self-review of my own code - [ ] I have attached images of the change if it is UI based - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] If my code has heavily changed functionality I have updated relevant docs on [Stirling-PDFs doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) - [x] My changes generate no new warnings - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) --- cucumber/exampleFiles/example.html | 1 - cucumber/features/environment.py | 2 +- cucumber/features/steps/step_definitions.py | 25 +++++++++++---------- cucumber/requirements.in | 2 +- cucumber/requirements.txt | 10 ++++++--- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/cucumber/exampleFiles/example.html b/cucumber/exampleFiles/example.html index 82e96100..c87692f8 100644 --- a/cucumber/exampleFiles/example.html +++ b/cucumber/exampleFiles/example.html @@ -8,4 +8,3 @@ - diff --git a/cucumber/features/environment.py b/cucumber/features/environment.py index ca56b21f..c85eb001 100644 --- a/cucumber/features/environment.py +++ b/cucumber/features/environment.py @@ -18,4 +18,4 @@ def after_scenario(context, scenario): # Remove any temporary files for temp_file in os.listdir('.'): if temp_file.startswith('genericNonCustomisableName') or temp_file.startswith('temp_image_'): - os.remove(temp_file) \ No newline at end of file + os.remove(temp_file) diff --git a/cucumber/features/steps/step_definitions.py b/cucumber/features/steps/step_definitions.py index ae8acd2a..5f2a92ec 100644 --- a/cucumber/features/steps/step_definitions.py +++ b/cucumber/features/steps/step_definitions.py @@ -1,7 +1,8 @@ import os import requests from behave import given, when, then -from PyPDF2 import PdfWriter, PdfReader +from pypdf import PdfWriter, PdfReader +from pypdf.errors import PdfReadError import io import random import string @@ -42,7 +43,7 @@ def step_use_example_file(context, filePath, fileInput): context.file_name = filePath.split('/')[-1] if not hasattr(context, 'files'): context.files = {} - + # Ensure the file exists before opening try: example_file = open(filePath, 'rb') @@ -165,17 +166,17 @@ def step_pdf_contains_pages_with_random_text(context, page_count): buffer = io.BytesIO() c = canvas.Canvas(buffer, pagesize=letter) width, height = letter - + for _ in range(page_count): text = ''.join(random.choices(string.ascii_letters + string.digits, k=100)) c.drawString(100, height - 100, text) c.showPage() - + c.save() - + with open(context.file_name, 'wb') as f: f.write(buffer.getvalue()) - + context.files[context.param_name].close() context.files[context.param_name] = open(context.file_name, 'rb') @@ -184,16 +185,16 @@ def step_pdf_pages_contain_text(context, text): buffer = io.BytesIO() c = canvas.Canvas(buffer, pagesize=letter) width, height = letter - + for _ in range(len(PdfReader(context.file_name).pages)): c.drawString(100, height - 100, text) c.showPage() - + c.save() - + with open(context.file_name, 'wb') as f: f.write(buffer.getvalue()) - + context.files[context.param_name].close() context.files[context.param_name] = open(context.file_name, 'rb') @@ -345,7 +346,7 @@ def step_check_response_pdf_page_count(context, page_count): def step_check_response_zip_file_count(context, file_count): response_file = io.BytesIO(context.response.content) with zipfile.ZipFile(io.BytesIO(response_file.getvalue())) as zip_file: - actual_file_count = len(zip_file.namelist()) + actual_file_count = len(zip_file.namelist()) assert actual_file_count == file_count, f"Expected {file_count} files but got {actual_file_count} files" @then('the response ZIP file should contain {doc_count:d} documents each having {pages_per_doc:d} pages') @@ -354,7 +355,7 @@ def step_check_response_zip_doc_page_count(context, doc_count, pages_per_doc): with zipfile.ZipFile(io.BytesIO(response_file.getvalue())) as zip_file: actual_doc_count = len(zip_file.namelist()) assert actual_doc_count == doc_count, f"Expected {doc_count} documents but got {actual_doc_count} documents" - + for file_name in zip_file.namelist(): with zip_file.open(file_name) as pdf_file: reader = PdfReader(pdf_file) diff --git a/cucumber/requirements.in b/cucumber/requirements.in index e5da3c1b..add7c499 100644 --- a/cucumber/requirements.in +++ b/cucumber/requirements.in @@ -1,5 +1,5 @@ behave requests -PyPDF2 +pypdf reportlab PyCryptodome diff --git a/cucumber/requirements.txt b/cucumber/requirements.txt index 9372140b..87546aea 100644 --- a/cucumber/requirements.txt +++ b/cucumber/requirements.txt @@ -231,9 +231,9 @@ pycryptodome==3.21.0 \ --hash=sha256:f7787e0d469bdae763b876174cf2e6c0f7be79808af26b1da96f1a64bcf47297 \ --hash=sha256:ff99f952db3db2fbe98a0b355175f93ec334ba3d01bbde25ad3a5a33abc02b58 # via -r cucumber\requirements.in -pypdf2==3.0.1 \ - --hash=sha256:a74408f69ba6271f71b9352ef4ed03dc53a31aa404d29b5d31f53bfecfee1440 \ - --hash=sha256:d16e4205cfee272fbdc0568b68d82be796540b1537508cef59388f839c191928 +pypdf==5.1.0 \ + --hash=sha256:3bd4f503f4ebc58bae40d81e81a9176c400cbbac2ba2d877367595fb524dfdfc \ + --hash=sha256:425a129abb1614183fd1aca6982f650b47f8026867c0ce7c4b9f281c443d2740 # via -r cucumber\requirements.in reportlab==4.2.5 \ --hash=sha256:5cf35b8fd609b68080ac7bbb0ae1e376104f7d5f7b2d3914c7adc63f2593941f \ @@ -249,6 +249,10 @@ six==1.17.0 \ # via # behave # parse-type +typing-extensions==4.12.2 \ + --hash=sha256:04e5ca0351e0f3f85c6853954072df659d0d13fac324d0072316b67d7794700d \ + --hash=sha256:1a7ead55c7e559dd4dee8856e3a88b41225abfe1ce8df57b7c13915fe121ffb8 + # via pypdf urllib3==2.3.0 \ --hash=sha256:1cee9ad369867bfdbbb48b7dd50374c0967a0bb7710050facf0dd6911440e3df \ --hash=sha256:f8c5449b3cf0861679ce7e0503c7b44b5ec981bec0d1d3795a07f1ba96f0204d