Skip to content

Commit 16120cd

Browse files
Merge pull request #11 from datalogics-cgreen/pdfcloud-5464-security
PDFCLOUD-5547 Add PDF encryption and restriction client methods
2 parents 8563cd7 + 85ff431 commit 16120cd

8 files changed

Lines changed: 2984 additions & 2 deletions

File tree

src/pdfrest/client.py

Lines changed: 421 additions & 0 deletions
Large diffs are not rendered by default.

src/pdfrest/models/_internal.py

Lines changed: 189 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626
OcrLanguage,
2727
PdfAType,
2828
PdfInfoQuery,
29+
PdfRestriction,
2930
PdfXType,
3031
SummaryFormat,
3132
SummaryOutputFormat,
3233
SummaryOutputType,
3334
TranslateOutputFormat,
3435
)
35-
from . import PdfRestFile
36-
from .public import PdfRestFileID
36+
from .public import PdfRestFile, PdfRestFileID
3737

3838

3939
def _ensure_list(value: Any) -> Any:
@@ -1142,6 +1142,193 @@ class PdfFlattenAnnotationsPayload(BaseModel):
11421142
] = None
11431143

11441144

1145+
class PdfRestrictPayload(BaseModel):
1146+
"""Adapt caller options into a pdfRest-ready restrict-PDF request payload."""
1147+
1148+
files: Annotated[
1149+
list[PdfRestFile],
1150+
Field(
1151+
min_length=1,
1152+
max_length=1,
1153+
validation_alias=AliasChoices("file", "files"),
1154+
serialization_alias="id",
1155+
),
1156+
BeforeValidator(_ensure_list),
1157+
AfterValidator(
1158+
_allowed_mime_types("application/pdf", error_msg="Must be a PDF file")
1159+
),
1160+
PlainSerializer(_serialize_as_first_file_id),
1161+
]
1162+
new_permissions_password: Annotated[
1163+
str,
1164+
Field(
1165+
serialization_alias="new_permissions_password",
1166+
min_length=6,
1167+
max_length=128,
1168+
),
1169+
]
1170+
restrictions: Annotated[
1171+
list[PdfRestriction] | None,
1172+
Field(serialization_alias="restrictions", min_length=1, default=None),
1173+
BeforeValidator(_ensure_list),
1174+
] = None
1175+
current_permissions_password: Annotated[
1176+
str | None,
1177+
Field(
1178+
serialization_alias="current_permissions_password",
1179+
min_length=1,
1180+
max_length=128,
1181+
default=None,
1182+
),
1183+
] = None
1184+
current_open_password: Annotated[
1185+
str | None,
1186+
Field(
1187+
serialization_alias="current_open_password",
1188+
min_length=1,
1189+
max_length=128,
1190+
default=None,
1191+
),
1192+
] = None
1193+
output: Annotated[
1194+
str | None,
1195+
Field(serialization_alias="output", min_length=1, default=None),
1196+
AfterValidator(_validate_output_prefix),
1197+
] = None
1198+
1199+
1200+
class PdfUnrestrictPayload(BaseModel):
1201+
"""Adapt caller options into a pdfRest-ready unrestrict-PDF request payload."""
1202+
1203+
files: Annotated[
1204+
list[PdfRestFile],
1205+
Field(
1206+
min_length=1,
1207+
max_length=1,
1208+
validation_alias=AliasChoices("file", "files"),
1209+
serialization_alias="id",
1210+
),
1211+
BeforeValidator(_ensure_list),
1212+
AfterValidator(
1213+
_allowed_mime_types("application/pdf", error_msg="Must be a PDF file")
1214+
),
1215+
PlainSerializer(_serialize_as_first_file_id),
1216+
]
1217+
current_permissions_password: Annotated[
1218+
str,
1219+
Field(
1220+
serialization_alias="current_permissions_password",
1221+
min_length=1,
1222+
max_length=128,
1223+
),
1224+
]
1225+
current_open_password: Annotated[
1226+
str | None,
1227+
Field(
1228+
serialization_alias="current_open_password",
1229+
min_length=1,
1230+
max_length=128,
1231+
default=None,
1232+
),
1233+
] = None
1234+
output: Annotated[
1235+
str | None,
1236+
Field(serialization_alias="output", min_length=1, default=None),
1237+
AfterValidator(_validate_output_prefix),
1238+
] = None
1239+
1240+
1241+
class PdfEncryptPayload(BaseModel):
1242+
"""Adapt caller options into a pdfRest-ready encrypt-PDF request payload."""
1243+
1244+
files: Annotated[
1245+
list[PdfRestFile],
1246+
Field(
1247+
min_length=1,
1248+
max_length=1,
1249+
validation_alias=AliasChoices("file", "files"),
1250+
serialization_alias="id",
1251+
),
1252+
BeforeValidator(_ensure_list),
1253+
AfterValidator(
1254+
_allowed_mime_types("application/pdf", error_msg="Must be a PDF file")
1255+
),
1256+
PlainSerializer(_serialize_as_first_file_id),
1257+
]
1258+
new_open_password: Annotated[
1259+
str,
1260+
Field(
1261+
serialization_alias="new_open_password",
1262+
min_length=6,
1263+
max_length=128,
1264+
),
1265+
]
1266+
current_open_password: Annotated[
1267+
str | None,
1268+
Field(
1269+
serialization_alias="current_open_password",
1270+
min_length=1,
1271+
max_length=128,
1272+
default=None,
1273+
),
1274+
] = None
1275+
current_permissions_password: Annotated[
1276+
str | None,
1277+
Field(
1278+
serialization_alias="current_permissions_password",
1279+
min_length=1,
1280+
max_length=128,
1281+
default=None,
1282+
),
1283+
] = None
1284+
output: Annotated[
1285+
str | None,
1286+
Field(serialization_alias="output", min_length=1, default=None),
1287+
AfterValidator(_validate_output_prefix),
1288+
] = None
1289+
1290+
1291+
class PdfDecryptPayload(BaseModel):
1292+
"""Adapt caller options into a pdfRest-ready decrypt-PDF request payload."""
1293+
1294+
files: Annotated[
1295+
list[PdfRestFile],
1296+
Field(
1297+
min_length=1,
1298+
max_length=1,
1299+
validation_alias=AliasChoices("file", "files"),
1300+
serialization_alias="id",
1301+
),
1302+
BeforeValidator(_ensure_list),
1303+
AfterValidator(
1304+
_allowed_mime_types("application/pdf", error_msg="Must be a PDF file")
1305+
),
1306+
PlainSerializer(_serialize_as_first_file_id),
1307+
]
1308+
current_open_password: Annotated[
1309+
str,
1310+
Field(
1311+
serialization_alias="current_open_password",
1312+
min_length=1,
1313+
max_length=128,
1314+
),
1315+
]
1316+
current_permissions_password: Annotated[
1317+
str | None,
1318+
Field(
1319+
serialization_alias="current_permissions_password",
1320+
min_length=1,
1321+
max_length=128,
1322+
default=None,
1323+
),
1324+
] = None
1325+
output: Annotated[
1326+
str | None,
1327+
Field(serialization_alias="output", min_length=1, default=None),
1328+
AfterValidator(_validate_output_prefix),
1329+
] = None
1330+
1331+
11451332
class BmpPdfRestPayload(BasePdfRestGraphicPayload[Literal["rgb", "gray"]]):
11461333
"""Adapt caller options into a pdfRest-ready BMP request payload."""
11471334

