File:  [LON-CAPA] / loncom / interface / printout.pl
Revision 1.25: download - view: text, annotated - select for diffs
Wed May 28 21:25:33 2003 UTC (20 years, 11 months ago) by www
Branches: MAIN
CVS tags: version_0_99_1, HEAD
Bug #1519: printout pages register with Remote

    1: #!/usr/bin/perl
    2: use IO::File;
    3: use Image::Magick;
    4:  print "Content-type: text/html\n\n";
    5: print (<<ENDSCRIPT);
    6:     <script type="text/javascript">
    7: function LONCAPAreg() {
    8:    swmenu=window.open('',"LCmenumsul1","",false);swmenu.windowloaded(self.name);
    9: } 
   10: 
   11: function LONCAPAstale() {
   12:    swmenu=window.open('',"LCmenumsul1","",false);swmenu.windowunloaded(self.name);
   13: }
   14: </script>
   15: ENDSCRIPT
   16:  print "<body bgcolor='#FFFFFF' onLoad='LONCAPAreg();' onUnload='LONCAPAstate()'>\n";
   17:   my ($texfile,$laystyle,$numberofcolumns,$selectionmade) = split(/&/,$ENV{'QUERY_STRING'});
   18:   my $figfile = $texfile;
   19:   $figfile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.dat/;
   20:   my $duefile = $texfile;
   21:   $duefile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.due/;
   22:   #do we have figures?
   23:   if (-e $figfile) {
   24:       my $temporary_file=IO::File->new($figfile) || die "Couldn't open file for reading: $!\n";
   25:       my @content_of_file = <$temporary_file>;
   26:       close $temporary_file;  
   27:       my $noteps;
   28:       foreach $not_eps (@content_of_file) {
   29: 	  if ($not_eps ne '') {
   30: 	      my $eps_f = $not_eps;
   31: 	      $eps_f =~ s/\.[^.]*$/\.eps/i;
   32: 	      $_ = $eps_f;
   33: 	      m/\/([^\/]+)$/;
   34: 	      $eps_f = '/home/httpd/prtspool/'.$1;
   35: 	      my $image = Image::Magick->new;
   36: 	      $not_eps =~ s/^\s+//;
   37: 	      $not_eps =~ s/\s+$//;
   38: 	      $status = $image->Read($not_eps);
   39: 	      if ($status) {print "  $status  ";}
   40: 	      $image->Set(page => '+100+200'); 
   41: 	      $status = $image->Write($eps_f);	    
   42: 	      if ($status) {print "  $status  ";}
   43:               #check is eps exist in prtspool
   44:               if(not -e $eps_f) {
   45: 		  for (my $i=0;$i<10000;$i++) {
   46: 		      if (-e $eps_f.'.'.$i) {
   47: 			  rename $eps_f.'.'.$i, $eps_f;
   48: 			  last;
   49: 		      }
   50: 		  }
   51: 	      }  
   52: 	  }
   53:       }
   54:       unlink $figfile;
   55:   }
   56:   #print "$texfile\n"; #name of the tex file for debugging only
   57:   $texfile =~ m/^(.*)\/([^\/]+)$/; 
   58:   my $name_file = $2;
   59:   my $path_file = $1.'/';
   60:   chdir $path_file;
   61:   system("latex $name_file 1>/dev/null 2>/dev/null");
   62:   #Do we have a latex error in the log file?
   63:   my $logfilename = $texfile;
   64:   $logfilename =~ s/\.tex$/\.log/;
   65:   my $temporary_file=IO::File->new($logfilename) || die "Couldn't open file for reading: $!\n";
   66:   my @content_of_file = <$temporary_file>;
   67:   close $temporary_file; 
   68:   my $body_log_file = join(' ',@content_of_file); 
   69:   if ($body_log_file=~m/!\s+Emergency stop/) {
   70:       #LaTeX failed to parse tex file 
   71:       print "<h2>LaTeX could not successfully parse your tex file.</h2>";
   72:       print "It probably has errors in it.<br />";
   73:       my $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART';
   74:       my $whereitends = rindex $body_log_file,'STAMPOFPASSEDRESOURCEEND';
   75:       if ($whereitbegins!=-1 and $whereitends!=-1) {
   76: 	print "With very high probability this error occured in ".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)."<br /><br />";
   77:       }
   78:       print "Here are the error messages in the LaTeX log file</br><br />";
   79:       my $sygnal = 0;
   80:       for (my $i=0;$i<=$#content_of_file;$i++) {
   81:           if ($content_of_file[$i]=~m/^Runaway argument?/ or $content_of_file[$i]=~m/^!/) {
   82: 	      $sygnal = 1;
   83: 	  } 
   84: 	  if ($content_of_file[$i]=~m/Here is how much of/) {
   85: 	      $sygnal = 0;
   86: 	  } 
   87: 	  if ($sygnal) {
   88: 	      print "$content_of_file[$i]<br />";
   89: 	  }  
   90:       } 
   91:   } else {
   92:       #LaTeX successfully parsed tex file 
   93:       $name_file =~ s/\.tex/\.dvi/;
   94:       my $new_name_file = $name_file;
   95:       $new_name_file =~ s/\.dvi/\.ps/;
   96:       my $comma = "dvips -Ppdf -G0 -o $new_name_file";
   97:       system("$comma $name_file 1>/dev/null 2>/dev/null");
   98:       if (-e $new_name_file) {
   99: 	  print "<h1>Successfully created PDF output file (click on link below)</h1>\n";
  100: 	  $new_name_file =~ m/^(.*)\./;
  101: 	  my $tempo_file = $1.'temporar.ps';
  102: 	  my $name_file = $1.'.pdf';
  103: 	  if ($laystyle eq 'album' and $numberofcolumns eq '2') {
  104: 	      $comma = "psnup -2 -s1.0 $new_name_file";
  105: 	      system("$comma $tempo_file 1>/dev/null 2>/dev/null"); 
  106: 	      system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  107: 	  } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
  108: 	      $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
  109: 	      system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
  110: 	      system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  111: 	  } else {
  112: 	      system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
  113: 	  }	    
  114: 	  my $texlog = $texfile;
  115: 	  my $texaux = $texfile;
  116: 	  my $texdvi = $texfile;
  117: 	  my $texps = $texfile;
  118: 	  $texlog =~ s/\.tex/\.log/;
  119: 	  $texaux =~ s/\.tex/\.aux/;
  120: 	  $texdvi =~ s/\.tex/\.dvi/;
  121: 	  $texps =~ s/\.tex/\.ps/;
  122: 	  my @garb = ($texlog,$texaux,$texdvi,$texps);
  123: #	  unlink @garb;
  124: 	  unlink $duefile;
  125: 	  print "<a href=\"/prtspool/$name_file\">Your PDF document</a>";
  126: 	  print "\n";
  127:       }
  128:   }
  129: 
  130: 
  131:   
  132: 
  133: 
  134: 
  135: 
  136: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>