Skip to content

Commit 81f38cc

Browse files
Split watermark_pdf by text and image watermarking
Assisted-by: Codex
1 parent 08e2b8c commit 81f38cc

4 files changed

Lines changed: 371 additions & 136 deletions

File tree

src/pdfrest/client.py

Lines changed: 203 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
PdfFlattenAnnotationsPayload,
8989
PdfFlattenFormsPayload,
9090
PdfFlattenTransparenciesPayload,
91+
PdfImageWatermarkPayload,
9192
PdfInfoPayload,
9293
PdfLinearizePayload,
9394
PdfMergePayload,
@@ -97,13 +98,13 @@
9798
PdfRestRawFileResponse,
9899
PdfRestrictPayload,
99100
PdfSplitPayload,
101+
PdfTextWatermarkPayload,
100102
PdfToExcelPayload,
101103
PdfToPdfaPayload,
102104
PdfToPdfxPayload,
103105
PdfToPowerpointPayload,
104106
PdfToWordPayload,
105107
PdfUnrestrictPayload,
106-
PdfWatermarkPayload,
107108
PdfXfaToAcroformsPayload,
108109
PngPdfRestPayload,
109110
SummarizePdfTextPayload,
@@ -226,6 +227,21 @@ def _parse_retry_after_header(header_value: str | None) -> float | None:
226227
return seconds if seconds > 0 else 0.0
227228

228229

230+
def _merge_non_default_values(
231+
*,
232+
payload: dict[str, Any],
233+
values: Mapping[str, Any],
234+
defaults: Mapping[str, Any],
235+
) -> None:
236+
payload.update(
237+
{
238+
key: value
239+
for key, value in values.items()
240+
if value is not None and (key not in defaults or value != defaults[key])
241+
}
242+
)
243+
244+
229245
FileContent = IO[bytes] | bytes | str
230246
FileTuple2 = tuple[str | None, FileContent]
231247
FileTuple3 = tuple[str | None, FileContent, str | None]
@@ -3082,18 +3098,16 @@ def rasterize_pdf(
30823098
timeout=timeout,
30833099
)
30843100

