Skip to content

Commit

Permalink
Merge pull request #590 from chinapandaman/PPF-589
Browse files Browse the repository at this point in the history
PPF-589: support image field
  • Loading branch information
chinapandaman authored Apr 20, 2024
2 parents f1d523d + c37563f commit 4c926d7
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 8 deletions.
4 changes: 4 additions & 0 deletions PyPDFForm/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
DEPRECATION_NOTICE = "{} will be deprecated soon. Use {} instead."

Annots = "/Annots"
A = "/A"
JS = "/JS"
T = "/T"
Rect = "/Rect"
Subtype = "/Subtype"
Expand Down Expand Up @@ -64,6 +66,8 @@

NEW_LINE_SYMBOL = "\n"

IMAGE_FIELD_IDENTIFIER = "event.target.buttonImportIcon();"

DEFAULT_CHECKBOX_STYLE = "\u2713"
DEFAULT_RADIO_STYLE = "\u25CF"
BUTTON_STYLES = {
Expand Down
4 changes: 2 additions & 2 deletions PyPDFForm/coordinate.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ def get_draw_checkbox_radio_coordinates(
)


def get_draw_sig_coordinates_resolutions(
def get_draw_image_coordinates_resolutions(
widget: dict,
) -> Tuple[float, float, float, float]:
"""
Returns coordinates and resolutions to draw signature at given a PDF form signature widget.
Returns coordinates and resolutions to draw image at given a PDF form signature/image widget.
"""

x = float(widget[Rect][0])
Expand Down
7 changes: 4 additions & 3 deletions PyPDFForm/filler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from .constants import WIDGET_TYPES, Annots
from .coordinate import (get_draw_checkbox_radio_coordinates,
get_draw_sig_coordinates_resolutions,
get_draw_image_coordinates_resolutions,
get_draw_text_coordinates,
get_text_line_x_coordinates)
from .font import checkbox_radio_font_size
Expand All @@ -19,6 +19,7 @@
from .middleware.radio import Radio
from .middleware.signature import Signature
from .middleware.text import Text
from .middleware.image import Image
from .patterns import (simple_flatten_generic, simple_flatten_radio,
simple_update_checkbox_value,
simple_update_dropdown_value, simple_update_radio_value,
Expand Down Expand Up @@ -69,12 +70,12 @@ def fill(
radio_button_tracker[key] += 1
if widgets[key].value == radio_button_tracker[key] - 1:
text_needs_to_be_drawn = True
elif isinstance(widgets[key], Signature):
elif isinstance(widgets[key], (Signature, Image)):
stream = widgets[key].stream
if stream is not None:
any_image_to_draw = True
stream = any_image_to_jpg(stream)
x, y, width, height = get_draw_sig_coordinates_resolutions(_widget)
x, y, width, height = get_draw_image_coordinates_resolutions(_widget)
images_to_draw[page].append(
[
stream,
Expand Down
8 changes: 8 additions & 0 deletions PyPDFForm/middleware/image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-
"""Contains image field middleware."""

from .signature import Signature


class Image(Signature):
"""A class to represent an image field widget."""
10 changes: 8 additions & 2 deletions PyPDFForm/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@
from pypdf.generic import (DictionaryObject, NameObject, NumberObject,
TextStringObject)

from .constants import (AP, AS, CA, DA, FT, MK, READ_ONLY, Btn, Ch, D, Ff, Off,
Opt, Parent, Q, Sig, Subtype, T, Tx, V, Widget)
from .constants import (A, AP, AS, CA, DA, FT, MK, READ_ONLY, Btn, Ch, D, Ff, Off,
Opt, Parent, Q, Sig, Subtype, T, Tx, V, Widget, JS,
IMAGE_FIELD_IDENTIFIER)
from .middleware.checkbox import Checkbox
from .middleware.dropdown import Dropdown
from .middleware.radio import Radio
from .middleware.signature import Signature
from .middleware.image import Image
from .middleware.text import Text

WIDGET_TYPE_PATTERNS = [
(
({A: {JS: IMAGE_FIELD_IDENTIFIER}},),
Image,
),
(
({FT: Sig},),
Signature,
Expand Down
24 changes: 24 additions & 0 deletions docs/fill.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,27 @@ with open("output.pdf", "wb+") as output:

**NOTE:** As described [here](install.md/#create-a-pdf-wrapper), the value of the signature in your dictionary can be
a file path shown above, but also an open file object and a file stream that's in `bytes`.

## Fill image widgets (beta)

**NOTE:** This is a beta feature, meaning it still needs to be tested against more PDF forms and may not work for
some of them.

An image field widget can be filled similarly to a signature field, by providing a value of file path, file object, or
file stream.

Consider [this PDF](https://github.com/chinapandaman/PyPDFForm/raw/master/pdf_samples/sample_template_with_image_field.pdf)
and [this image](https://github.com/chinapandaman/PyPDFForm/raw/master/image_samples/sample_image.jpg):

```python
from PyPDFForm import PdfWrapper

filled = PdfWrapper("sample_template_with_image_field.pdf").fill(
{
"image_1": "sample_image.jpg"
},
)

with open("output.pdf", "wb+") as output:
output.write(filled.read())
```
2 changes: 1 addition & 1 deletion docs/simple_fill.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
The `FormWrapper` class allows you to fill a PDF form in place as if you were filling it manually.

Similar to the `PdfWrapper` class, the `FormWrapper` also supports widgets including text fields, checkboxes, radio
buttons, dropdowns, and paragraphs. However, it does NOT support signature widgets.
buttons, dropdowns, and paragraphs. However, it does NOT support signature or image widgets.

Consider [this PDF](https://github.com/chinapandaman/PyPDFForm/raw/master/pdf_samples/dropdown/sample_template_with_dropdown.pdf):

Expand Down
Binary file added pdf_samples/sample_filled_image.pdf
Binary file not shown.
Binary file added pdf_samples/sample_template_with_image_field.pdf
Binary file not shown.
Binary file added pdf_samples/scenario/issues/560.pdf
Binary file not shown.
Binary file added pdf_samples/scenario/issues/560_expected.pdf
Binary file not shown.
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ def sample_template_with_dropdown(pdf_samples):
return f.read()


@pytest.fixture
def sample_template_with_image_field(pdf_samples):
with open(os.path.join(pdf_samples, "sample_template_with_image_field.pdf"), "rb+") as f:
return f.read()


@pytest.fixture
def dropdown_alignment(pdf_samples):
with open(
Expand Down
16 changes: 16 additions & 0 deletions tests/scenario/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,19 @@ def test_encrypted_edit_pdf_form(issue_pdf_directory, request):
expected = f.read()
assert len(obj.read()) == len(expected)
assert obj.read() == expected


def test_fill_image(issue_pdf_directory, image_samples, request):
obj = PdfWrapper(os.path.join(issue_pdf_directory, "560.pdf"))
obj = obj.fill(
{
"ImageSign": os.path.join(image_samples, "sample_image.jpg")
}
)
expected_path = os.path.join(issue_pdf_directory, "560_expected.pdf")
request.config.results["expected_path"] = expected_path
request.config.results["stream"] = obj.read()
with open(expected_path, "rb+") as f:
expected = f.read()
assert len(obj.read()) == len(expected)
assert obj.read() == expected
18 changes: 18 additions & 0 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,3 +610,21 @@ def test_radio_change_size_and_button_style(

assert len(obj.read()) == len(expected)
assert obj.stream == expected


def test_fill_image(sample_template_with_image_field, image_samples, pdf_samples, request):
expected_path = os.path.join(pdf_samples, "sample_filled_image.pdf")
with open(expected_path, "rb+") as f:
obj = PdfWrapper(sample_template_with_image_field).fill(
{
"image_1": os.path.join(image_samples, "sample_image.jpg")
},
)

request.config.results["expected_path"] = expected_path
request.config.results["stream"] = obj.read()

expected = f.read()

assert len(obj.read()) == len(expected)
assert obj.stream == expected

0 comments on commit 4c926d7

Please sign in to comment.