caerp-sign-pdf 2024.1.0__py3-none-any.whl → 2024.1.2__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 CHANGED
@@ -4,12 +4,11 @@ import hashlib
4
4
 
5
5
  from io import BytesIO
6
6
  from pyhanko.pdf_utils.incremental_writer import IncrementalPdfFileWriter
7
- from pyhanko.sign import (
8
- fields,
9
- signers,
10
- )
7
+ from pyhanko.sign import fields, signers
11
8
  from pyhanko.sign.general import SigningError
12
9
  from pyhanko.stamp import TextStampStyle
10
+ from PyPDF4.pdf import PdfFileReader, PdfFileWriter, utils
11
+ from pyramid.httpexceptions import HTTPInternalServerError
13
12
 
14
13
  from caerp.views.files.controller import FileData
15
14
 
@@ -20,7 +19,6 @@ logger = logging.getLogger(f"caerp.{__name__}")
20
19
 
21
20
 
22
21
  class SignPDFService(object):
23
-
24
22
  def __init__(self, context, request):
25
23
  self.context = context
26
24
  self.request = request
@@ -52,6 +50,19 @@ class SignPDFService(object):
52
50
  """
53
51
  return hashlib.md5(file_data.getvalue()).hexdigest()
54
52
 
53
+ def _get_safe_pdf(self, file_data: BytesIO) -> str:
54
+ """
55
+ Create a new clean PDF buffer from the original
56
+ to avoid some format errors
57
+ """
58
+ writer = PdfFileWriter()
59
+ reader = PdfFileReader(file_data, strict=False)
60
+ num_pages = reader.getNumPages()
61
+ for page in range(num_pages):
62
+ writer.addPage(reader.getPage(page))
63
+ writer.write(file_data)
64
+ return file_data
65
+
55
66
  def sign(
56
67
  self, pdf_data: FileData, node_id: int = None, with_stamp: bool = False
57
68
  ) -> bool:
@@ -92,8 +103,23 @@ in config)"
92
103
  stamp_text="Signé par: %(signer)s\nLe: %(ts)s",
93
104
  border_width=2,
94
105
  )
106
+ else:
107
+ stamp = TextStampStyle(
108
+ stamp_text="",
109
+ border_width=0,
110
+ )
111
+
112
+ # Clean PDF before signing
113
+ try:
114
+ pdf_data.data = self._get_safe_pdf(pdf_data.data)
115
+ except Exception as error:
116
+ logger.error(f"Unknown error while cleaning original PDF file : {error}")
117
+ raise HTTPInternalServerError(
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
+ )
95
121
 
96
- # Sign
122
+ # Sign PDF
97
123
  w = IncrementalPdfFileWriter(pdf_data.data, strict=False)
98
124
  try:
99
125
  fields.append_signature_field(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: caerp-sign-pdf
3
- Version: 2024.1.0
3
+ Version: 2024.1.2
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] (==0.20.1)
17
+ Requires-Dist: pyHanko[image-support,opentype,pkcs11,xmp] (==0.25.0)
18
18
 
19
19
  CAERP Sign PDF
20
20
  ======================================================
@@ -38,21 +38,35 @@ L'API publique est configurable dans CAERP au travers du fichier de configuratio
38
38
  caerp_sign_pdf
39
39
 
40
40
 
41
- ** Configurez le service `caerp.interfaces.ISignPDFService` **
41
+ ** Configurez le service **
42
42
 
43
43
  .. code-block:: console
44
44
 
45
- caerp.interfaces.ISignPDFService = caerp_sign_pdf.public.SignPDFService
45
+ caerp.services.sign_pdf_service = caerp_sign_pdf.public.SignPDFService
46
46
 
47
47
 
48
48
  ** Configurez le chemin vers le certificat à utiliser pour signer et sa clé secrète **
49
49
 
50
50
  .. code-block:: console
51
51
 
52
- caerp.sign_certificate_path=/path/to/certificate.pfx
53
- caerp.sign_certificate_passphrase=**************************
52
+ caerp.sign_certificate_path = /path/to/certificate.p12
53
+ caerp.sign_certificate_passphrase = **************************
54
54
 
55
55
 
56
56
  ** Configurez les journaux de `caerp_sign_pdf` **
57
57
 
58
58
  Voir la [documentation sur le module Python `logging`](https://docs.python.org/2/library/logging.html) pour le détail ainsi que les exemples dans le fichier development.ini.sample.
59
+
60
+
61
+
62
+ Vérification de la signature d'un document
63
+ ------------------------------------------
64
+
65
+ La plupart des clients PDF permettent de visualiser et contrôler la signature numérique des documents, mais pas tous : les navigateurs web ne les affichent pas par défaut, de même pour certaines applications mobiles ou volontairement très simple.
66
+
67
+ Sous linux la signature d'un document PDF peut être vérifiée facilement en ligne de commande grâce à l'utilitaire `pdfsig` de la librairie `poppler-utils` :
68
+
69
+ .. code-block:: console
70
+
71
+ pdfsig <monfichierpdf.pdf>
72
+
@@ -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=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,,
@@ -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=ZKPzyO8EoCME2g_nEdAuDGLmwI3AgpLPQI-0yhSX0Es,4203
4
- caerp_sign_pdf-2024.1.0.dist-info/LICENSE.txt,sha256=jOtLnuWt7d5Hsx6XXB2QxzrSe2sWWh3NgMfFRetluQM,35147
5
- caerp_sign_pdf-2024.1.0.dist-info/METADATA,sha256=gChMaEhufnTKH-EsMTL489bGUQRLf1aNZt3CXvBMOek,1870
6
- caerp_sign_pdf-2024.1.0.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
7
- caerp_sign_pdf-2024.1.0.dist-info/top_level.txt,sha256=kNSdxjlamRUTFu_IvJHzd1pJIv12fmuWotrcJ2sRgEw,15
8
- caerp_sign_pdf-2024.1.0.dist-info/RECORD,,