diff --git a/test/unit_tests/conftest.py b/test/unit_tests/conftest.py index 314b186..af380d4 100644 --- a/test/unit_tests/conftest.py +++ b/test/unit_tests/conftest.py @@ -1,3 +1,4 @@ +import io import random import tempfile from operator import itemgetter @@ -162,16 +163,31 @@ def metadata(images): @pytest.fixture def pdf(images, metadata): + + def add_image(image, metadata): + + def fewer_pages_then_required(): + return metadata["page_idx"] > pdf.page - 1 + + def add_image_to_last_page(): + x, y, w, h = itemgetter("x1", "y1", "width", "height")(metadata) + + with tempfile.NamedTemporaryFile(suffix=".png") as temp_image: + image.save(temp_image.name) + pdf.image(temp_image.name, x=x, y=y, w=w, h=h) + + while fewer_pages_then_required(): + pdf.add_page() + + add_image_to_last_page() + + def pdf_object_to_actual_pdf(): + return pdf.output(dest="S").encode("latin1") + pdf = fpdf.FPDF(unit="pt") pdf.add_page() + for image, metadata in zip(images, metadata): - while metadata["page_idx"] > pdf.page - 1: - pdf.add_page() - x, y, w, h = itemgetter("x1", "y1", "width", "height")(metadata) - with tempfile.NamedTemporaryFile(suffix=".png") as temp_image: - image.save(temp_image.name) - pdf.image(temp_image.name, x=x, y=y, w=w, h=h) - with tempfile.NamedTemporaryFile(suffix=".pdf") as temp_pdf: - pdf.output(temp_pdf.name) - with open(temp_pdf.name, "rb") as open_temp_pdf: - yield open_temp_pdf.read() + add_image(image, metadata) + + return pdf_object_to_actual_pdf()