5.5 KiB
PDF Rename Sort
A Python web application built with Flask that automatically renames PDF files based on form field data extracted from within the PDFs. Designed for batch processing of fillable PDFs, extracting "Last Name" and "First Name" to create standardized filenames.
Features
- Web-Based Interface: Clean, responsive web UI for easy file uploads
- Batch Processing: Handle multiple PDFs or ZIP archives containing PDFs
- Form Field Extraction: Uses PyPDF2 to read PDF form fields
- Intelligent Renaming: Renames files to "LastName_FirstName.pdf" format
- Duplicate Handling: Automatically appends counters for duplicate names
- ZIP Support: Upload ZIP files and download processed results as ZIP
- Job Management: Background processing with job status tracking and history
- Authentication: Microsoft Entra ID and local admin login support
- Error Handling: Gracefully skips files that can't be processed
Prerequisites
- Python 3.6 or higher
- Virtual environment (recommended)
- Microsoft Entra ID (Azure AD) tenant with app registration
Microsoft Entra ID Setup
Detailed setup instructions: See ENTRA_SETUP.md for comprehensive step-by-step guide to configure Microsoft Entra ID authentication.
Quick Setup:
- Register application in Azure Portal (optional for admin login)
- Get Tenant ID, Client ID, and Client Secret (for Entra login)
- Set environment variables:
# For Entra ID login (optional) export TENANT_ID="your-tenant-id" export CLIENT_ID="your-client-id" export CLIENT_SECRET="your-client-secret" # For admin login export ADMIN_USERNAME="trbcit" export ADMIN_PASSWORD="atdrmbicn" # Required export SECRET_KEY="your-random-secret-key"
Authentication
The application supports two authentication methods:
- Microsoft Entra ID: OAuth2 authentication with Azure AD
- Admin Login: Local username/password authentication
Users can choose their preferred login method on the login page.
Installation
-
Clone the repository:
git clone https://gitlab.com/trbc-dev/pdf-rename-sort.git cd pdf-rename-sort -
Create and activate virtual environment:
python -m venv .venv # On Windows: & .venv/Scripts/Activate.ps1 # On macOS/Linux: source .venv/bin/activate -
Install dependencies:
pip install -r requirements.txt
Usage
-
Set environment variables (see Entra ID Setup above)
-
Run the application:
python pdf_renamer.py -
Access the web interface:
- Open
http://127.0.0.1:3000/ - Authenticate using Microsoft Entra ID or admin login
- Open
-
Upload files:
- Select individual PDF files or ZIP archives containing PDFs
- Click "Process PDFs" - this creates a background job
-
Monitor jobs:
- Click "View Jobs" to see processing status and history
- Jobs show as "Queued", "Processing", or "Completed"
-
Download results:
- When complete, download the ZIP file from the jobs page
- Review any skipped files listed for each job
How It Works
- File Upload: Accepts PDFs or ZIPs via web form
- Extraction: If ZIP, extracts PDFs to temporary directory
- Processing: For each PDF, reads form fields using PyPDF2
- Renaming: Extracts "Last Name" and "First Name" values
- Packaging: Creates a ZIP archive of renamed PDFs
- Download: Provides secure download link for results
Important Notes
- PDFs must contain fillable form fields named exactly "Last Name" and "First Name"
- Files without these fields or with extraction errors will be skipped
- All processing happens server-side with temporary file storage
- Maximum upload size is limited to 500MB
Production Deployment
For production deployment on Ubuntu with reverse proxy (multi-domain setup):
-
Run the automated installation script:
# On your Ubuntu server (can coexist with other apps) wget https://gitlab.com/trbc-dev/pdf-rename-sort/-/raw/main/ubuntu-install.sh chmod +x ubuntu-install.sh sudo ./ubuntu-install.sh -
Configure DNS:
- Point a subdomain (e.g.,
pdf-renamer.yourdomain.com) to your server's IP - This allows the app to run alongside your existing app on port 5000
- Point a subdomain (e.g.,
-
Configure environment variables: Edit
/etc/systemd/system/pdf-renamer.serviceand update:SECRET_KEY: Generate a secure random keyTENANT_ID,CLIENT_ID,CLIENT_SECRET: From Azure AD app registrationADMIN_USERNAMEandADMIN_PASSWORD: Set secure credentials
-
Update domain configuration: Edit
/etc/nginx/sites-available/pdf-renamerand replacepdf-renamer.your-domain.comwith your actual subdomain -
Enable SSL (recommended):
sudo apt install certbot python3-certbot-nginx sudo certbot --nginx -d your-subdomain.yourdomain.com -
Reload services:
sudo systemctl daemon-reload sudo systemctl restart pdf-renamer sudo systemctl reload nginx
The application will be accessible at your subdomain on ports 80 (HTTP) and 443 (HTTPS after SSL setup), running alongside your existing application.
Development
To contribute or modify:
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
For issues or questions, please open an issue on the GitLab repository.