caerp-sign-pdf 2024.1.2__py3-none-any.whl → 2024.1.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- caerp_sign_pdf/public.py +36 -21
- {caerp_sign_pdf-2024.1.2.dist-info → caerp_sign_pdf-2024.1.4.dist-info}/METADATA +3 -3
- caerp_sign_pdf-2024.1.4.dist-info/RECORD +8 -0
- {caerp_sign_pdf-2024.1.2.dist-info → caerp_sign_pdf-2024.1.4.dist-info}/WHEEL +1 -1
- caerp_sign_pdf-2024.1.2.dist-info/RECORD +0 -8
- {caerp_sign_pdf-2024.1.2.dist-info → caerp_sign_pdf-2024.1.4.dist-info}/LICENSE.txt +0 -0
- {caerp_sign_pdf-2024.1.2.dist-info → caerp_sign_pdf-2024.1.4.dist-info}/top_level.txt +0 -0
caerp_sign_pdf/public.py
CHANGED
|
@@ -4,11 +4,12 @@ import hashlib
|
|
|
4
4
|
|
|
5
5
|
from io import BytesIO
|
|
6
6
|
from pyhanko.pdf_utils.incremental_writer import IncrementalPdfFileWriter
|
|
7
|
+
from pyhanko.pdf_utils.xref import ObjectHeaderReadError
|
|
7
8
|
from pyhanko.sign import fields, signers
|
|
8
9
|
from pyhanko.sign.general import SigningError
|
|
9
10
|
from pyhanko.stamp import TextStampStyle
|
|
10
|
-
from PyPDF4.pdf import PdfFileReader, PdfFileWriter
|
|
11
|
-
from pyramid.httpexceptions import
|
|
11
|
+
from PyPDF4.pdf import PdfFileReader, PdfFileWriter
|
|
12
|
+
from pyramid.httpexceptions import HTTPUnsupportedMediaType
|
|
12
13
|
|
|
13
14
|
from caerp.views.files.controller import FileData
|
|
14
15
|
|
|
@@ -18,6 +19,14 @@ from .models import PDFSignatureHistory
|
|
|
18
19
|
logger = logging.getLogger(f"caerp.{__name__}")
|
|
19
20
|
|
|
20
21
|
|
|
22
|
+
ERROR_LOAD_CERTIFICATE_MSG = "Impossible de charger le certificat à utiliser \
|
|
23
|
+
pour signer numériquement le fichier. Veuillez contacter le support."
|
|
24
|
+
|
|
25
|
+
ERROR_PDF_FORMAT_MSG = "Le fichier PDF ne peut pas être signé numériquement \
|
|
26
|
+
car son format n'est pas correct. Vous pouvez essayer de l'imprimer dans un \
|
|
27
|
+
nouveau fichier PDF avant de le recharger."
|
|
28
|
+
|
|
29
|
+
|
|
21
30
|
class SignPDFService(object):
|
|
22
31
|
def __init__(self, context, request):
|
|
23
32
|
self.context = context
|
|
@@ -89,11 +98,11 @@ class SignPDFService(object):
|
|
|
89
98
|
passphrase=self._get_certificate_passphrase().encode(),
|
|
90
99
|
)
|
|
91
100
|
if not signer:
|
|
92
|
-
|
|
101
|
+
logger.error(
|
|
93
102
|
"Unable to load certificate for signing PDF files (check \
|
|
94
|
-
'caerp.sign_certificate_path' and 'caerp.sign_certificate_passphrase'
|
|
95
|
-
in config)"
|
|
103
|
+
'caerp.sign_certificate_path' and 'caerp.sign_certificate_passphrase' in config)"
|
|
96
104
|
)
|
|
105
|
+
raise Exception(ERROR_LOAD_CERTIFICATE_MSG)
|
|
97
106
|
|
|
98
107
|
# Prepare stamp (if needed)
|
|
99
108
|
stamp = None
|
|
@@ -109,21 +118,18 @@ in config)"
|
|
|
109
118
|
border_width=0,
|
|
110
119
|
)
|
|
111
120
|
|
|
112
|
-
#
|
|
121
|
+
# Open original PDF
|
|
113
122
|
try:
|
|
114
123
|
pdf_data.data = self._get_safe_pdf(pdf_data.data)
|
|
124
|
+
pdf_writer = IncrementalPdfFileWriter(pdf_data.data, strict=False)
|
|
115
125
|
except Exception as error:
|
|
116
|
-
logger.error(f"
|
|
117
|
-
raise
|
|
118
|
-
"Le fichier PDF ne peut pas être traité car son format n'est pas \
|
|
119
|
-
correct. Vous pouvez essayer de le réimprimer dans un nouveau fichier PDF."
|
|
120
|
-
)
|
|
126
|
+
logger.error(f"Error while opening original PDF file : {error}")
|
|
127
|
+
raise HTTPUnsupportedMediaType(ERROR_PDF_FORMAT_MSG)
|
|
121
128
|
|
|
122
|
-
#
|
|
123
|
-
w = IncrementalPdfFileWriter(pdf_data.data, strict=False)
|
|
129
|
+
# Add signature field on the PDF
|
|
124
130
|
try:
|
|
125
131
|
fields.append_signature_field(
|
|
126
|
-
|
|
132
|
+
pdf_writer,
|
|
127
133
|
sig_field_spec=fields.SigFieldSpec(
|
|
128
134
|
"SignatureCAERP", box=(445, 800, 580, 830)
|
|
129
135
|
),
|
|
@@ -131,13 +137,22 @@ correct. Vous pouvez essayer de le réimprimer dans un nouveau fichier PDF."
|
|
|
131
137
|
except SigningError:
|
|
132
138
|
logger.warn("File is already signed, nothing to do")
|
|
133
139
|
return True
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
140
|
+
except ObjectHeaderReadError as error:
|
|
141
|
+
logger.error(f"Error while reading Xref header : {error}")
|
|
142
|
+
raise HTTPUnsupportedMediaType(ERROR_PDF_FORMAT_MSG)
|
|
143
|
+
|
|
144
|
+
# Sign PDF
|
|
145
|
+
try:
|
|
146
|
+
meta = signers.PdfSignatureMetadata(field_name="SignatureCAERP")
|
|
147
|
+
pdf_signer = signers.PdfSigner(
|
|
148
|
+
meta,
|
|
149
|
+
signer=signer,
|
|
150
|
+
stamp_style=stamp,
|
|
151
|
+
)
|
|
152
|
+
pdf_data.data = pdf_signer.sign_pdf(pdf_writer)
|
|
153
|
+
except Exception as error:
|
|
154
|
+
logger.error(f"Error while signing PDF file : {error}")
|
|
155
|
+
raise HTTPUnsupportedMediaType(ERROR_PDF_FORMAT_MSG)
|
|
141
156
|
logger.info(
|
|
142
157
|
"File '{}' signed successfully by {} !".format(
|
|
143
158
|
pdf_data.name, signer.subject_name
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
|
-
Name:
|
|
3
|
-
Version: 2024.1.
|
|
2
|
+
Name: caerp_sign_pdf
|
|
3
|
+
Version: 2024.1.4
|
|
4
4
|
Summary: caerp_sign_pdf
|
|
5
5
|
Home-page: https://framagit.org/caerp/caerp_sign_pdf
|
|
6
6
|
Author: Kilya
|
|
@@ -14,7 +14,7 @@ Classifier: Topic :: Internet :: WWW/HTTP
|
|
|
14
14
|
Classifier: Topic :: Internet :: WWW/HTTP :: WSGI :: Application
|
|
15
15
|
Description-Content-Type: text/x-rst
|
|
16
16
|
License-File: LICENSE.txt
|
|
17
|
-
Requires-Dist: pyHanko[image-support,opentype,pkcs11,xmp]
|
|
17
|
+
Requires-Dist: pyHanko[image-support,opentype,pkcs11,xmp] ==0.25.1
|
|
18
18
|
|
|
19
19
|
CAERP Sign PDF
|
|
20
20
|
======================================================
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
caerp_sign_pdf/__init__.py,sha256=vT-YO-kLKOqDIFKyVo_YGpLvZMZNNonAksW7dVWaScs,192
|
|
2
|
+
caerp_sign_pdf/models.py,sha256=R_-jV-hO0Pm4_Rd1evyNerx8vuXELhMecNODdI598B4,592
|
|
3
|
+
caerp_sign_pdf/public.py,sha256=Ocb7dULKWAz_Jhumb9SWOvAoPQ8cY1Jy3Aa3s3_tqV8,6090
|
|
4
|
+
caerp_sign_pdf-2024.1.4.dist-info/LICENSE.txt,sha256=jOtLnuWt7d5Hsx6XXB2QxzrSe2sWWh3NgMfFRetluQM,35147
|
|
5
|
+
caerp_sign_pdf-2024.1.4.dist-info/METADATA,sha256=9JAUmLKLwZHRGuOx7pZuvtLlowohjh9vWXTIUlhkMi0,2399
|
|
6
|
+
caerp_sign_pdf-2024.1.4.dist-info/WHEEL,sha256=yQN5g4mg4AybRjkgi-9yy4iQEFibGQmlz78Pik5Or-A,92
|
|
7
|
+
caerp_sign_pdf-2024.1.4.dist-info/top_level.txt,sha256=kNSdxjlamRUTFu_IvJHzd1pJIv12fmuWotrcJ2sRgEw,15
|
|
8
|
+
caerp_sign_pdf-2024.1.4.dist-info/RECORD,,
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
caerp_sign_pdf/__init__.py,sha256=vT-YO-kLKOqDIFKyVo_YGpLvZMZNNonAksW7dVWaScs,192
|
|
2
|
-
caerp_sign_pdf/models.py,sha256=R_-jV-hO0Pm4_Rd1evyNerx8vuXELhMecNODdI598B4,592
|
|
3
|
-
caerp_sign_pdf/public.py,sha256=VUMCfPACZ8xaqpldl6ly2BlWUnaHFExXhLIrBxttq9Q,5336
|
|
4
|
-
caerp_sign_pdf-2024.1.2.dist-info/LICENSE.txt,sha256=jOtLnuWt7d5Hsx6XXB2QxzrSe2sWWh3NgMfFRetluQM,35147
|
|
5
|
-
caerp_sign_pdf-2024.1.2.dist-info/METADATA,sha256=ZQCLNLiq0arJE3So7RfqIjb9h84M4S7x1fa4I17qBEY,2401
|
|
6
|
-
caerp_sign_pdf-2024.1.2.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
|
|
7
|
-
caerp_sign_pdf-2024.1.2.dist-info/top_level.txt,sha256=kNSdxjlamRUTFu_IvJHzd1pJIv12fmuWotrcJ2sRgEw,15
|
|
8
|
-
caerp_sign_pdf-2024.1.2.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|