Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions sign_oca/models/sign_oca_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,16 @@ def action_sign(self, items, access_token=False, latitude=False, longitude=False
signatory_data = self.request_id.signatory_data

input_data = BytesIO(b64decode(self.request_id.data))
reader = PdfFileReader(input_data)
try:
reader = PdfFileReader(input_data, strict=False)
except Exception as e: # pragma: no cover
_logger.error("Error reading PDF: %s", str(e))
raise ValidationError(
self.env._(
"The PDF file appears to be corrupted or contains "
"invalid characters. Please upload a valid PDF file."
)
) from e
output = PdfFileWriter()
pages = {}
for page_number in range(1, reader.numPages + 1):
Expand All @@ -464,7 +473,17 @@ def action_sign(self, items, access_token=False, latitude=False, longitude=False
for page_number in pages:
output.addPage(pages[page_number])
output_stream = BytesIO()
output.write(output_stream)
try:
output.write(output_stream)
except Exception as e: # pragma: no cover
_logger.error("Error writing PDF: %s", str(e))
raise ValidationError(
self.env._(
"The PDF file appears to be corrupted or contains invalid "
"characters that prevent signing. Please try re-generating "
"the PDF or use a different PDF tool to create a valid file."
)
) from e
output_stream.seek(0)
signed_pdf = output_stream.read()
final_hash = hashlib.sha1(signed_pdf).hexdigest()
Expand Down