Skip to content

Commit

Permalink
Merge pull request #193 from chinapandaman/PPF-190
Browse files Browse the repository at this point in the history
PPF-190: any image to jpg
  • Loading branch information
chinapandaman authored Apr 5, 2021
2 parents 9620f9c + 460276d commit 194ce41
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 1 deletion.
2 changes: 1 addition & 1 deletion PyPDFForm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

PyPDFForm = Wrapper

__version__ = "0.3.2"
__version__ = "0.3.3"
23 changes: 23 additions & 0 deletions PyPDFForm/core/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,26 @@ def rotate_image(image_stream: bytes, rotation: Union[float, int]) -> bytes:
rotated_buff.close()

return result

@staticmethod
def any_image_to_jpg(image_stream: bytes) -> bytes:
"""Converts an image of any type to jpg."""

buff = BytesIO()
buff.write(image_stream)
buff.seek(0)

image = Img.open(buff)

if image.format == "JPEG":
buff.close()
return image_stream

rgb_image = image.convert("RGB")
with BytesIO() as f:
rgb_image.save(f, format="JPEG")
f.seek(0)
result = f.read()

buff.close()
return result
5 changes: 5 additions & 0 deletions PyPDFForm/middleware/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ def _fill(
self.elements[key].validate_constants()
self.elements[key].validate_value()
self.elements[key].validate_text_attributes()
if self.elements[key].type == ElementType.image:
value = ImageCore().any_image_to_jpg(value)
self.elements[key].value = value

self.stream = FillerCore().fill(self.stream, self.elements)

Expand Down Expand Up @@ -151,6 +154,7 @@ def _simple_fill(
if isinstance(value, bytes):
if not ImageCore().is_image(value):
raise InvalidFormDataError
data[key] = ImageCore().any_image_to_jpg(value)

if not isinstance(editable, bool):
raise InvalidEditableParameterError
Expand Down Expand Up @@ -243,6 +247,7 @@ def draw_image(
if not ImageCore().is_image(image):
raise InvalidImageError

image = ImageCore().any_image_to_jpg(image)
image = ImageCore().rotate_image(image, rotation)

if not isinstance(page_number, int):
Expand Down
Binary file added image_samples/after_converted.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image_samples/before_converted.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pdf_samples/v2/sample_filled_png_images.pdf
Binary file not shown.
Binary file not shown.
Binary file added pdf_samples/v2/sample_pdf_with_png_image.pdf
Binary file not shown.
20 changes: 20 additions & 0 deletions tests/functional/test_draw_on_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,23 @@ def test_draw_image_on_one_page_f_obj_param(
assert obj.stream == expected
else:
assert obj.stream[:32767] == expected[:32767]


def test_draw_png_image_on_one_page(template_stream, image_samples, pdf_samples):
with open(os.path.join(pdf_samples, "sample_pdf_with_png_image.pdf"), "rb+") as f:
obj = PyPDFForm(template_stream).draw_image(
os.path.join(image_samples, "before_converted.png"),
2,
100,
100,
400,
225,
)

expected = f.read()

if os.name == "nt":
assert len(obj.stream) == len(expected)
assert obj.stream == expected
else:
assert obj.stream[:32767] == expected[:32767]
45 changes: 45 additions & 0 deletions tests/functional/test_fill.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,51 @@ def test_fill_images_f_obj_params_explicitly_setting_elements(
assert obj.stream[:32767] == expected[:32767]


def test_fill_png_images(pdf_samples, image_samples):
with open(os.path.join(pdf_samples, "sample_filled_png_images.pdf"), "rb+") as f:
expected = f.read()

obj = PyPDFForm(
os.path.join(pdf_samples, "sample_template_with_image_field.pdf"),
simple_mode=False,
).fill(
{
"image_1": os.path.join(image_samples, "before_converted.png"),
"image_2": os.path.join(image_samples, "before_converted.png"),
"image_3": os.path.join(image_samples, "before_converted.png"),
}
)

if os.name == "nt":
assert len(obj.stream) == len(expected)
assert obj.stream == expected
else:
assert obj.stream[:16383] == expected[:16383]


def test_simple_fill_png_images(pdf_samples, image_samples):
with open(
os.path.join(pdf_samples, "sample_filled_png_images_simple_mode.pdf"), "rb+"
) as f:
expected = f.read()

obj = PyPDFForm(
os.path.join(pdf_samples, "sample_template_with_image_field.pdf"),
).fill(
{
"image_1": os.path.join(image_samples, "before_converted.png"),
"image_2": os.path.join(image_samples, "before_converted.png"),
"image_3": os.path.join(image_samples, "before_converted.png"),
}
)

if os.name == "nt":
assert len(obj.stream) == len(expected)
assert obj.stream == expected
else:
assert obj.stream[:16383] == expected[:16383]


def test_simple_fill_radiobutton(pdf_samples, template_with_radiobutton_stream):
with open(
os.path.join(pdf_samples, "sample_filled_radiobutton_simple.pdf"), "rb+"
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ def image_stream(image_samples):
return f.read()


@pytest.fixture
def before_converted(image_samples):
with open(os.path.join(image_samples, "before_converted.png"), "rb+") as f:
return f.read()


@pytest.fixture
def after_converted(image_samples):
with open(os.path.join(image_samples, "after_converted.jpg"), "rb+") as f:
return f.read()


def test_is_image(image_stream):
assert not ImageCore().is_image(b"bad_stream")
assert ImageCore().is_image(image_stream)
Expand All @@ -47,3 +59,13 @@ def test_rotate_image(image_stream):

buff.close()
rotated_buff.close()


def test_any_image_to_jpg(before_converted, after_converted):
if os.name == "nt":
assert ImageCore().any_image_to_jpg(before_converted) == after_converted
else:
assert (
ImageCore().any_image_to_jpg(before_converted)[:511]
== after_converted[:511]
)

0 comments on commit 194ce41

Please sign in to comment.