A collection of Python tools for PDF layout fixing and content adjustment. This package provides utilities to fix PDF page orientation and adjust content positioning within PDF documents.
- PDF Layout Fixing: Convert portrait pages to landscape orientation while preserving content direction
- Content Position Adjustment: Move PDF content to correct positions within pages
- Batch Processing: Handle multiple pages efficiently
- Debug Information: Optional verbose output for troubleshooting
# Install uv if you haven't already
pip install uv
# Clone the repository
git clone https://github.com/CrueChan/pdf-tools.git
cd pdf-tools
# Install the package and dependencies
uv pip install -e .pip install git+https://github.com/CrueChan/pdf-tools.gitAfter installation, you can use the command-line tools:
# Fix PDF layout (convert to landscape)
pdf-fix-layout input.pdf output.pdf
# Adjust content position
pdf-adjust-content input.pdf output.pdffrom pdf_tools.fix_pdf_layout import fix_pdf_rotation
from pdf_tools.adjust_pdf import adjust_content_position
# Fix PDF layout
success = fix_pdf_rotation("input.pdf", "output_rotated.pdf", debug=True)
# Adjust content position
adjust_content_position("input.pdf", "output_adjusted.pdf")import os
from pdf_tools import fix_pdf_rotation, adjust_content_position
def process_pdf_workflow(input_file):
"""Complete PDF processing workflow"""
# Step 1: Fix layout orientation
temp_file = "temp_rotated.pdf"
success = fix_pdf_rotation(input_file, temp_file, debug=True)
if success:
# Step 2: Adjust content position
final_output = "processed_output.pdf"
adjust_content_position(temp_file, final_output)
# Clean up temporary file
os.remove(temp_file)
print(f"Processing complete! Output saved as: {final_output}")
else:
print("Failed to process PDF")
# Process a PDF file
if __name__ == "__main__":
process_pdf_workflow("your_input.pdf")The fix_pdf_rotation function:
- Reads the input PDF file
- Analyzes each page's dimensions
- Swaps width and height for portrait pages to make them landscape
- Adjusts MediaBox and CropBox accordingly
- Preserves the original content orientation
The adjust_content_position function:
- Applies transformation matrix to move content
- Shifts content vertically by 40% of page height
- Maintains original page dimensions
- Processes all pages uniformly
- Dependencies: PyPDF2 3.0.0+
- Python Support: 3.8+
- Page Format: Optimized for A4 size (841.89 x 595.27 points)
- Coordinate System: Uses PDF coordinate system (bottom-left origin)
# Clone repository
git clone https://github.com/CrueChan/pdf-tools.git
cd pdf-tools
# Install development dependencies
uv pip install -e ".[dev]"
# Run tests
pytest
# Format code
black src/ tests/
# Lint code
flake8 src/ tests/
# Type checking
mypy src/pdf-tools/
├── src/pdf_tools/ # Main package source
│ ├── __init__.py # Package initialization
│ ├── fix_pdf_layout.py # Layout fixing functionality
│ ├── adjust_pdf.py # Content adjustment functionality
│ └── cli.py # Command-line interface
├── tests/ # Test files
├── examples/ # Usage examples
├── README.md # This file
├── pyproject.toml # Project configuration
└── LICENSE # MIT License
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Add tests for new functionality
- Run the test suite (
pytest) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- "No module named 'PyPDF2'": Install dependencies with
uv pip install PyPDF2 - Permission errors: Ensure you have write permissions in the output directory
- Memory issues with large PDFs: Process files in smaller batches
Enable debug mode for detailed processing information:
fix_pdf_rotation("input.pdf", "output.pdf", debug=True)This will show:
- Original page dimensions
- MediaBox and CropBox information
- Processing progress
- Error details if issues occur
- Initial release
- PDF layout fixing functionality
- Content position adjustment
- Command-line interface
- Python API
If you encounter issues or have questions:
- Check the Issues page
- Create a new issue with detailed information
- Include sample PDF files if possible (remove sensitive content)
Made with ❤️ by CrueChan