Skip to content

Instantly share code, notes, and snippets.

@NicholasBallard
Forked from gildotdev/txt2rtf.sh
Created June 12, 2018 16:50
Show Gist options
  • Save NicholasBallard/32555b35f582399ee553d7dffb946a3f to your computer and use it in GitHub Desktop.
Save NicholasBallard/32555b35f582399ee553d7dffb946a3f to your computer and use it in GitHub Desktop.
Simple Bash script to create a rich text format file from a plain text file
#!/bin/sh
#check to see if arguments are set
E_BADARGS=65
if [ ! -n "$1" ]
then
echo "Usage: `basename $0` textfilename [rtffilename optional]"
exit $E_BADARGS
fi
#Set font face
#font="Courier"
font="Arial"
#font="Times New Roman"
#Set font size in pt
fontsize=12
#Set document height in inches
height=11 #letter
#height=14 #legal
#Set document width in inches
width=8.5
#Set document orientation
orientation="portrait"
#orientation="landscape"
#set document margins in inches
leftm=0.5
rightm=0.5
topm=0.5
bottomm=0.5
##################################
#NOTHING BELOW NEEDS TO BE EDITED#
##################################
#calculate rtf sizes
fontsize=$(echo "$fontsize*2" | bc | sed 's/\.[^.]*$//')
height=$(echo "$height*1440" | bc | sed 's/\.[^.]*$//')
width=$(echo "$width*1440" | bc | sed 's/\.[^.]*$//')
leftm=$(echo "$leftm*1440" | bc | sed 's/\.[^.]*$//')
rightm=$(echo "$rightm*1440" | bc | sed 's/\.[^.]*$//')
topm=$(echo "$topm*1440" | bc | sed 's/\.[^.]*$//')
bottomm=$(echo "$bottomm*1440" | bc | sed 's/\.[^.]*$//')
#set output filename
if [ ! -n "$2" ]
then
fname=`echo $1 | sed 's/\.[^.]*$//'`.rtf
else
fname=`echo $2`
fi
#start header
echo "{\rtf1\ansi\deff0 {\fonttbl {\f0 $font;}}" > $fname
echo "\paperh$height \paperw$width" >> $fname
echo "\margl$leftm \margr$rightm \margt$topm \margb$bottomm" >> $fname
echo "\f0\fs$fontsize" >> $fname
#add \line to the end of each line and replace an form feed characters
#with \page for page breaks
sed s/\$/'\\line'/ $1 | sed s/\\f/'\\page'/ >> $fname
#close rtf file
echo "}" >> $fname
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment