# 清理函数 - 默认清理中间PDF文件 cleanup() { if [ -f "$PDF_FILE" ]; then log_info "Cleaning up temporary PDF file: $PDF_FILE" rm -f "$PDF_FILE" fi }
# 错误处理 trap cleanup EXIT
# 检查输入参数 if [ "$#" -ne 2 ]; then log_error "Invalid arguments. Usage: $0 <input_file_path> <output_dir>" exit 1 fi
INPUT_PATH="$1" OUTPUT_DIR="$2"
# 检查输入文件是否存在且可读 if [ ! -f "$INPUT_PATH" ] || [ ! -r "$INPUT_PATH" ]; then log_error "Input file does not exist or is not readable: $INPUT_PATH" exit 1 fi
# 确保输出目录存在 mkdir -p "$OUTPUT_DIR"
# 检查输出目录是否可写 if [ ! -w "$OUTPUT_DIR" ]; then log_error "Output directory is not writable: $OUTPUT_DIR" exit 1 fi
# 检查必要的命令是否存在 for cmd in libreoffice pdftoppm; do if ! command -v $cmd &> /dev/null; then log_error "Required command '$cmd' not found" exit 1 fi done
# 检查文件扩展名 case"$EXTENSION"in ppt|pptx) log_info "Converting $INPUT_PATH to PDF..." if ! libreoffice --headless --convert-to pdf:impress_pdf_Export --outdir "$OUTPUT_DIR""$INPUT_PATH"; then log_error "PPT to PDF conversion failed" exit 2 fi
if [ ! -f "$PDF_FILE" ]; then log_error "PDF file was not created" exit 2 fi
log_info "PPT to PDF conversion succeeded: $PDF_FILE" log_info "Converting $PDF_FILE to PNG..."
# 以basename为目录名,确保目录存在 OUTPUT_SUB_DIR="$OUTPUT_DIR/$BASE_NAME" if ! mkdir -p "$OUTPUT_SUB_DIR"; then log_error "Failed to create output directory: $OUTPUT_SUB_DIR" exit 1 fi # 以basename为目录名, 1为前缀 if ! pdftoppm -png "$PDF_FILE""$OUTPUT_SUB_DIR/1"; then log_error "PDF to PNG conversion failed" exit 3 fi
log_info "PDF to PNG conversion completed successfully" log_info "Output files are located in: $OUTPUT_DIR"
# 检查是否至少生成了一个PNG文件 if ! ls"$OUTPUT_DIR/${BASE_NAME}"*.png >/dev/null 2>&1; then log_error "No PNG files were generated" exit 3 fi ;; *) log_error "Unsupported file type: $EXTENSION. Only PPT and PPTX are supported." exit 4 ;; esac