3085-
def watermark_pdf(
3101+
def watermark_pdf_with_text(
30863102
self,
30873103
file: PdfRestFile | Sequence[PdfRestFile],
30883104
*,
3089-
watermark_text: str | None = None,
3090-
watermark_file: PdfRestFile | Sequence[PdfRestFile] | None = None,
3105+
watermark_text: str,
30913106
output: str | None = None,
30923107
font: str | None = None,
30933108
text_size: int = 72,
30943109
text_color_rgb: PdfRGBColor | Sequence[int] | None = None,
30953110
text_color_cmyk: PdfCMYKColor | Sequence[int] | None = None,
3096-
watermark_file_scale: float = 0.5,
30973111
opacity: float = 0.5,
30983112
horizontal_alignment: WatermarkHorizontalAlignment = "center",
30993113
vertical_alignment: WatermarkVerticalAlignment = "center",
@@ -3107,34 +3121,107 @@ def watermark_pdf(
31073121
extra_body: Body | None = None,
31083122
timeout: TimeoutTypes | None = None,
31093123
) -> PdfRestFileBasedResponse:
3110-
"""Apply a text or file watermark to a PDF."""
3124+
"""Apply a text watermark to a PDF."""
31113125

31123126
payload: dict[str, Any] = {
31133127
"files": file,
31143128
"watermark_text": watermark_text,
3129+
}
3130+
_merge_non_default_values(
3131+
payload=payload,
3132+
values={
3133+
"output": output,
3134+
"font": font,
3135+
"text_size": text_size,
3136+
"text_color_rgb": text_color_rgb,
3137+
"text_color_cmyk": text_color_cmyk,
3138+
"opacity": opacity,
3139+
"horizontal_alignment": horizontal_alignment,
3140+
"vertical_alignment": vertical_alignment,
3141+
"x": x,
3142+
"y": y,
3143+
"rotation": rotation,
3144+
"pages": pages,
3145+
"behind_page": behind_page,
3146+
},
3147+
defaults={
3148+
"text_size": 72,
3149+
"opacity": 0.5,
3150+
"horizontal_alignment": "center",
3151+
"vertical_alignment": "center",
3152+
"x": 0,
3153+
"y": 0,
3154+
"rotation": 0,
3155+
"behind_page": False,
3156+
},
3157+
)
3158+
3159+
return self._post_file_operation(
3160+
endpoint="/watermarked-pdf",
3161+
payload=payload,
3162+
payload_model=PdfTextWatermarkPayload,
3163+
extra_query=extra_query,
3164+
extra_headers=extra_headers,
3165+
extra_body=extra_body,
3166+
timeout=timeout,
3167+
)
3168+
3169+
def watermark_pdf_with_image(
3170+
self,
3171+
file: PdfRestFile | Sequence[PdfRestFile],
3172+
*,
3173+
watermark_file: PdfRestFile | Sequence[PdfRestFile],
3174+
output: str | None = None,
3175+
watermark_file_scale: float = 0.5,
3176+
opacity: float = 0.5,
3177+
horizontal_alignment: WatermarkHorizontalAlignment = "center",
3178+
vertical_alignment: WatermarkVerticalAlignment = "center",
3179+
x: int = 0,
3180+
y: int = 0,
3181+
rotation: int = 0,
3182+
pages: PdfPageSelection | None = None,
3183+
behind_page: bool = False,
3184+
extra_query: Query | None = None,
3185+
extra_headers: AnyMapping | None = None,
3186+
extra_body: Body | None = None,
3187+
timeout: TimeoutTypes | None = None,
3188+
) -> PdfRestFileBasedResponse:
3189+
"""Apply an image watermark to a PDF."""
3190+
3191+
payload: dict[str, Any] = {
3192+
"files": file,
31153193
"watermark_file": watermark_file,
3116-
"font": font,
3117-
"text_size": text_size,
3118-
"text_color_rgb": text_color_rgb,
3119-
"text_color_cmyk": text_color_cmyk,
3120-
"watermark_file_scale": watermark_file_scale,
3121-
"opacity": opacity,
3122-
"horizontal_alignment": horizontal_alignment,
3123-
"vertical_alignment": vertical_alignment,
3124-
"x": x,
3125-
"y": y,
3126-
"rotation": rotation,
3127-
"behind_page": behind_page,
31283194
}
3129-
if output is not None:
3130-
payload["output"] = output
3131-
if pages is not None:
3132-
payload["pages"] = pages
3195+
_merge_non_default_values(
3196+
payload=payload,
3197+
values={
3198+
"output": output,
3199+
"watermark_file_scale": watermark_file_scale,
3200+
"opacity": opacity,
3201+
"horizontal_alignment": horizontal_alignment,
3202+
"vertical_alignment": vertical_alignment,
3203+
"x": x,
3204+
"y": y,
3205+
"rotation": rotation,
3206+
"pages": pages,
3207+
"behind_page": behind_page,
3208+
},
3209+
defaults={
3210+
"watermark_file_scale": 0.5,
3211+
"opacity": 0.5,
3212+
"horizontal_alignment": "center",
3213+
"vertical_alignment": "center",
3214+
"x": 0,
3215+
"y": 0,
3216+
"rotation": 0,
3217+
"behind_page": False,
3218+
},
3219+
)
31333220

31343221
return self._post_file_operation(
31353222
endpoint="/watermarked-pdf",
31363223
payload=payload,
3137-
payload_model=PdfWatermarkPayload,
3224+
payload_model=PdfImageWatermarkPayload,
31383225
extra_query=extra_query,
31393226
extra_headers=extra_headers,
31403227
extra_body=extra_body,
@@ -4428,18 +4515,16 @@ async def rasterize_pdf(
44284515
timeout=timeout,
44294516
)
44304517

4431-
async def watermark_pdf(
4518+
async def watermark_pdf_with_text(
44324519
self,
44334520
file: PdfRestFile | Sequence[PdfRestFile],
44344521
*,
4435-
watermark_text: str | None = None,
4436-
watermark_file: PdfRestFile | Sequence[PdfRestFile] | None = None,
4522+
watermark_text: str,
44374523
output: str | None = None,
44384524
font: str | None = None,
44394525
text_size: int = 72,
44404526
text_color_rgb: PdfRGBColor | Sequence[int] | None = None,
44414527
text_color_cmyk: PdfCMYKColor | Sequence[int] | None = None,
4442-
watermark_file_scale: float = 0.5,
44434528
opacity: float = 0.5,
44444529
horizontal_alignment: WatermarkHorizontalAlignment = "center",
44454530
vertical_alignment: WatermarkVerticalAlignment = "center",
@@ -4453,34 +4538,107 @@ async def watermark_pdf(
44534538
extra_body: Body | None = None,
44544539
timeout: TimeoutTypes | None = None,
44554540
) -> PdfRestFileBasedResponse:
4456-
"""Asynchronously apply a text or file watermark to a PDF."""
4541+
"""Asynchronously apply a text watermark to a PDF."""
44574542

44584543
payload: dict[str, Any] = {
44594544
"files": file,
44604545
"watermark_text": watermark_text,
4546+
}
4547+
_merge_non_default_values(
4548+
payload=payload,
4549+
values={
4550+
"output": output,
4551+
"font": font,
4552+
"text_size": text_size,
4553+
"text_color_rgb": text_color_rgb,
4554+
"text_color_cmyk": text_color_cmyk,
4555+
"opacity": opacity,
4556+
"horizontal_alignment": horizontal_alignment,
4557+
"vertical_alignment": vertical_alignment,
4558+
"x": x,
4559+
"y": y,
4560+
"rotation": rotation,
4561+
"pages": pages,
4562+
"behind_page": behind_page,
4563+
},
4564+
defaults={
4565+
"text_size": 72,
4566+
"opacity": 0.5,
4567+
"horizontal_alignment": "center",
4568+
"vertical_alignment": "center",
4569+
"x": 0,
4570+
"y": 0,
4571+
"rotation": 0,
4572+
"behind_page": False,
4573+
},
4574+
)
4575+
4576+
return await self._post_file_operation(
4577+
endpoint="/watermarked-pdf",
4578+
payload=payload,
4579+
payload_model=PdfTextWatermarkPayload,
4580+
extra_query=extra_query,
4581+
extra_headers=extra_headers,
4582+
extra_body=extra_body,
4583+
timeout=timeout,
4584+
)
4585+
4586+
async def watermark_pdf_with_image(
4587+
self,
4588+
file: PdfRestFile | Sequence[PdfRestFile],
4589+
*,
4590+
watermark_file: PdfRestFile | Sequence[PdfRestFile],
4591+
output: str | None = None,
4592+
watermark_file_scale: float = 0.5,
4593+
opacity: float = 0.5,
4594+
horizontal_alignment: WatermarkHorizontalAlignment = "center",
4595+
vertical_alignment: WatermarkVerticalAlignment = "center",
4596+
x: int = 0,
4597+
y: int = 0,
4598+
rotation: int = 0,
4599+
pages: PdfPageSelection | None = None,
4600+
behind_page: bool = False,
4601+
extra_query: Query | None = None,
4602+
extra_headers: AnyMapping | None = None,
4603+
extra_body: Body | None = None,
4604+
timeout: TimeoutTypes | None = None,
4605+
) -> PdfRestFileBasedResponse:
4606+
"""Asynchronously apply an image watermark to a PDF."""
4607+
4608+
payload: dict[str, Any] = {
4609+
"files": file,
44614610
"watermark_file": watermark_file,
4462-
"font": font,
4463-
"text_size": text_size,
4464-
"text_color_rgb": text_color_rgb,
4465-
"text_color_cmyk": text_color_cmyk,
4466-
"watermark_file_scale": watermark_file_scale,
4467-
"opacity": opacity,
4468-
"horizontal_alignment": horizontal_alignment,
4469-
"vertical_alignment": vertical_alignment,
4470-
"x": x,
4471-
"y": y,
4472-
"rotation": rotation,
4473-
"behind_page": behind_page,
44744611
}
4475-
if output is not None:
4476-
payload["output"] = output
4477-
if pages is not None:
4478-
payload["pages"] = pages
4612+
_merge_non_default_values(
4613+
payload=payload,
4614+
values={
4615+
"output": output,
4616+
"watermark_file_scale": watermark_file_scale,
4617+
"opacity": opacity,
4618+
"horizontal_alignment": horizontal_alignment,
4619+
"vertical_alignment": vertical_alignment,
4620+
"x": x,
4621+
"y": y,
4622+
"rotation": rotation,
4623+
"pages": pages,
4624+
"behind_page": behind_page,
4625+
},
4626+
defaults={
4627+
"watermark_file_scale": 0.5,
4628+
"opacity": 0.5,
4629+
"horizontal_alignment": "center",
4630+
"vertical_alignment": "center",
4631+
"x": 0,
4632+
"y": 0,
4633+
"rotation": 0,
4634+
"behind_page": False,
4635+
},
4636+
)
44794637

44804638
return await self._post_file_operation(
44814639
endpoint="/watermarked-pdf",
44824640
payload=payload,
4483-
payload_model=PdfWatermarkPayload,
4641+
payload_model=PdfImageWatermarkPayload,
44844642
extra_query=extra_query,
44854643
extra_headers=extra_headers,
44864644
extra_body=extra_body,

0 commit comments

Comments
 (0)