src/pdfrest/types/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from .public import (
44
ALL_OCR_LANGUAGES,
55
ALL_PDF_INFO_QUERIES,
6+
ALL_PDF_RESTRICTIONS,
67
BmpColorModel,
78
CompressionLevel,
89
ExtractTextGranularity,
@@ -19,6 +20,7 @@
1920
PdfRedactionInstruction,
2021
PdfRedactionPreset,
2122
PdfRedactionType,
23+
PdfRestriction,
2224
PdfRGBColor,
2325
PdfXType,
2426
PngColorModel,
@@ -32,6 +34,7 @@
3234
__all__ = [
3335
"ALL_OCR_LANGUAGES",
3436
"ALL_PDF_INFO_QUERIES",
37+
"ALL_PDF_RESTRICTIONS",
3538
"BmpColorModel",
3639
"CompressionLevel",
3740
"ExtractTextGranularity",
@@ -49,6 +52,7 @@
4952
"PdfRedactionInstruction",
5053
"PdfRedactionPreset",
5154
"PdfRedactionType",
55+
"PdfRestriction",
5256
"PdfXType",
5357
"PngColorModel",
5458
"SummaryFormat",

src/pdfrest/types/public.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
__all__ = (
1616
"ALL_OCR_LANGUAGES",
1717
"ALL_PDF_INFO_QUERIES",
18+
"ALL_PDF_RESTRICTIONS",
1819
"BmpColorModel",
1920
"CompressionLevel",
2021
"ExtractTextGranularity",
@@ -32,6 +33,7 @@
3233
"PdfRedactionInstruction",
3334
"PdfRedactionPreset",
3435
"PdfRedactionType",
36+
"PdfRestriction",
3537
"PdfXType",
3638
"PngColorModel",
3739
"SummaryFormat",
@@ -160,3 +162,18 @@ class PdfMergeSource(TypedDict, total=False):
160162
ALL_OCR_LANGUAGES: tuple[OcrLanguage, ...] = cast(
161163
tuple[OcrLanguage, ...], get_args(OcrLanguage)
162164
)
165+
166+
PdfRestriction = Literal[
167+
"print_low",
168+
"print_high",
169+
"edit_document_assembly",
170+
"edit_fill_and_sign_form_fields",
171+
"edit_annotations",
172+
"edit_content",
173+
"copy_content",
174+
"accessibility_off",
175+
]
176+
177+
ALL_PDF_RESTRICTIONS: tuple[PdfRestriction, ...] = cast(
178+
tuple[PdfRestriction, ...], get_args(PdfRestriction)
179+
)

0 commit comments

Comments
 (0)