-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert-docx-to-pdf
executable file
·138 lines (121 loc) · 3.4 KB
/
convert-docx-to-pdf
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
# Script: convert-docx-to-pdf
# Description: Converts specified DOCX files to PDF format
# Author: Konnor Kooi
# Date: 2024-11-11
# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m' # No Color
# Function to print status messages
print_status() {
echo -e "${CYAN}=>${NC} $1"
}
# Function to print success messages
print_success() {
echo -e "${GREEN}✓${NC} $1"
}
# Function to print error messages
print_error() {
echo -e "${RED}✗${NC} $1" >&2
}
# Function to show help message
show_help() {
echo -e "
${GREEN}DOCX to PDF Converter${NC}
A tool to convert DOCX files to PDF format
${BOLD}USAGE${NC}
$(basename "$0") [options] file1.docx [file2.docx ...]
${BOLD}OPTIONS${NC}
${CYAN}-o${NC}, ${CYAN}--outdir${NC} DIR Output directory for PDF files (default: same as input)
${CYAN}-d${NC}, ${CYAN}--debug${NC} Enable debug output
${CYAN}-h${NC}, ${CYAN}--help${NC} Show this help message
${BOLD}NOTES${NC}
• By default, PDFs are created in the same directory as the input files
• Requires LibreOffice to be installed
• Use --debug to see detailed conversion information
• Multiple files can be converted in a single command"
}
# Default values
OUTPUT_DIR=""
DEBUG=false
# Check if libreoffice is installed
if ! command -v libreoffice &> /dev/null; then
print_error "LibreOffice is not installed. Please install it first:"
echo -e "${BLUE}For Debian/Ubuntu:${NC}"
echo " sudo apt-get install libreoffice"
exit 1
fi
# Parse command line arguments
FILES=()
while [[ $# -gt 0 ]]; do
case $1 in
-o|--outdir)
if [ -d "$2" ]; then
OUTPUT_DIR="$(cd "$2" && pwd)"
else
print_error "Output directory does not exist: $2"
exit 1
fi
shift 2
;;
-d|--debug)
DEBUG=true
shift
;;
-h|--help)
show_help
exit 0
;;
-*)
print_error "Unknown option: $1"
show_help
exit 1
;;
*)
if [[ $1 == *.docx ]]; then
if [ -f "$1" ]; then
FILES+=("$1")
else
print_error "File does not exist: $1"
exit 1
fi
else
print_error "Not a DOCX file: $1"
exit 1
fi
shift
;;
esac
done
# Check if any files were provided
if [ ${#FILES[@]} -eq 0 ]; then
print_error "No input files specified"
show_help
exit 1
fi
# Convert files
converted=0
failed=0
for file in "${FILES[@]}"; do
[ "$DEBUG" = true ] && print_status "Converting: $file"
# Determine output directory
outdir="${OUTPUT_DIR:-$(dirname "$file")}"
if libreoffice --headless --convert-to pdf "$file" --outdir "$outdir" 2>/dev/null; then
((converted++))
print_success "Converted: $(basename "$file")"
else
((failed++))
print_error "Failed to convert: $(basename "$file")"
fi
done
# Print summary
echo
print_status "Conversion completed!"
print_success "Successfully converted: $converted files"
if [ "$failed" -gt 0 ]; then
print_error "Failed to convert: $failed files"
fi