Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'And here's a more sophisticated version...' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
And here's a more sophisticated version...
Authored by: snuggles on Dec 10, '09 10:45:50AM
...taking advantage of bash, PHP, enscript, and gs (all part of OS X) to create a nicely-formatted PDF:
#!/bin/bash

MOD_TIME_EPOCH=`stat -f %m $1`
MOD_TIME_FMT=`date -r $MOD_TIME_EPOCH +'%a %e %b %Y @ %R %Z' | sed 's/ [ ]*/ /g'`
FULL_NAME=`php -r "echo realpath('$1');"`

mkdir -p $HOME/.enscript

# Create temporary "fancy header" file for enscipt.
cat > $HOME/.enscript/tmp_txt2pdf.hdr <<EOF
% -- code follows this line --
%Format: last_mod_timestr $MOD_TIME_FMT
%Format: pagestr page \$V$%/$=
%HeaderHeight: 30
%FooterHeight: 30
/Helvetica /helvetica-encoded MF
/Helvetica-10-Regular /helvetica-encoded findfont 10 scalefont def
/Helvetica-Bold /helvetica-bold-encoded MF
/Helvetica-10-Bold   /helvetica-bold-encoded findfont 10 scalefont def
/new_marg 4 def
/do_header {
  gsave
    Helvetica-10-Bold setfont
    d_footer_x d_footer_w add pagestr stringwidth pop sub
    new_marg sub
    d_footer_y 7 add moveto pagestr show
    Helvetica-10-Bold setfont
    d_footer_x new_marg add d_footer_y 7 add moveto last_mod_timestr show
    Helvetica-10-Bold setfont
    d_header_x d_header_w 2 div add fname stringwidth pop 2 div sub
    d_header_y 16 add moveto fname show
  grestore
} def
EOF

enscript --fancy-header=tmp_txt2pdf --word-wrap -p - "$FULL_NAME" | gs -q \
    -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile="$2" -

rm -f $HOME/.enscript/tmp_txt2pdf.hdr
Save this in your somewhere in your path as 'txt2pdf', make sure it's executable, and use a follows:
txt2pdf input_file.txt output_file.pdf
The contents of the text file will be rendered in word-wrapped 10-point Courier with headers and footers in 10-point bold Helvetica: the filename (including full path) centered in the header, file modification date and time on the left side of the footer, and "page x/y" on the right side of the footer.

Edited on Dec 10, '09 10:53:32AM by snuggles


[ Reply to This | # ]