@@ -34,16 +34,24 @@ def assert_is_pdf(file_path_or_bytes: str | bytes) -> None:
3434
3535def create_test_image (tmp_path : Path , filename : str = "watermark.png" ) -> str :
3636 """Create a simple test PNG image."""
37- # PNG header for a 1x1 transparent pixel
38- png_data = (
39- b"\x89 PNG\r \n \x1a \n \x00 \x00 \x00 \r IHDR\x00 \x00 \x00 \x01 \x00 \x00 \x00 \x01 "
40- b"\x08 \x06 \x00 \x00 \x00 \x1f \x15 \xc4 \x89 \x00 \x00 \x00 \r IDATx\x9c c\xf8 \x0f "
41- b"\x00 \x00 \x01 \x01 \x00 \x00 \xcb \xd6 \x8e \n \x00 \x00 \x00 \x00 IEND\xae B`\x82 "
42- )
43-
44- image_path = tmp_path / filename
45- image_path .write_bytes (png_data )
46- return str (image_path )
37+ try :
38+ # Try to use PIL to create a proper image
39+ from PIL import Image
40+ img = Image .new ('RGB' , (100 , 100 ), color = 'red' )
41+ image_path = tmp_path / filename
42+ img .save (str (image_path ))
43+ return str (image_path )
44+ except ImportError :
45+ # Fallback to a simple but valid PNG if PIL is not available
46+ # This is a 2x2 red PNG image
47+ png_data = (
48+ b'\x89 PNG\r \n \x1a \n \x00 \x00 \x00 \r IHDR\x00 \x00 \x00 \x02 \x00 \x00 \x00 \x02 '
49+ b'\x08 \x02 \x00 \x00 \x00 \xfd \xd4 \x9a s\x00 \x00 \x00 \x0c IDATx\x9c c\xf8 \xcf '
50+ b'\xc0 \x00 \x00 \x03 \x01 \x01 \x00 \x18 \xdd \x8d \xb4 \x00 \x00 \x00 \x00 IEND\xae B`\x82 '
51+ )
52+ image_path = tmp_path / filename
53+ image_path .write_bytes (png_data )
54+ return str (image_path )
4755
4856
4957@pytest .mark .skipif (not API_KEY , reason = "No API key configured in integration_config.py" )
@@ -82,12 +90,21 @@ def test_watermark_pdf_with_image_file_path(self, client, sample_pdf_path, tmp_p
8290
8391 def test_watermark_pdf_with_image_bytes (self , client , sample_pdf_path ):
8492 """Test watermark_pdf with image as bytes."""
85- # PNG header for a 1x1 transparent pixel
86- png_bytes = (
87- b"\x89 PNG\r \n \x1a \n \x00 \x00 \x00 \r IHDR\x00 \x00 \x00 \x01 \x00 \x00 \x00 \x01 "
88- b"\x08 \x06 \x00 \x00 \x00 \x1f \x15 \xc4 \x89 \x00 \x00 \x00 \r IDATx\x9c c\xf8 \x0f "
89- b"\x00 \x00 \x01 \x01 \x00 \x00 \xcb \xd6 \x8e \n \x00 \x00 \x00 \x00 IEND\xae B`\x82 "
90- )
93+ # Create a proper PNG image as bytes
94+ try :
95+ from PIL import Image
96+ import io
97+ img = Image .new ('RGB' , (100 , 100 ), color = 'blue' )
98+ img_buffer = io .BytesIO ()
99+ img .save (img_buffer , format = 'PNG' )
100+ png_bytes = img_buffer .getvalue ()
101+ except ImportError :
102+ # Fallback to a 2x2 blue PNG if PIL is not available
103+ png_bytes = (
104+ b'\x89 PNG\r \n \x1a \n \x00 \x00 \x00 \r IHDR\x00 \x00 \x00 \x02 \x00 \x00 \x00 \x02 '
105+ b'\x08 \x02 \x00 \x00 \x00 \xfd \xd4 \x9a s\x00 \x00 \x00 \x0c IDATx\x9c c\x98 \x00 '
106+ b'\x00 \x00 \x05 \x00 \x01 \x85 \xb7 \xb2 \xf3 \x00 \x00 \x00 \x00 IEND\xae B`\x82 '
107+ )
91108
92109 result = client .watermark_pdf (
93110 sample_pdf_path ,
0 commit comments