Skip to content

Commit

Permalink
Add merge_papers.sh script for merging PDF papers and update README
Browse files Browse the repository at this point in the history
- Added  script to automate the merging of O and A level papers into single PDFs for each paper type.
- The script checks for the installation of  and installs it if necessary, based on the user's Linux distribution.
- Updated README to include instructions for using the new  script.
  • Loading branch information
fam007e committed Dec 26, 2024
1 parent 57a65a1 commit 41a1c94
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ python o_and_a_lv_qp_sdl.py

Please be respectful of the Xtremepapers website and avoid overloading their servers with too many requests in a short time.

## Merging Papers

After downloading the papers, you can run the `pdfmerger.sh` script to merge the papers by type using `poppler` utility package `pdfunite`:

```bash
bash pdfmerger.sh
```

The script will:

- Find and merge the papers (question papers, mark schemes, etc.) for each paper type (e.g., `qp_1`, `qp_2`, etc.) into single PDFs.
- Save the merged PDFs in the `merged` directory.
- Organize the downloaded papers into folders by examination level, subject, and paper type (`ms` for mark schemes, `qp` for question papers, and `misc` for other types).

## License

This project is open source and available under the [LICENSE](LICENSE).
Expand Down
72 changes: 72 additions & 0 deletions pdfmerger.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/bin/bash

# Function to install poppler-utils based on the Linux distribution
install_poppler() {
echo "Attempting to install poppler-utils..."
if [ -f /etc/os-release ]; then
source /etc/os-release
case $ID in
ubuntu|debian)
sudo apt update && sudo apt install -y poppler-utils
;;
fedora)
sudo dnf install -y poppler-utils
;;
arch|manjaro)
sudo pacman -Syu poppler
;;
opensuse|suse)
sudo zypper install -y poppler-tools
;;
*)
echo "Unsupported Linux distribution. Please install poppler-utils manually."
exit 1
;;
esac
else
echo "Could not detect Linux distribution. Please install poppler-utils manually."
exit 1
fi
}

# Ensure pdfunite is installed
if ! command -v pdfunite &> /dev/null; then
echo "Error: pdfunite is not installed."
install_poppler
if ! command -v pdfunite &> /dev/null; then
echo "Installation failed or pdfunite is still unavailable. Exiting."
exit 1
fi
fi

# Directory containing the PDF files
PDF_DIR="./" # Change this if your PDFs are in another directory
MERGED_DIR="./merged" # Directory to save merged PDFs

# Create output directory if it doesn't exist
mkdir -p "$MERGED_DIR"

# Merge papers by type (e.g., qp_1X)
for PAPER_TYPE in 1 2 3 4; do # Add more paper types as needed
# Find all PDFs matching the pattern *_qp_${PAPER_TYPE}*.pdf
PAPER_FILES=$(find "$PDF_DIR" -type f -name "*_qp_${PAPER_TYPE}*.pdf" | sort)

if [ -n "$PAPER_FILES" ]; then
OUTPUT_FILE="${MERGED_DIR}/merged_paper_${PAPER_TYPE}.pdf"
echo "Merging Paper $PAPER_TYPE into $OUTPUT_FILE..."

# Use pdfunite to merge files
pdfunite $PAPER_FILES "$OUTPUT_FILE"

# Check if merge succeeded
if [ $? -eq 0 ]; then
echo "Successfully merged Paper $PAPER_TYPE into $OUTPUT_FILE."
else
echo "Failed to merge Paper $PAPER_TYPE. Please check your files."
fi
else
echo "No files found for Paper $PAPER_TYPE."
fi
done

echo "All merging completed. Merged files are in the $MERGED_DIR directory."

0 comments on commit 41a1c94

Please sign in to comment.