-
Notifications
You must be signed in to change notification settings - Fork 18
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
ENH: Add --layout
parameter to up2
#67
base: main
Are you sure you want to change the base?
Conversation
…or flexible page layouts.
… 1x2, 2x1) without impacting default behavior.
…gurations, ensuring basic functionality and output verification.
--layout
Parameter--layout
Parameter
# File paths for the input and output PDFs | ||
input_pdf = Path("test_input.pdf") | ||
output_pdf = Path("test_output.pdf") | ||
|
||
# Create the test PDF | ||
create_test_pdf(input_pdf) | ||
|
||
# List of layouts to test | ||
layouts = ["2x2", "3x3", "1x2", "2x1"] | ||
for layout in layouts: | ||
print(f"\nTesting layout: {layout}") | ||
up2_main(input_pdf, output_pdf, layout=layout) | ||
|
||
# Read the output PDF and print the number of pages | ||
reader = PdfReader(str(output_pdf)) | ||
print(f"Output PDF for layout {layout} has {len(reader.pages)} pages.") | ||
|
||
# Clean up | ||
if output_pdf.exists(): | ||
os.remove(output_pdf) | ||
if input_pdf.exists(): | ||
os.remove(input_pdf) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use pytest as a test suite to automatically run the tests and check if they succeeded.
Pytest looks for functions starting with "test_"
# File paths for the input and output PDFs | |
input_pdf = Path("test_input.pdf") | |
output_pdf = Path("test_output.pdf") | |
# Create the test PDF | |
create_test_pdf(input_pdf) | |
# List of layouts to test | |
layouts = ["2x2", "3x3", "1x2", "2x1"] | |
for layout in layouts: | |
print(f"\nTesting layout: {layout}") | |
up2_main(input_pdf, output_pdf, layout=layout) | |
# Read the output PDF and print the number of pages | |
reader = PdfReader(str(output_pdf)) | |
print(f"Output PDF for layout {layout} has {len(reader.pages)} pages.") | |
# Clean up | |
if output_pdf.exists(): | |
os.remove(output_pdf) | |
if input_pdf.exists(): | |
os.remove(input_pdf) | |
def test_layout_option(): | |
# File paths for the input and output PDFs | |
input_pdf = Path("test_input.pdf") | |
output_pdf = Path("test_output.pdf") | |
# Create the test PDF | |
create_test_pdf(input_pdf) | |
# List of layouts to test | |
layouts = ["2x2", "3x3", "1x2", "2x1"] | |
for layout in layouts: | |
print(f"\nTesting layout: {layout}") | |
up2_main(input_pdf, output_pdf, layout=layout) | |
# Read the output PDF and print the number of pages | |
reader = PdfReader(str(output_pdf)) | |
print(f"Output PDF for layout {layout} has {len(reader.pages)} pages.") | |
# Clean up | |
if output_pdf.exists(): | |
os.remove(output_pdf) | |
if input_pdf.exists(): | |
os.remove(input_pdf) |
writer.add_page(page) | ||
|
||
with open(output, "wb") as f_out: | ||
writer.write(f_out) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
writer.write(f_out) | |
writer.write(f_out) | |
# Define layout configurations (columns, rows) for each grid type | ||
layout_options = { | ||
"2x2": (2, 2), | ||
"3x3": (3, 3), | ||
"1x2": (1, 2), | ||
"2x1": (2, 1) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason to restrict the layout options? Wouldn't any XxY with X and Y being positive integers work?
--layout
Parameter--layout
parameter to up2
Thank you for your contribution! |
Could you please run |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could add this test to test_up2.py
@mulla028 Sorry that it took me a month to review 🙈 If you want, I would clean-up the remaining parts. Just let me know if you want to finish it yourself or not :-) |
Closes #64
Overview
This PR introduces an optional
--layout
parameter to theup2
command, allowing users to arrange PDF pages in configurable grid layouts. Supported layouts include:If
--layout
is not specified, the command defaults to the original behavior, ensuring full backward compatibility.Changes
up2.py
:up2_main
function to manage both default behavior and specified layouts.mediabox
for compatibility with recent versions ofpypdf
.main
function for backward compatibility, though future calls should useup2_main
.cli.py
:up2
command to callup2_main
with an optional--layout
parameter for layout configurations.Testing
To test each layout configuration, run the following commands: