An All-in-One Bash Script for PDF Conversion
Published:
Do you often find yourself needing to convert various file types to PDF format? If so, this versatile bash script can be a valuable tool in your arsenal. With this script, you can easily convert files to PDF using appropriate command-line utilities based on their file types. Whether you have text documents, markdown files, presentations, or even obscure file formats, this script has got you covered.
#!/bin/bash
# Making pdf from anything.
# This script is mapped to a key in my ranger config.
file=$(readlink -f "$1")
dir=$(dirname "$file")
base=$(basename "$file" ."$ext")
ext="${file##*.}"
cd "$dir" || exit 1
textype() {
command="pdflatex"
if head -n5 "$file" | grep -qi 'xelatex'; then
command="xelatex"
fi
if head -n1 "$file" | grep -qi 'handout'; then
$command --output-directory="$dir" --jobname="${base}_handout" "\PassOptionsToClass{handout}{beamer} \input{$base}" && bibtex "${base}_handout.aux"
fi
$command --output-directory="$dir" "$base" && bibtex "$base".aux
}
mdtype() {
command="md1"
if head -n5 "$file" | grep -qi 'beamer'; then
command="beamer"
fi
if head -n5 "$file" | grep -qi 'twoside'; then
command="md2"
fi
~/scripts/pandoc.sh "$1" "$command" # It's a bash script made using pandoc with some predefined settings.
}
case "$ext" in
djvu) ddjvu -format=pdf "$file" "${base}.pdf";;
md) mdtype "$file";;
ps) ps2pdf "$file";;
odt|ods|doc|ppt|pptx|docx) unoconv -f pdf "$file";;
txt) enscript -j -p -B --margins=10:10: -X dos -o "${base}.ps" "$file" && ps2pdf "${base}.ps" "${base}.pdf" && rm "${base}.ps";;
chm) chm2pdf --book "$file" "${base}.pdf";;
epub) pandoc -s -t latex "$file" -o "${base}.pdf";;
tex) textype "$file";;
bib) pandoc "$file" --csl ieee.csl -s -o "${base}.pdf";;
*) head -n1 "$file" | grep "^#!/" | sed "s/^#!//" | xargs -r -I % "$file" ;;
esac
Script Overview
Let’s take a closer look at the key features and functionality of this script:**
Flexibility: This script is designed to handle a wide range of file types, making it a convenient tool for various conversion needs.
Ease of Use: The script can be easily executed by binding it to a key in a file manager like Ranger
, simplifying the conversion process.
Automatic Detection: The script automatically detects the file type based on its extension and applies the appropriate conversion command.
Customizable Conversions: The script includes two functions, textype and mdtype, which scan the first 5 lines of the file for predefined parameters and apply corresponding conversion commands. This allows you to customize the conversion process based on specific requirements.
Multiple Utilities: The script utilizes various command-line utilities such as pdflatex
, xelatex
, bibtex
, pandoc
, unoconv
, ddjvu
, ps2pdf
, enscript
, and chm2pdf
to handle different file types and perform the conversions.
Usage Instructions
To use this script effectively, follow these steps:
- Save the script to a file, e.g.,
makepdf.sh
. - Make the script executable by running the command:
chmod +x makepdf.sh.
- Bind the script to a key or shortcut in your file manager (e.g., Ranger,vifm,thunar) for easy access.
- Select the file you want to convert and trigger the script using the bound key or shortcut.
- The script will automatically detect the file type, apply the corresponding conversion command, and generate a PDF file with the same name in the same directory as the original file.