import pytest from PIL import Image from paperpod.pdf.build import images_to_pdf def _write_png(path, color): Image.new("RGB", (200, 300), color).save(path) def test_images_to_pdf_single_page(tmp_path): img_path = tmp_path / "page.png" _write_png(img_path, (255, 255, 255)) out = tmp_path / "out.pdf" result = images_to_pdf([img_path], out, title="Test Doc") assert result == out assert out.exists() data = out.read_bytes() assert data.startswith(b"%PDF") def test_images_to_pdf_multi_page(tmp_path): paths = [] for i, color in enumerate([(255, 0, 0), (0, 255, 0), (0, 0, 255)]): p = tmp_path / f"page_{i}.png" _write_png(p, color) paths.append(p) out = tmp_path / "multi.pdf" images_to_pdf(paths, out) # img2pdf writes one xobject image per page; a 3-page PDF should be # meaningfully larger than a 1-page PDF of the same-size images. single = tmp_path / "single.pdf" images_to_pdf([paths[0]], single) assert out.stat().st_size > single.stat().st_size def test_images_to_pdf_requires_at_least_one_image(tmp_path): with pytest.raises(ValueError): images_to_pdf([], tmp_path / "empty.pdf") def test_images_to_pdf_creates_parent_dirs(tmp_path): img_path = tmp_path / "page.png" _write_png(img_path, (10, 20, 30)) out = tmp_path / "nested" / "dir" / "out.pdf" images_to_pdf([img_path], out) assert out.exists()