0

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:

#!/bin/bash
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:

#!/bin/bash
`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.

1
  • 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 '17 at 23:17
0

Create a java2ps script to easying the parameter handling

#!/bin/bash

export ENSCRIPT="
--portrait
--columns=1
--font=Courier8
--highlight=java
--color
--line-numbers
--output=-
"

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
3
  • 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 '17 at 15:42
  • I have changed it for single output file.
    – ingopingo
    Feb 26 '17 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 '17 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.