#!/bin/bash

# txt2pdf.sh
# reads text from stdin an writes single-sheet PDF-file DIN A4 to stdout
# Frank W. Bergmann, www.tuxad.com, 11/2008

{
echo '%!PS-Adobe-2.0
%%Title: txt2pdf.pdf
%%Creator: txt2pdf.sh
%%CreationDate: Fri Sep 26 22:30:32 2008
%%For: root@localhost.localdomain (root)
%%Orientation: Portrait
%%Pages: 1
%%BoundingBox: 0 0 595 842
%%DocumentPaperSizes: a4
%%BeginSetup
[{
%BeginFeature: *PageRegion A4
<</PageSize [595 842]>> setpagedevice
%EndFeature
} stopped cleartomark
%%EndSetup
save
newpath 0 842 moveto 0 0 lineto 595 0 lineto 595 842 lineto closepath clip newpath
28 41 translate

% some shortcuts
/gs {gsave} bind def
/gr {grestore} bind def
/sa {save} bind def
/rs {restore} bind def
/l {lineto} bind def
/m {moveto} bind def
/rl {rlineto} bind def
/rm {rmoveto} bind def
/n {newpath} bind def
/s {stroke} bind def
/sh {show} bind def

% re-encode fonts to iso
/reencdict 12 dict def /ReEncode { reencdict begin
/newcodesandnames exch def /newfontname exch def /basefontname exch def
/basefontdict basefontname findfont def /newfont basefontdict maxlength dict def
basefontdict { exch dup /FID ne { dup /Encoding eq
{ exch dup length array copy newfont 3 1 roll put }
{ exch newfont 3 1 roll put } ifelse } { pop pop } ifelse } forall
newfont /FontName newfontname put newcodesandnames aload pop
128 1 255 { newfont /Encoding get exch /.notdef put } for
newcodesandnames length 2 idiv { newfont /Encoding get 3 1 roll put } repeat
newfontname newfont definefont pop end } def
/isovec [
8#055 /minus 8#200 /grave 8#201 /acute 8#202 /circumflex 8#203 /tilde
8#204 /macron 8#205 /breve 8#206 /dotaccent 8#207 /dieresis
8#210 /ring 8#211 /cedilla 8#212 /hungarumlaut 8#213 /ogonek 8#214 /caron
8#220 /dotlessi 8#230 /oe 8#231 /OE
8#240 /space 8#241 /exclamdown 8#242 /cent 8#243 /sterling
8#244 /currency 8#245 /yen 8#246 /brokenbar 8#247 /section 8#250 /dieresis
8#251 /copyright 8#252 /ordfeminine 8#253 /guillemotleft 8#254 /logicalnot
8#255 /hyphen 8#256 /registered 8#257 /macron 8#260 /degree 8#261 /plusminus
8#262 /twosuperior 8#263 /threesuperior 8#264 /acute 8#265 /mu 8#266 /paragraph
8#267 /periodcentered 8#270 /cedilla 8#271 /onesuperior 8#272 /ordmasculine
8#273 /guillemotright 8#274 /onequarter 8#275 /onehalf
8#276 /threequarters 8#277 /questiondown 8#300 /Agrave 8#301 /Aacute
8#302 /Acircumflex 8#303 /Atilde 8#304 /Adieresis 8#305 /Aring
8#306 /AE 8#307 /Ccedilla 8#310 /Egrave 8#311 /Eacute
8#312 /Ecircumflex 8#313 /Edieresis 8#314 /Igrave 8#315 /Iacute
8#316 /Icircumflex 8#317 /Idieresis 8#320 /Eth 8#321 /Ntilde 8#322 /Ograve
8#323 /Oacute 8#324 /Ocircumflex 8#325 /Otilde 8#326 /Odieresis 8#327 /multiply
8#330 /Oslash 8#331 /Ugrave 8#332 /Uacute 8#333 /Ucircumflex
8#334 /Udieresis 8#335 /Yacute 8#336 /Thorn 8#337 /germandbls 8#340 /agrave
8#341 /aacute 8#342 /acircumflex 8#343 /atilde 8#344 /adieresis 8#345 /aring
8#346 /ae 8#347 /ccedilla 8#350 /egrave 8#351 /eacute
8#352 /ecircumflex 8#353 /edieresis 8#354 /igrave 8#355 /iacute
8#356 /icircumflex 8#357 /idieresis 8#360 /eth 8#361 /ntilde 8#362 /ograve
8#363 /oacute 8#364 /ocircumflex 8#365 /otilde 8#366 /odieresis 8#367 /divide
8#370 /oslash 8#371 /ugrave 8#372 /uacute 8#373 /ucircumflex
8#374 /udieresis 8#375 /yacute 8#376 /thorn 8#377 /ydieresis] def
/Helvetica /Helvetica-iso isovec ReEncode
/Helvetica-Bold /Helvetica-Bold-iso isovec ReEncode

% some other base settings
10 setmiterlimit
0 setlinejoin 0 setlinecap
0.000 0.000 0.000 setrgbcolor
1.6 setlinewidth

% begin content

'


# set some default and include optional rc-data

FONTNAME=Helvetica
FONTSIZE=12
[ -f ~/.txt2ps/fontname ] && FONTNAME=$(tail -n 1 ~/.txt2ps/fontname)
[ -f ~/.txt2pdf/fontname ] && FONTNAME=$(tail -n 1 ~/.txt2pdf/fontname)
[ -f ~/.txt2ps/fontsize ] && FONTSIZE=$(tail -n 1 ~/.txt2ps/fontsize)
[ -f ~/.txt2pdf/fontsize ] && FONTSIZE=$(tail -n 1 ~/.txt2pdf/fontsize)
FONTNAME="$FONTNAME"-iso
FONTSIZE=$(echo $FONTSIZE|cut -d. -f1)
let "LINELEN=960/$FONTSIZE"

echo "/$FONTNAME findfont $FONTSIZE.0 scalefont setfont"

YPOS=782
let YDIFF=$FONTSIZE+1
let YPOS=$YPOS-$YDIFF

fold -bs -w $LINELEN | while read txtline
do
  echo "5 $YPOS m gs ($txtline) sh gr"
  [ "$YPOS" -le "$YDIFF" ] && break
  let YPOS=$YPOS-$YDIFF
done

echo restore showpage

} \
  |ps2pdf13 - -

