File:  [LON-CAPA] / loncom / interface / printout.pl
Revision 1.40.2.1: download - view: text, annotated - select for diffs
Thu Feb 12 22:50:35 2004 UTC (20 years, 3 months ago) by albertel
Branches: version_1_1_X
Diff to branchpoint 1.40: preferred, unified
- backport 1.46

    1: #!/usr/bin/perl
    2: # CGI-script to run LaTeX, dvips, ps2ps, ps2pdf etc.
    3: #
    4: # $Id: printout.pl,v 1.40.2.1 2004/02/12 22:50:35 albertel Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: use lib '/home/httpd/lib/perl';
   30: use LONCAPA::loncgi();
   31: use File::Path;
   32: use IO::File;
   33: use Image::Magick;
   34: 
   35: my %origENV=%ENV;
   36: if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
   37:     print <<END;
   38: Content-type: text/html
   39: 
   40: <html>
   41: <head><title>Bad Cookie</title></head>
   42: <body>
   43: Your cookie information is incorrect.
   44: </body>
   45: </html>
   46: END
   47:     return;
   48: }
   49: 
   50:  print "Content-type: text/html\n\n";
   51:  print "<body bgcolor=\"#FFFFFF\">\n";
   52: 
   53:   my $identifier = $ENV{'QUERY_STRING'};
   54:   my $texfile = $ENV{'cgi.'.$identifier.'.file'};
   55:   my $laystyle = $ENV{'cgi.'.$identifier.'.layout'};
   56:   my $numberofcolumns = $ENV{'cgi.'.$identifier.'.numcol'};
   57:   my $selectionmade = $ENV{'cgi.'.$identifier.'.selection'};
   58:   my $tableofcontents = $ENV{'cgi.'.$identifier.'tableofcontents'};
   59:   my $tableofindex = $ENV{'cgi.'.$identifier.'tableofindex'};
   60:   my $advans_role = $ENV{'cgi.'.$identifier.'role'};
   61: 
   62:   my $figfile = $texfile;
   63:   $figfile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.dat/;
   64:   my $duefile = $texfile;
   65:   $duefile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.due/;
   66:   #do we have figures?
   67:   if (-e $figfile) {
   68:       my $temporary_file=IO::File->new($figfile) || die "Couldn't open file for reading: $!\n";
   69:       my @content_of_file = <$temporary_file>;
   70:       close $temporary_file;  
   71:       my $noteps;
   72:       foreach $not_eps (@content_of_file) {
   73: 	  if ($not_eps ne '') {
   74: 	      my $eps_f = $not_eps;
   75: 	      $eps_f =~ s/\.[^.]*$/\.eps/i;
   76: 	      $_ = $eps_f;
   77: 	      m/\/home\/httpd\/html\/res\/(.+)/;
   78: 	      $eps_f = '/home/httpd/prtspool/'.$1;
   79: 	      my $path=$eps_f;
   80: 	      $path=~s/\/([^\/]+)\.eps$//; 
   81: 	      File::Path::mkpath($path,0,0777);
   82: 	      my $image = Image::Magick->new;
   83: 	      $not_eps =~ s/^\s+//;
   84: 	      $not_eps =~ s/\s+$//;
   85: 	      $status = $image->Read($not_eps);
   86: 	      if ($status) {print "  $status  ";}
   87: 	      $image->Set(page => '+100+200'); 
   88: 	      $status = $image->Write($eps_f);	    
   89: 	      if ($status) {print "  $status  ";}
   90:               #check is eps exist in prtspool
   91:               if(not -e $eps_f) {
   92: 		  for (my $i=0;$i<10000;$i++) {
   93: 		      if (-e $eps_f.'.'.$i) {
   94: 			  rename $eps_f.'.'.$i, $eps_f;
   95: 			  last;
   96: 		      }
   97: 		  }
   98: 	      }  
   99: 	  }
  100:       }
  101:   }
  102:   #print "$texfile\n"; #name of the tex file for debugging only
  103:   if (-e $texfile) {
  104:       $texfile =~ m/^(.*)\/([^\/]+)$/; 
  105:       my $name_file = $2;
  106:       my $path_file = $1.'/';
  107:       chdir $path_file;
  108:       system("latex $name_file 1>/dev/null 2>/dev/null");
  109:       if ($tableofcontents eq 'yes') {
  110: 	  system("latex $name_file 1>/dev/null 2>/dev/null");
  111:       } #to create table of contents
  112:       my $idxname=$name_file;
  113:       $idxname=~s/\.tex$/\.idx/;
  114:       if ($tableofindex eq 'yes') {
  115: 	  system("makeindex $idxname");
  116: 	  system("latex $name_file 1>/dev/null 2>/dev/null");
  117:       } #to create index
  118:       #Do we have a latex error in the log file?
  119:       my $logfilename = $texfile;
  120:       $logfilename =~ s/\.tex$/\.log/;
  121:       my $temporary_file=IO::File->new($logfilename) || die "Couldn't open file for reading: $!\n";
  122:       my @content_of_file = <$temporary_file>;
  123:       close $temporary_file; 
  124:       my $body_log_file = join(' ',@content_of_file);
  125:       $logfilename =~ s/\.log$/\.html/;
  126:       $temporary_file = IO::File->new('>'.$logfilename); 
  127:       print $temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_log_file.'</pre></body></html>'."\n";
  128:       if ($body_log_file=~m/!\s+Emergency stop/) {
  129: 	  #LaTeX failed to parse tex file 
  130: 	  print "<h2>LaTeX could not successfully parse your tex file.</h2>";
  131: 	  print "It probably has errors in it.<br />";
  132: 	  my $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART';
  133: 	  my $whereitends = rindex $body_log_file,'STAMPOFPASSEDRESOURCEEND';
  134: 	  if ($whereitbegins!=-1 and $whereitends!=-1) {
  135: 	      print "With very high probability this error occured in ".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)."<br /><br />";
  136: 	  }
  137: 	  print "Here are the error messages in the LaTeX log file</br><br />";
  138: 	  my $sygnal = 0;
  139: 	  for (my $i=0;$i<=$#content_of_file;$i++) {
  140: 	      if ($content_of_file[$i]=~m/^Runaway argument?/ or $content_of_file[$i]=~m/^!/) {
  141: 		  $sygnal = 1;
  142: 	      } 
  143: 	      if ($content_of_file[$i]=~m/Here is how much of/) {
  144: 		  $sygnal = 0;
  145: 	      } 
  146: 	      if ($sygnal) {
  147: 		  print "$content_of_file[$i]<br />";
  148: 	      }  
  149: 	  }
  150:           if ($advans_role) {  
  151:               print "<b><big>The link to ";
  152:               $logfilename=~s/\/home\/httpd//;
  153: 	      print "<a href=\"$logfilename\">Your log file </a></big></b>";
  154: 	      print "\n";
  155:               #link tooriginal LaTeX file (included according Michael Hamlin desire)
  156: 	      my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
  157: 	      my @tex_content_of_file = <$tex_temporary_file>;
  158: 	      close $tex_temporary_file; 
  159: 	      my $body_tex_file = join(' ',@tex_content_of_file);
  160: 	      $texfile =~ s/\.tex$/aaaaa\.html/;
  161: 	      $tex_temporary_file = IO::File->new('>'.$texfile); 
  162: 	      print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
  163: 	      print "<br /><br />";
  164: 	      print "<b><big>The link to ";
  165: 	      $texfile=~s/\/home\/httpd//;
  166: 	      print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
  167: 	      print "\n";
  168: 	  }
  169: 
  170:       } elsif ($body_log_file=~m/<inserted text>/) {
  171: 	  my $whereitbegins = index $body_log_file,'<inserted text>';
  172: 	  print "You are running LaTeX in the <b>batch mode</b>.";
  173: 	  while ($whereitbegins != -1) {
  174: 	      my $tempobegin=$whereitbegins;
  175: 	      $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART',$whereitbegins;
  176: 	      my $whereitends = index $body_log_file,'STAMPOFPASSEDRESOURCEEND',$whereitbegins;
  177: 	      print "<br />It has found an error in".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)." <br /> and corrected it.\n";
  178: 	      print "Usually this correction is valid but you probably need to check the indicated resource one more time and implement neccessary corrections by yourself.\n";
  179: 	      $whereitbegins = index $body_log_file,'<inserted text>',$tempobegin+10;
  180: 	  }
  181: 	  $name_file =~ s/\.tex/\.dvi/;
  182: 	  my $new_name_file = $name_file;
  183: 	  $new_name_file =~ s/\.dvi/\.ps/;
  184: 	  my $comma = "dvips -Ppdf -G0 -o $new_name_file";
  185: 	  system("$comma $name_file 1>/dev/null 2>/dev/null");
  186: 	  if (-e $new_name_file) {
  187: 	      print "<h1>PDF output file (see link below)</h1>\n";
  188: 	      $new_name_file =~ m/^(.*)\./;
  189: 	      my $tempo_file = $1.'temporar.ps';
  190: 	      my $name_file = $1.'.pdf';
  191: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
  192: 		  $comma = "psnup -2 -s1.0 $new_name_file";
  193: 		  system("$comma $tempo_file 1>/dev/null 2>/dev/null"); 
  194: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  195: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
  196: 		  $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
  197: 		  system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
  198: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  199: 	      } else {
  200: 		  system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
  201: 	      }	    
  202: 	      my $texlog = $texfile;
  203: 	      my $texaux = $texfile;
  204: 	      my $texdvi = $texfile;
  205: 	      my $texps = $texfile;
  206: 	      $texlog =~ s/\.tex/\.log/;
  207: 	      $texaux =~ s/\.tex/\.aux/;
  208: 	      $texdvi =~ s/\.tex/\.dvi/;
  209: 	      $texps =~ s/\.tex/\.ps/;
  210: 	      my @garb = ($texaux,$texdvi,$texps);
  211: #	  unlink @garb;
  212: 	      unlink $duefile;
  213: 	      print "<a href=\"/prtspool/$name_file\">Your PDF document</a>";
  214: 	      if ($advans_role) {  
  215: 		  print "<br /><br />";
  216: 		  print "<b><big>The link to ";
  217: 		  $logfilename=~s/\/home\/httpd//;
  218: 		  print "<a href=\"$logfilename\">Your log file </a></big></b>";
  219: 		  print "\n";
  220: 		  #link tooriginal LaTeX file (included according Michael Hamlin desire)
  221: 		  my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
  222: 		  my @tex_content_of_file = <$tex_temporary_file>;
  223: 		  close $tex_temporary_file; 
  224: 		  my $body_tex_file = join(' ',@tex_content_of_file);
  225: 		  $texfile =~ s/\.tex$/aaaaa\.html/;
  226: 		  $tex_temporary_file = IO::File->new('>'.$texfile); 
  227: 		  print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
  228: 		  print "<br /><br />";
  229: 		  print "<b><big>The link to ";
  230: 		  $texfile=~s/\/home\/httpd//;
  231: 		  print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
  232: 		  print "\n";
  233: 	      }
  234: 	  }
  235:       } else {
  236: 	  #LaTeX successfully parsed tex file 
  237: 	  $name_file =~ s/\.tex/\.dvi/;
  238: 	  my $new_name_file = $name_file;
  239: 	  $new_name_file =~ s/\.dvi/\.ps/;
  240: 	  my $comma = "dvips -Ppdf -G0 -o $new_name_file";
  241: 	  system("$comma $name_file 1>/dev/null 2>/dev/null");
  242: 	  if (-e $new_name_file) {
  243: 	      print "<h1>Successfully created PDF output file (see link below)</h1>\n";
  244: 	      $new_name_file =~ m/^(.*)\./;
  245: 	      my $tempo_file = $1.'temporar.ps';
  246: 	      my $name_file = $1.'.pdf';
  247: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
  248: 		  $comma = "psnup -2 -s1.0 $new_name_file";
  249: 		  system("$comma $tempo_file 1>/dev/null 2>/dev/null"); 
  250: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  251: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
  252: 		  $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
  253: 		  system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
  254: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  255: 	      } else {
  256: 		  system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
  257: 	      }	    
  258: 	      my $texlog = $texfile;
  259: 	      my $texaux = $texfile;
  260: 	      my $texdvi = $texfile;
  261: 	      my $texps = $texfile;
  262: 	      $texlog =~ s/\.tex/\.log/;
  263: 	      $texaux =~ s/\.tex/\.aux/;
  264: 	      $texdvi =~ s/\.tex/\.dvi/;
  265: 	      $texps =~ s/\.tex/\.ps/;
  266: 	      my @garb = ($texlog,$texaux,$texdvi,$texps);
  267: #	  unlink @garb;
  268: 	      unlink $duefile;
  269: 	      print "<a href=\"/prtspool/$name_file\">Your PDF document</a>";
  270: 	      print "\n";
  271: 	  }
  272:       }
  273:   } else {
  274:       print "LaTeX file $texfile was not created successfully";
  275:   }
  276:   #got what we needed reset ENV in case it is to big for system
  277:   %ENV=%origENV;
  278: 
  279: 
  280:   
  281: 
  282: 
  283: 
  284: 
  285: 

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