#!/usr/bin/perl
if($#ARGV ne 1){
  print "TO remove CR with LF outside stream\n";
  print "USAGE: clpdf.pl infile.pdf outfile.pdf\n";
}else{
$infile = $ARGV[0];
$outfile = $ARGV[1];
#
open(Lista,"<$infile")||die "Can't open the file";
open(OUT,">$outfile")||die "Can't open the file";
#
# according to the pdf spec the keyword
# "stream" should be followed by a CR \r and a LF \n or
# just a LF. So there can only be one stream and/or one
# endstream one a line.
#
##################################
#
@listind = <Lista>;
#
 $off = 1;
#
 select(OUT);
 for $i (@listind){
   if($i =~ /(.*)endstream(.*)/){
#
#    $st is stream, $as is ascii
#
      $st = $1;
      $as = $2;
      $as =~ s/\r/\n/g;
#
#     substitution patterns remove the \n
#     (don't use chop because \n\n should be replaced with a blank\n
#      in for example the xref)
#
      $as = $as."\n";
      $as =~ s/\n\n/ \n/g;
      $i = $st."endstream".$as;
#
#     if a new stream appears turn off the global subsitution
#
      if($as =~ /stream/){
         $off = 0;
      }else{
         $off = 1;
      }
   }else{
#
#   in sentences without "stream" rely on $off and check for "stream"
#
    if($off eq 1){
        $i =~ s/\r/\n/g;
        $i =~ s/\n\n/ \n/g;
    }
    if(($i =~ /[^d]stream/) || ($i =~ /^stream/)){
        $off = 0;
    }
   }
   print $i;
 }
close(Lista);
close(OUT);
}
