I'm trying to write a small bash script that'll convert source files in a directory to a .pdf. The steps are these

  • Locate files with find
  • execute iconv on the files converting the character encoding from utf-8 to iso-8859-1 via finds -exec
  • pass this output through enscript and eventually ps2pdf

The only reason I've added iconv is that enscript does not support utf-8. So far I've made the following mess:

enscript --portrait --columns=1 --header ' $n|'"$2"'|%F  Page $% of $=' --highlight='java' --line-numbers --output='-' --color --font='Courier8' `find $1 -name '*.java' -type f -exec iconv -f utf-8 -t iso-8859-1 {} +` | ps2pdf - "$2"'.pdf'

Running it give me this output: enscript: invalid option -- ',' Try `enscript --help' for more information.

Edit: It did not give that error before I added -exec iconv.
2. edit: The original formulation I made was vague I see now. Heres the original script I tried to modify:

`which enscript` --portrait --columns=1 --header ' $n|'"$2"'|%F  Page $% of $=' --highlight='java' --line-numbers --output='-' --color --font='Courier8' `find $1'/src/' -name '*.java'` | ps2pdf - "$2"'.pdf'

./script.sh target_dir output_file_name

The original script combines all source files in a folder recursively into one single pdf with each file starting at a fresh page. Maybe I just got to live with having to do this in two steps. First converting charset and then converting to pdf.

  • While I cannot tell what's wrong with your line by looking at it, it could help to try prepending it with echo and checking that the expanded line is what you are actually expecting it to be. Doing so may oftentimes help you see the problem in an instant.
    – undercat
    Feb 25, 2017 at 23:17

1 Answer 1


Create a java2ps script to easying the parameter handling


export ENSCRIPT="

iconv -f utf-8 -t iso-8859-1 "$1" \
| enscript --header=' $n|'"$1"'|%F  Page $% of $='

Now, the execution is simple

find -name '*.java' -type f -exec ~/java2ps {} \; | ps2pdf - > all.pdf
  • Appreciate your reply. This gives a pdf for each source file. I was looking for a single file combining all source files into one.
    – ndx
    Feb 26, 2017 at 15:42
  • I have changed it for single output file.
    – ingopingo
    Feb 26, 2017 at 15:55
  • Very nice. Thank you. Now I just need to figure out how to make a decent header/footer with enscript and wrap it all into a one step process.
    – ndx
    Feb 26, 2017 at 16:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .