Skip to content

Commit 1708407

Browse files
Split watermark_pdf by text and image watermarking
Assisted-by: Codex
1 parent 925568e commit 1708407

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
@@ -91,6 +91,7 @@
9191
PdfFlattenAnnotationsPayload,
9292
PdfFlattenFormsPayload,
9393
PdfFlattenTransparenciesPayload,
94+
PdfImageWatermarkPayload,
9495
PdfInfoPayload,
9596
PdfLinearizePayload,
9697
PdfMergePayload,
@@ -100,13 +101,13 @@
100101
PdfRestRawFileResponse,
101102
PdfRestrictPayload,
102103
PdfSplitPayload,
104+
PdfTextWatermarkPayload,
103105
PdfToExcelPayload,
104106
PdfToPdfaPayload,
105107
PdfToPdfxPayload,
106108
PdfToPowerpointPayload,
107109
PdfToWordPayload,
108110
PdfUnrestrictPayload,
109-
PdfWatermarkPayload,
110111
PdfXfaToAcroformsPayload,
111112
PngPdfRestPayload,
112113
SummarizePdfTextPayload,
@@ -230,6 +231,21 @@ def _parse_retry_after_header(header_value: str | None) -> float | None:
230231
return seconds if seconds > 0 else 0.0
231232

232233

234+
def _merge_non_default_values(
235+
*,
236+
payload: dict[str, Any],
237+
values: Mapping[str, Any],
238+
defaults: Mapping[str, Any],
239+
) -> None:
240+
payload.update(
241+
{
242+
key: value
243+
for key, value in values.items()
244+
if value is not None and (key not in defaults or value != defaults[key])
245+
}
246+
)
247+
248+
233249
FileContent = IO[bytes] | bytes | str
234250
FileTuple2 = tuple[str | None, FileContent]
235251
FileTuple3 = tuple[str | None, FileContent, str | None]
@@ -3179,18 +3195,16 @@ def rasterize_pdf(
31793195
timeout=timeout,
31803196
)
31813197

