Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 2-up printing #78

Merged
merged 5 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pdfly/up2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Binary file added resources/input8.pdf
Binary file not shown.
63 changes: 63 additions & 0 deletions tests/test_up2.py
Original file line number Diff line number Diff line change
@@ -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
Loading