Skip to content

Commit c05a660

Browse files
Fix 2-up printing (#78)
1 parent 788e8b4 commit c05a660

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

pdfly/up2.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ def main(pdf: Path, output: Path) -> None:
1818
for i in range(0, len(reader.pages) - 1, 2):
1919
lhs = reader.pages[i]
2020
rhs = reader.pages[i + 1]
21-
lhs.merge_translated_page(rhs, float(lhs.mediabox.right), 0, True)
21+
lhs.merge_translated_page(
22+
rhs, tx=float(lhs.mediabox.width), ty=0, expand=True
23+
)
2224
writer.add_page(lhs)
2325
print(str(i) + " ")
2426
sys.stdout.flush()

resources/input8.pdf

14.3 KB
Binary file not shown.

tests/test_up2.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import os
2+
import pytest
3+
from pypdf import PdfReader
4+
5+
from .conftest import RESOURCES_ROOT, chdir, run_cli
6+
7+
8+
def test_up2_fewer_args(capsys, tmp_path):
9+
with chdir(tmp_path):
10+
exit_code = run_cli(["2-up", str(RESOURCES_ROOT / "box.pdf")])
11+
assert exit_code == 2
12+
captured = capsys.readouterr()
13+
assert "Missing argument" in captured.err
14+
15+
16+
def test_up2_extra_args(capsys, tmp_path):
17+
with chdir(tmp_path):
18+
exit_code = run_cli(
19+
[
20+
"2-up",
21+
str(RESOURCES_ROOT / "box.pdf"),
22+
"./out.pdf",
23+
"./out2.pdf",
24+
]
25+
)
26+
27+
assert exit_code == 2
28+
captured = capsys.readouterr()
29+
assert "unexpected extra argument" in captured.err
30+
31+
with chdir(tmp_path):
32+
assert not os.path.exists("out.pdf"), f"'out.pdf' should not exist."
33+
assert not os.path.exists("out2.pdf"), f"'out2.pdf' should not exist."
34+
35+
36+
def test_up2_8page_file(capsys, tmp_path):
37+
# Act
38+
with chdir(tmp_path):
39+
exit_code = run_cli(
40+
[
41+
"2-up",
42+
str(RESOURCES_ROOT / "input8.pdf"),
43+
"./out.pdf",
44+
]
45+
)
46+
captured = capsys.readouterr()
47+
48+
# Assert
49+
assert exit_code == 0, captured
50+
assert not captured.err
51+
in_reader = PdfReader(RESOURCES_ROOT / "input8.pdf")
52+
out_reader = PdfReader(tmp_path / "./out.pdf")
53+
54+
assert len(in_reader.pages) == 8
55+
assert len(out_reader.pages) == 4
56+
57+
in_width = in_reader.pages[0].mediabox.width
58+
in_height = in_reader.pages[0].mediabox.height
59+
out_width = out_reader.pages[0].mediabox.width
60+
out_height = out_reader.pages[0].mediabox.height
61+
62+
assert out_width == 2 * in_width # PR #78
63+
assert out_height == out_height

0 commit comments

Comments
 (0)