program bookprint; {print both sides of paper} { author: Russell Whitaker Dec. 25, 2000 released under terms of gnu gpl } uses gpc; {for execute command} {$define ESC char(27)} const TABS = "\t\t\t\t\t\t\t\t\t\t"; TOF = "\f"; MaxLines = 59; {58 plus one for page number} Maxlen = 90; {maximum line length without warning message} AddBlank = 30; {for printer with push feed} {-- printer specific values - setup for panasonic kx-p2123 in epson mode --} LQ_R_E = ESC + char(1) + ESC + 'k' + char(0) + ESC + 'M'; {LQ, roman, elite} {set left margin in 6 spaces, rt margin full} MarginOdd = ESC + 'l' + char(6) + ESC + 'Q' + char(96); {set left margin back to begining and rt margin in 6 spaces} MarginEven = ESC + 'l' + char(0) + ESC + 'Q' + char(90); B_R = char(7) + char(7) + ESC + '@'; {bells + reset printer} var filenam : string ( 80 ); InputFile : text; LST_odd, LST_even: text; errcode : integer; IsEven : boolean = FALSE; TxtLine : string ( 512 ); LineCount : integer = 0; PageCount : integer = 1; ch : char; procedure NewPage; begin while ( not EOF( InputFile )) and ( length( TxtLine ) = 0 ) do readln( Inputfile, TxtLine ); { discard blank lines } If length( TxtLine ) > 0 then begin Inc( PageCount ); IsEven := not IsEven; if IsEven then begin if PageCount > 2 then write( LST_even, TOF ) else write( LST_even, LQ_R_E, MarginEven ); writeln( LST_even, PageCount ); writeln( LST_even, TxtLine ); end else begin if PageCount > 2 then write( LST_odd, TOF ); {casn't do the else here} writeln( LST_odd, TABS, PageCount ); writeln( LST_odd, TxtLine ); end; LineCount := 2; {because we printed 2 lines} end; end; {-- main --} begin if paramcount = 0 then begin writeln('splits file into odd and even pages for printing'); writeln('syntax: bookprint {using TOF}'); end else {-- open input file --} begin filenam := paramstr( 1 ); assign(InputFile, filenam); {$I-} reset( InputFile ); {$I+} errcode := IOresult; if errcode <> 0 then writeln('Error: unable to open input file "', filenam, '"' ) else {-- open output files --} begin assign( LST_odd, filenam + '.odd' ); rewrite( LST_odd ); assign( LST_even, filenam + '.even' ); rewrite( LST_even ); {-- inital printer setup --} write( LST_odd, LQ_R_E, MarginOdd ); {-- read write loop --} while not EOF( Inputfile ) do begin readln( Inputfile, TxtLine ); inc( LineCount ); {-- long line kludge --} if length( TxtLine ) > Maxlen then begin writeln("WARNING ", length( TxtLine ), " cols at page ", PageCount, ", line ", LineCount ); inc( LineCount ); end; {-- check for existing TOF --} if TxtLine[ 1 ] = TOF then begin if length( TxtLine ) = 1 then TxtLine := '' else TxtLine[ 1 ] := ' '; NewPage; end; {-- prevent single line at bottem of page --} if( LineCount = MaxLines ) and ( length( TxtLine ) = 0 ) then NewPage else if LineCount > MaxLines then NewPage else if IsEven then writeln( LST_even, TxtLine ) else writeln( LST_odd, TxtLine ); end; {-- adjustment if blank page needed --} if IsEven and ( LineCount > AddBlank ) then begin writeln( LST_odd, TOF ); writeln(" Added blank page to end"); end; writeln( LST_even, TOF, B_R ); {-- close all files --} close( InputFile ); close( LST_odd ); close( LST_even ); {-- send to printer --} writeln(" Page count = ", PageCount, "; OK to print? [y|n] " ); repeat readln( ch ); until (ch = "y") or (ch = "n"); if ch = "y" then begin {note: if you are using apsfilter, add -Praw after lpr} errcode := execute('lpr -r '+ filenam +'.odd'); writeln(" Now printing odd pages. When finished, reload printer"); writeln(" to print back of page one and press enter"); readln; errcode := execute('lpr -r '+ filenam +'.even'); end; end; end; end.