3182-
def watermark_pdf(
3198+
def watermark_pdf_with_text(
31833199
self,
31843200
file: PdfRestFile | Sequence[PdfRestFile],
31853201
*,
3186-
watermark_text: str | None = None,
3187-
watermark_file: PdfRestFile | Sequence[PdfRestFile] | None = None,
3202+
watermark_text: str,
31883203
output: str | None = None,
31893204
font: str | None = None,
31903205
text_size: int = 72,
31913206
text_color_rgb: PdfRGBColor | Sequence[int] | None = None,
31923207
text_color_cmyk: PdfCMYKColor | Sequence[int] | None = None,
3193-
watermark_file_scale: float = 0.5,
31943208
opacity: float = 0.5,
31953209
horizontal_alignment: WatermarkHorizontalAlignment = "center",
31963210
vertical_alignment: WatermarkVerticalAlignment = "center",
@@ -3204,34 +3218,107 @@ def watermark_pdf(
32043218
extra_body: Body | None = None,
32053219
timeout: TimeoutTypes | None = None,
32063220
) -> PdfRestFileBasedResponse:
3207-
"""Apply a text or file watermark to a PDF."""
3221+
"""Apply a text watermark to a PDF."""
32083222

32093223
payload: dict[str, Any] = {
32103224
"files": file,
32113225
"watermark_text": watermark_text,
3226+
}
3227+
_merge_non_default_values(
3228+
payload=payload,
3229+
values={
3230+
"output": output,
3231+
"font": font,
3232+
"text_size": text_size,
3233+
"text_color_rgb": text_color_rgb,
3234+
"text_color_cmyk": text_color_cmyk,
3235+
"opacity": opacity,
3236+
"horizontal_alignment": horizontal_alignment,
3237+
"vertical_alignment": vertical_alignment,
3238+
"x": x,
3239+
"y": y,
3240+
"rotation": rotation,
3241+
"pages": pages,
3242+
"behind_page": behind_page,
3243+
},
3244+
defaults={
3245+
"text_size": 72,
3246+
"opacity": 0.5,
3247+
"horizontal_alignment": "center",
3248+
"vertical_alignment": "center",
3249+
"x": 0,
3250+
"y": 0,
3251+
"rotation": 0,
3252+
"behind_page": False,
3253+
},
3254+
)
3255+
3256+
return self._post_file_operation(
3257+
endpoint="/watermarked-pdf",
3258+
payload=payload,
3259+
payload_model=PdfTextWatermarkPayload,
3260+
extra_query=extra_query,
3261+
extra_headers=extra_headers,
3262+
extra_body=extra_body,
3263+
timeout=timeout,
3264+
)
3265+
3266+
def watermark_pdf_with_image(
3267+
self,
3268+
file: PdfRestFile | Sequence[PdfRestFile],
3269+
*,
3270+
watermark_file: PdfRestFile | Sequence[PdfRestFile],
3271+
output: str | None = None,
3272+
watermark_file_scale: float = 0.5,
3273+
opacity: float = 0.5,
3274+
horizontal_alignment: WatermarkHorizontalAlignment = "center",
3275+
vertical_alignment: WatermarkVerticalAlignment = "center",
3276+
x: int = 0,
3277+
y: int = 0,
3278+
rotation: int = 0,
3279+
pages: PdfPageSelection | None = None,
3280+
behind_page: bool = False,
3281+
extra_query: Query | None = None,
3282+
extra_headers: AnyMapping | None = None,
3283+
extra_body: Body | None = None,
3284+
timeout: TimeoutTypes | None = None,
3285+
) -> PdfRestFileBasedResponse:
3286+
"""Apply an image watermark to a PDF."""
3287+
3288+
payload: dict[str, Any] = {
3289+
"files": file,
32123290
"watermark_file": watermark_file,
3213-
"font": font,
3214-
"text_size": text_size,
3215-
"text_color_rgb": text_color_rgb,
3216-
"text_color_cmyk": text_color_cmyk,
3217-
"watermark_file_scale": watermark_file_scale,
3218-
"opacity": opacity,
3219-
"horizontal_alignment": horizontal_alignment,
3220-
"vertical_alignment": vertical_alignment,
3221-
"x": x,
3222-
"y": y,
3223-
"rotation": rotation,
3224-
"behind_page": behind_page,
32253291
}
3226-
if output is not None:
3227-
payload["output"] = output
3228-
if pages is not None:
3229-
payload["pages"] = pages
3292+
_merge_non_default_values(
3293+
payload=payload,
3294+
values={
3295+
"output": output,
3296+
"watermark_file_scale": watermark_file_scale,
3297+
"opacity": opacity,
3298+
"horizontal_alignment": horizontal_alignment,
3299+
"vertical_alignment": vertical_alignment,
3300+
"x": x,
3301+
"y": y,
3302+
"rotation": rotation,
3303+
"pages": pages,
3304+
"behind_page": behind_page,
3305+
},
3306+
defaults={
3307+
"watermark_file_scale": 0.5,
3308+
"opacity": 0.5,
3309+
"horizontal_alignment": "center",
3310+
"vertical_alignment": "center",
3311+
"x": 0,
3312+
"y": 0,
3313+
"rotation": 0,
3314+
"behind_page": False,
3315+
},
3316+
)
32303317

32313318
return self._post_file_operation(
32323319
endpoint="/watermarked-pdf",
32333320
payload=payload,
3234-
payload_model=PdfWatermarkPayload,
3321+
payload_model=PdfImageWatermarkPayload,
32353322
extra_query=extra_query,
32363323
extra_headers=extra_headers,
32373324
extra_body=extra_body,
@@ -4618,18 +4705,16 @@ async def rasterize_pdf(
46184705
timeout=timeout,
46194706
)
46204707

4621-
async def watermark_pdf(
4708+
async def watermark_pdf_with_text(
46224709
self,
46234710
file: PdfRestFile | Sequence[PdfRestFile],
46244711
*,
4625-
watermark_text: str | None = None,
4626-
watermark_file: PdfRestFile | Sequence[PdfRestFile] | None = None,
4712+
watermark_text: str,
46274713
output: str | None = None,
46284714
font: str | None = None,
46294715
text_size: int = 72,
46304716
text_color_rgb: PdfRGBColor | Sequence[int] | None = None,
46314717
text_color_cmyk: PdfCMYKColor | Sequence[int] | None = None,
4632-
watermark_file_scale: float = 0.5,
46334718
opacity: float = 0.5,
46344719
horizontal_alignment: WatermarkHorizontalAlignment = "center",
46354720
vertical_alignment: WatermarkVerticalAlignment = "center",
@@ -4643,34 +4728,107 @@ async def watermark_pdf(
46434728
extra_body: Body | None = None,
46444729
timeout: TimeoutTypes | None = None,
46454730
) -> PdfRestFileBasedResponse:
4646-
"""Asynchronously apply a text or file watermark to a PDF."""
4731+
"""Asynchronously apply a text watermark to a PDF."""
46474732

46484733
payload: dict[str, Any] = {
46494734
"files": file,
46504735
"watermark_text": watermark_text,
4736+
}
4737+
_merge_non_default_values(
4738+
payload=payload,
4739+
values={
4740+
"output": output,
4741+
"font": font,
4742+
"text_size": text_size,
4743+
"text_color_rgb": text_color_rgb,
4744+
"text_color_cmyk": text_color_cmyk,
4745+
"opacity": opacity,
4746+
"horizontal_alignment": horizontal_alignment,
4747+
"vertical_alignment": vertical_alignment,
4748+
"x": x,
4749+
"y": y,
4750+
"rotation": rotation,
4751+
"pages": pages,
4752+
"behind_page": behind_page,
4753+
},
4754+
defaults={
4755+
"text_size": 72,
4756+
"opacity": 0.5,
4757+
"horizontal_alignment": "center",
4758+
"vertical_alignment": "center",
4759+
"x": 0,
4760+
"y": 0,
4761+
"rotation": 0,
4762+
"behind_page": False,
4763+
},
4764+
)
4765+
4766+
return await self._post_file_operation(
4767+
endpoint="/watermarked-pdf",
4768+
payload=payload,
4769+
payload_model=PdfTextWatermarkPayload,
4770+
extra_query=extra_query,
4771+
extra_headers=extra_headers,
4772+
extra_body=extra_body,
4773+
timeout=timeout,
4774+
)
4775+
4776+
async def watermark_pdf_with_image(
4777+
self,
4778+
file: PdfRestFile | Sequence[PdfRestFile],
4779+
*,
4780+
watermark_file: PdfRestFile | Sequence[PdfRestFile],
4781+
output: str | None = None,
4782+
watermark_file_scale: float = 0.5,
4783+
opacity: float = 0.5,
4784+
horizontal_alignment: WatermarkHorizontalAlignment = "center",
4785+
vertical_alignment: WatermarkVerticalAlignment = "center",
4786+
x: int = 0,
4787+
y: int = 0,
4788+
rotation: int = 0,
4789+
pages: PdfPageSelection | None = None,
4790+
behind_page: bool = False,
4791+
extra_query: Query | None = None,
4792+
extra_headers: AnyMapping | None = None,
4793+
extra_body: Body | None = None,
4794+
timeout: TimeoutTypes | None = None,
4795+
) -> PdfRestFileBasedResponse:
4796+
"""Asynchronously apply an image watermark to a PDF."""
4797+
4798+
payload: dict[str, Any] = {
4799+
"files": file,
46514800
"watermark_file": watermark_file,
4652-
"font": font,
4653-
"text_size": text_size,
4654-
"text_color_rgb": text_color_rgb,
4655-
"text_color_cmyk": text_color_cmyk,
4656-
"watermark_file_scale": watermark_file_scale,
4657-
"opacity": opacity,
4658-
"horizontal_alignment": horizontal_alignment,
4659-
"vertical_alignment": vertical_alignment,
4660-
"x": x,
4661-
"y": y,
4662-
"rotation": rotation,
4663-
"behind_page": behind_page,
46644801
}
4665-
if output is not None:
4666-
payload["output"] = output
4667-
if pages is not None:
4668-
payload["pages"] = pages
4802+
_merge_non_default_values(
4803+
payload=payload,
4804+
values={
4805+
"output": output,
4806+
"watermark_file_scale": watermark_file_scale,
4807+
"opacity": opacity,
4808+
"horizontal_alignment": horizontal_alignment,
4809+
"vertical_alignment": vertical_alignment,
4810+
"x": x,
4811+
"y": y,
4812+
"rotation": rotation,
4813+
"pages": pages,
4814+
"behind_page": behind_page,
4815+
},
4816+
defaults={
4817+
"watermark_file_scale": 0.5,
4818+
"opacity": 0.5,
4819+
"horizontal_alignment": "center",
4820+
"vertical_alignment": "center",
4821+
"x": 0,
4822+
"y": 0,
4823+
"rotation": 0,
4824+
"behind_page": False,
4825+
},
4826+
)
46694827

46704828
return await self._post_file_operation(
46714829
endpoint="/watermarked-pdf",
46724830
payload=payload,
4673-
payload_model=PdfWatermarkPayload,
4831+
payload_model=PdfImageWatermarkPayload,
46744832
extra_query=extra_query,
46754833
extra_headers=extra_headers,
46764834
extra_body=extra_body,

0 commit comments

Comments
 (0)