Skip to content

Instantly share code, notes, and snippets.

@knee-cola
Created August 19, 2024 12:33
Show Gist options
  • Save knee-cola/a25cf8fdeeb0bb3d7cdd5d3a4411a839 to your computer and use it in GitHub Desktop.
Save knee-cola/a25cf8fdeeb0bb3d7cdd5d3a4411a839 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Converts a given input file into a PDF document
# Check if the input argument is provided
if [ -z "$1" ]; then
echo "Usage: $0 <input_file.jpeg>"
exit 1
fi
# Get the input file name
input_file="$1"
# Check if the input file exists
if [ ! -f "$input_file" ]; then
echo "Error: File '$input_file' not found."
exit 1
fi
# Check if the input file is a valid JPEG
file_type=$(file --mime-type -b "$input_file")
if [ "$file_type" != "image/jpeg" ]; then
echo "Error: File '$input_file' is not a valid JPEG file."
exit 1
fi
# Extract the base name (without extension)
base_name="${input_file%.*}"
# Set the output file name
output_file="${base_name}.pdf"
# Convert JPEG to PDF using Ghostscript
gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile="$output_file" "$input_file"
# Check if the conversion was successful
if [ $? -eq 0 ]; then
echo "Conversion completed: ${output_file}"
else
echo "Error: Ghostscript failed to convert '$input_file' to PDF."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment