-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf-to-szi.sh
More file actions
executable file
·138 lines (105 loc) · 3.32 KB
/
pdf-to-szi.sh
File metadata and controls
executable file
·138 lines (105 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/bash
set -euo pipefail
# Script to convert PDF to SZI format using mutool and vips
# Usage: ./pdf-to-szi.sh /path/to/file.pdf
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Initialize variables
WORK_DIR=""
# Function to print error messages
error() {
echo -e "${RED}Error: $1${NC}" >&2
exit 1
}
# Function to print success messages
success() {
echo -e "${GREEN}$1${NC}"
}
# Function to print info messages
info() {
echo -e "${YELLOW}$1${NC}"
}
# Function to cleanup on error or interrupt
cleanup() {
if [ -n "$WORK_DIR" ] && [ -d "$WORK_DIR" ]; then
info "Cleaning up temporary files..."
rm -f "$WORK_DIR"/*.png
fi
}
# Set up trap to cleanup on exit or interrupt
trap cleanup EXIT INT TERM
# Validate argument count
if [ $# -ne 1 ]; then
error "Usage: $0 <path-to-pdf-file>"
fi
PDF_FILE="$1"
# Check if file exists
if [ ! -f "$PDF_FILE" ]; then
error "File not found: $PDF_FILE"
fi
# Check if file is a PDF
if [[ "$PDF_FILE" != *.pdf && "$PDF_FILE" != *.PDF ]]; then
error "File does not appear to be a PDF: $PDF_FILE"
fi
# Convert to absolute path before changing directories
PDF_FILE="$(cd "$(dirname "$PDF_FILE")" && pwd)/$(basename "$PDF_FILE")"
# Check if required tools are installed
command -v mutool &>/dev/null || error "mutool is not installed. Please install mupdf-tools."
command -v vips &>/dev/null || error "vips is not installed. Please install libvips."
# Get directory and filename
OUTPUT_DIR="$(pwd)"
BASENAME="$(basename "$PDF_FILE" .pdf)"
BASENAME="${BASENAME%.PDF}"
# Set working directory (temporary directory for intermediate files)
WORK_DIR=$(mktemp -d)
info "Converting PDF to PNG images..."
info "Input: $PDF_FILE"
# Change to working directory
cd "$WORK_DIR"
# Convert PDF pages to PNG images at 300 DPI
if ! mutool draw -r 300 -o "img%d.png" "$PDF_FILE"; then
error "Failed to convert PDF to PNG images"
fi
# Check if any PNG files were created
if ! ls img*.png 1>/dev/null 2>&1; then
error "No PNG files were created from the PDF"
fi
PNG_COUNT=$(ls img*.png 2>/dev/null | wc -l)
info "Successfully created $PNG_COUNT PNG images"
info "Joining images with vips..."
# Create temporary DZ file
TEMP_DZ="${BASENAME}.dz"
SZI_FILE="${BASENAME}.szi"
# Join PNG files into a deep zoom image
# Sort numerically to maintain correct page order (works on both Linux and macOS)
if ! vips arrayjoin "$(ls img*.png 2>/dev/null | sort -V | tr '\n' ' ' | sed 's/ $//')" "$TEMP_DZ" --shim 50 --across 4 --background "255, 255, 255"; then
error "Failed to join images with vips"
fi
# Check if DZ file was created
if [ ! -f "$TEMP_DZ" ]; then
error "Failed to create deep zoom file"
fi
info "Renaming to SZI format..."
# Rename DZ to SZI
if ! mv "$TEMP_DZ" "$SZI_FILE"; then
error "Failed to rename file to SZI"
fi
# Move SZI file to the same directory as the input PDF
FINAL_SZI_FILE="$OUTPUT_DIR/$SZI_FILE"
info "Moving SZI file to output directory..."
if ! mv "$SZI_FILE" "$FINAL_SZI_FILE"; then
error "Failed to move SZI file to output directory"
fi
info "Removing temporary PNG files..."
# Remove PNG files
rm -f img*.png
# Verify final output
if [ ! -f "$FINAL_SZI_FILE" ]; then
error "Final SZI file was not created"
fi
success "✓ Conversion complete!"
success "Output file: $FINAL_SZI_FILE"
echo "Size: $(du -h "$FINAL_SZI_FILE" | cut -f1)"