py-pdf / pdfly

CLI tool to extract (meta)data from PDF and manipulate PDF files
BSD 3-Clause "New" or "Revised" License
109 stars 18 forks source link

Add Flexible PDF Layout Options with `--layout` Parameter #67

Open mulla028 opened 3 weeks ago

mulla028 commented 3 weeks ago

Closes #64

Overview

This PR introduces an optional --layout parameter to the up2 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

  1. up2.py:

    • Added a new up2_main function to manage both default behavior and specified layouts.
    • Updated page dimension access to mediabox for compatibility with recent versions of pypdf.
    • Retained the original main function for backward compatibility, though future calls should use up2_main.
  2. cli.py:

    • Modified the up2 command to call up2_main with an optional --layout parameter for layout configurations.
    • Retained all original commands and functionality to ensure no changes to existing CLI behavior.

Testing

To test each layout configuration, run the following commands:


# Default behavior (no layout specified)
python cli.py up2 path/to/input.pdf path/to/output_default.pdf

# 2x2 layout
python cli.py up2 path/to/input.pdf path/to/output_2x2.pdf --layout 2x2

# 3x3 layout
python cli.py up2 path/to/input.pdf path/to/output_3x3.pdf --layout 3x3

# 1x2 layout
python cli.py up2 path/to/input.pdf path/to/output_1x2.pdf --layout 1x2

# 2x1 layout
python cli.py up2 path/to/input.pdf path/to/output_2x1.pdf --layout 2x1
mulla028 commented 3 weeks ago

@Lucas-C I have written yesterday a test for --layout feature, but wasn't sure if you need it. According to your recent feedback in Issue #63, I found that you would like to see some tests. Eventually, I just pushed it 😆