diff --git a/pdfly/up2.py b/pdfly/up2.py index 01b1c2a..ab902b0 100644 --- a/pdfly/up2.py +++ b/pdfly/up2.py @@ -18,7 +18,9 @@ def main(pdf: Path, output: Path) -> None: for i in range(0, len(reader.pages) - 1, 2): lhs = reader.pages[i] rhs = reader.pages[i + 1] - lhs.merge_translated_page(rhs, float(lhs.mediabox.right), 0, True) + lhs.merge_translated_page( + rhs, tx=float(lhs.mediabox.width), ty=0, expand=True + ) writer.add_page(lhs) print(str(i) + " ") sys.stdout.flush() diff --git a/resources/input8.pdf b/resources/input8.pdf new file mode 100644 index 0000000..f11b56d Binary files /dev/null and b/resources/input8.pdf differ diff --git a/tests/test_up2.py b/tests/test_up2.py new file mode 100644 index 0000000..c29467a --- /dev/null +++ b/tests/test_up2.py @@ -0,0 +1,63 @@ +import os +import pytest +from pypdf import PdfReader + +from .conftest import RESOURCES_ROOT, chdir, run_cli + + +def test_up2_fewer_args(capsys, tmp_path): + with chdir(tmp_path): + exit_code = run_cli(["2-up", str(RESOURCES_ROOT / "box.pdf")]) + assert exit_code == 2 + captured = capsys.readouterr() + assert "Missing argument" in captured.err + + +def test_up2_extra_args(capsys, tmp_path): + with chdir(tmp_path): + exit_code = run_cli( + [ + "2-up", + str(RESOURCES_ROOT / "box.pdf"), + "./out.pdf", + "./out2.pdf", + ] + ) + + assert exit_code == 2 + captured = capsys.readouterr() + assert "unexpected extra argument" in captured.err + + with chdir(tmp_path): + assert not os.path.exists("out.pdf"), f"'out.pdf' should not exist." + assert not os.path.exists("out2.pdf"), f"'out2.pdf' should not exist." + + +def test_up2_8page_file(capsys, tmp_path): + # Act + with chdir(tmp_path): + exit_code = run_cli( + [ + "2-up", + str(RESOURCES_ROOT / "input8.pdf"), + "./out.pdf", + ] + ) + captured = capsys.readouterr() + + # Assert + assert exit_code == 0, captured + assert not captured.err + in_reader = PdfReader(RESOURCES_ROOT / "input8.pdf") + out_reader = PdfReader(tmp_path / "./out.pdf") + + assert len(in_reader.pages) == 8 + assert len(out_reader.pages) == 4 + + in_width = in_reader.pages[0].mediabox.width + in_height = in_reader.pages[0].mediabox.height + out_width = out_reader.pages[0].mediabox.width + out_height = out_reader.pages[0].mediabox.height + + assert out_width == 2 * in_width # PR #78 + assert out_height == out_height