Skip to content

Commit d705f48

Browse files
Add Add Image methods
Assisted-by: Codex
1 parent 0dfabfb commit d705f48

2 files changed

Lines changed: 120 additions & 0 deletions

File tree

src/pdfrest/client.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
JpegPdfRestPayload,
8383
OcrPdfPayload,
8484
PdfAddAttachmentPayload,
85+
PdfAddImagePayload,
8586
PdfCompressPayload,
8687
PdfFlattenAnnotationsPayload,
8788
PdfFlattenFormsPayload,
@@ -2500,6 +2501,42 @@ def apply_redactions(
25002501
timeout=timeout,
25012502
)
25022503

2504+
def add_image_to_pdf(
2505+
self,
2506+
file: PdfRestFile | Sequence[PdfRestFile],
2507+
*,
2508+
image: PdfRestFile | Sequence[PdfRestFile],
2509+
x: int,
2510+
y: int,
2511+
page: int,
2512+
output: str | None = None,
2513+
extra_query: Query | None = None,
2514+
extra_headers: AnyMapping | None = None,
2515+
extra_body: Body | None = None,
2516+
timeout: TimeoutTypes | None = None,
2517+
) -> PdfRestFileBasedResponse:
2518+
"""Insert an image into a single page of a PDF."""
2519+
2520+
payload: dict[str, Any] = {
2521+
"files": file,
2522+
"image": image,
2523+
"x": x,
2524+
"y": y,
2525+
"page": page,
2526+
}
2527+
if output is not None:
2528+
payload["output"] = output
2529+
2530+
return self._post_file_operation(
2531+
endpoint="/pdf-with-added-image",
2532+
payload=payload,
2533+
payload_model=PdfAddImagePayload,
2534+
extra_query=extra_query,
2535+
extra_headers=extra_headers,
2536+
extra_body=extra_body,
2537+
timeout=timeout,
2538+
)
2539+
25032540
def split_pdf(
25042541
self,
25052542
file: PdfRestFile | Sequence[PdfRestFile],
@@ -3522,6 +3559,42 @@ async def apply_redactions(
35223559
timeout=timeout,
35233560
)
35243561

3562+
async def add_image_to_pdf(
3563+
self,
3564+
file: PdfRestFile | Sequence[PdfRestFile],
3565+
*,
3566+
image: PdfRestFile | Sequence[PdfRestFile],
3567+
x: int,
3568+
y: int,
3569+
page: int,
3570+
output: str | None = None,
3571+
extra_query: Query | None = None,
3572+
extra_headers: AnyMapping | None = None,
3573+
extra_body: Body | None = None,
3574+
timeout: TimeoutTypes | None = None,
3575+
) -> PdfRestFileBasedResponse:
3576+
"""Asynchronously insert an image into a PDF."""
3577+
3578+
payload: dict[str, Any] = {
3579+
"files": file,
3580+
"image": image,
3581+
"x": x,
3582+
"y": y,
3583+
"page": page,
3584+
}
3585+
if output is not None:
3586+
payload["output"] = output
3587+
3588+
return await self._post_file_operation(
3589+
endpoint="/pdf-with-added-image",
3590+
payload=payload,
3591+
payload_model=PdfAddImagePayload,
3592+
extra_query=extra_query,
3593+
extra_headers=extra_headers,
3594+
extra_body=extra_body,
3595+
timeout=timeout,
3596+
)
3597+
35253598
async def up(
35263599
self,
35273600
*,

src/pdfrest/models/_internal.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,53 @@ def _validate_profile_dependency(self) -> PdfCompressPayload:
10211021
return self
10221022

10231023

1024+
class PdfAddImagePayload(BaseModel):
1025+
"""Adapt caller options into a pdfRest-ready add-image request payload."""
1026+
1027+
files: Annotated[
1028+
list[PdfRestFile],
1029+
Field(
1030+
min_length=1,
1031+
max_length=1,
1032+
validation_alias=AliasChoices("file", "files"),
1033+
serialization_alias="id",
1034+
),
1035+
BeforeValidator(_ensure_list),
1036+
AfterValidator(
1037+
_allowed_mime_types("application/pdf", error_msg="Must be a PDF file")
1038+
),
1039+
PlainSerializer(_serialize_as_first_file_id),
1040+
]
1041+
image: Annotated[
1042+
list[PdfRestFile],
1043+
Field(
1044+
min_length=1,
1045+
max_length=1,
1046+
validation_alias=AliasChoices("image", "images", "image_file", "image_id"),
1047+
serialization_alias="image_id",
1048+
),
1049+
BeforeValidator(_ensure_list),
1050+
AfterValidator(
1051+
_allowed_mime_types(
1052+
"image/jpeg",
1053+
"image/png",
1054+
"image/tiff",
1055+
"image/gif",
1056+
error_msg="Image must be JPEG, PNG, TIFF, or GIF",
1057+
)
1058+
),
1059+
PlainSerializer(_serialize_as_first_file_id),
1060+
]
1061+
x: Annotated[int, Field(serialization_alias="x")]
1062+
y: Annotated[int, Field(serialization_alias="y")]
1063+
page: Annotated[int, Field(serialization_alias="page", ge=1)]
1064+
output: Annotated[
1065+
str | None,
1066+
Field(serialization_alias="output", min_length=1, default=None),
1067+
AfterValidator(_validate_output_prefix),
1068+
] = None
1069+
1070+
10241071
class PdfXfaToAcroformsPayload(BaseModel):
10251072
"""Adapt caller options into a pdfRest-ready XFA-to-AcroForms request payload."""
10261073

0 commit comments

Comments
 (0)