#!/usr/bin/bash
# Copyright 2011-2021 Chad Lemmen http://www.lemmen.com
#
################################################################################
# File:     txt2pdf 
# Date:     2011-03-07
# Author:   Chad Lemmen http://www.stansoft.org
# Purpose:  Export a text file to PDF
# History:
#           2021-12-03 CDL - Added gs -dNOSAFER option for reading image
#                            file from modps.
#
################################################################################

set -o pipefail

if [ $# = 0 ]; then # No options passed
  echo "Usage: $0 [option] [filename]"
  exit 0
fi

# The printable area can be found in a cups PPD file
# values are: llx lly urx ury
#*ImageableArea Letter/US Letter: "12 12 600 780"
#*ImageableArea A4/A4: "12 12 583 830
#
# 120/cpi ~ PostScript points 
# enscript font list /usr/share/enscript/afm/font.map 
OPT="-q --lines-per-page=66 --no-header --title= --margins=12:12:12:12"
OPT2="--font=Courier7.0 -s 4.5" # 16.67/8.5
OPT3="--font=Courier9.9 -s 1.3" # 12cpi


# do not quote the option variables
case "$1" in
  cnds)
     # Wide column do compressed print to fit on standard letter
     OPTS=($OPT $OPT2)
     ;;
  12cpi)
     OPTS=($OPT $OPT3)
     ;;
  *)
     OPTS=($OPT)
     ;;
esac 

enscript "${OPTS[@]}" "$2" -o - | modps | ps2pdf -dNOSAFER - "$2".pdf

