File:  [LON-CAPA] / loncom / interface / printout.pl
Revision 1.42: download - view: text, annotated - select for diffs
Tue Feb 3 00:02:15 2004 UTC (20 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- when priting multiple students the same image file might be listed many times
  lets just do the conversion once

    1: #!/usr/bin/perl
    2: # CGI-script to run LaTeX, dvips, ps2ps, ps2pdf etc.
    3: #
    4: # $Id: printout.pl,v 1.42 2004/02/03 00:02:15 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: if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
   36:     print <<END;
   37: Content-type: text/html
   38: 
   39: <html>
   40: <head><title>Bad Cookie</title></head>
   41: <body>
   42: Your cookie information is incorrect.
   43: </body>
   44: </html>
   45: END
   46:     return;
   47: }
   48: 
   49:  print "Content-type: text/html\n\n";
   50:  print "<body bgcolor=\"#FFFFFF\">\n";
   51: 
   52:   my $identifier = $ENV{'QUERY_STRING'};
   53:   my $texfile = $ENV{'cgi.'.$identifier.'.file'};
   54:   my $laystyle = $ENV{'cgi.'.$identifier.'.layout'};
   55:   my $numberofcolumns = $ENV{'cgi.'.$identifier.'.numcol'};
   56:   my $selectionmade = $ENV{'cgi.'.$identifier.'.selection'};
   57:   my $tableofcontents = $ENV{'cgi.'.$identifier.'tableofcontents'};
   58:   my $tableofindex = $ENV{'cgi.'.$identifier.'tableofindex'};
   59:   my $advans_role = $ENV{'cgi.'.$identifier.'role'};
   60: 
   61:   my $figfile = $texfile;
   62:   $figfile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.dat/;
   63:   my $duefile = $texfile;
   64:   $duefile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.due/;
   65:   #do we have figures?
   66:   if (-e $figfile) {
   67:       my %done_conversion;
   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: 	      $not_eps=~s|\/\.\/|\/|g;
   75: 	      my $eps_f = $not_eps;
   76: 	      $eps_f =~ s/\.[^.]*$/\.eps/i;
   77: 	      if ($eps_f=~/\/home\/([^\/]+)\/public_html\//) {
   78:                   $eps_f=~s/\/home\/([^\/]+)\/public_html/$1/;
   79: 		  $eps_f = '/home/httpd/prtspool/'.$eps_f;
   80: 	      } else {
   81: 		  $eps_f=~m/\/home\/httpd\/html\/res\/(.+)/;
   82: 		  $eps_f = '/home/httpd/prtspool/'.$1;
   83: 	      }
   84: 	      my $path=$eps_f;
   85: 	      $path=~s/\/([^\/]+)\.eps$//;
   86: 	      File::Path::mkpath($path,0,0777);
   87: 	      my $image = Image::Magick->new;
   88: 	      $not_eps =~ s/^\s+//;
   89: 	      $not_eps =~ s/\s+$//;
   90: 	      if ( exists($done_conversion{$not_eps})) {
   91: 		  next;
   92: 	      }
   93: 	      $done_conversion{$not_eps}=1;
   94: 	      $status = $image->Read($not_eps);
   95: 	      if ($status) {print "  $status  ";}
   96: 	      $image->Set(page => '+100+200'); 
   97: 	      $status = $image->Write($eps_f);	    
   98: 	      if ($status) {print "  $status  ";}
   99:               #check is eps exist in prtspool
  100:               if(not -e $eps_f) {
  101: 		  for (my $i=0;$i<10000;$i++) {
  102: 		      if (-e $eps_f.'.'.$i) {
  103: 			  rename $eps_f.'.'.$i, $eps_f;
  104: 			  last;
  105: 		      }
  106: 		  }
  107: 	      }  
  108: 	  }
  109:       }
  110:   }
  111:   #print "$texfile\n"; #name of the tex file for debugging only
  112:   if (-e $texfile) {
  113:       $texfile =~ m/^(.*)\/([^\/]+)$/; 
  114:       my $name_file = $2;
  115:       my $path_file = $1.'/';
  116:       chdir $path_file;
  117:       system("latex $name_file 1>/dev/null 2>/dev/null");
  118:       if ($tableofcontents eq 'yes') {
  119: 	  system("latex $name_file 1>/dev/null 2>/dev/null");
  120:       } #to create table of contents
  121:       my $idxname=$name_file;
  122:       $idxname=~s/\.tex$/\.idx/;
  123:       if ($tableofindex eq 'yes') {
  124: 	  system("makeindex $idxname");
  125: 	  system("latex $name_file 1>/dev/null 2>/dev/null");
  126:       } #to create index
  127:       #Do we have a latex error in the log file?
  128:       my $logfilename = $texfile;
  129:       $logfilename =~ s/\.tex$/\.log/;
  130:       my $temporary_file=IO::File->new($logfilename) || die "Couldn't open file for reading: $!\n";
  131:       my @content_of_file = <$temporary_file>;
  132:       close $temporary_file; 
  133:       my $body_log_file = join(' ',@content_of_file);
  134:       $logfilename =~ s/\.log$/\.html/;
  135:       $temporary_file = IO::File->new('>'.$logfilename); 
  136:       print $temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_log_file.'</pre></body></html>'."\n";
  137:       if ($body_log_file=~m/!\s+Emergency stop/) {
  138: 	  #LaTeX failed to parse tex file 
  139: 	  print "<h2>LaTeX could not successfully parse your tex file.</h2>";
  140: 	  print "It probably has errors in it.<br />";
  141: 	  my $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART';
  142: 	  my $whereitends = rindex $body_log_file,'STAMPOFPASSEDRESOURCEEND';
  143: 	  if ($whereitbegins!=-1 and $whereitends!=-1) {
  144: 	      print "With very high probability this error occured in ".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)."<br /><br />";
  145: 	  }
  146: 	  print "Here are the error messages in the LaTeX log file</br><br />";
  147: 	  my $sygnal = 0;
  148: 	  for (my $i=0;$i<=$#content_of_file;$i++) {
  149: 	      if ($content_of_file[$i]=~m/^Runaway argument?/ or $content_of_file[$i]=~m/^!/) {
  150: 		  $sygnal = 1;
  151: 	      } 
  152: 	      if ($content_of_file[$i]=~m/Here is how much of/) {
  153: 		  $sygnal = 0;
  154: 	      } 
  155: 	      if ($sygnal) {
  156: 		  print "$content_of_file[$i]<br />";
  157: 	      }  
  158: 	  }
  159:           if ($advans_role) {  
  160:               print "<b><big>The link to ";
  161:               $logfilename=~s/\/home\/httpd//;
  162: 	      print "<a href=\"$logfilename\">Your log file </a></big></b>";
  163: 	      print "\n";
  164:               #link tooriginal LaTeX file (included according Michael Hamlin desire)
  165: 	      my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
  166: 	      my @tex_content_of_file = <$tex_temporary_file>;
  167: 	      close $tex_temporary_file; 
  168: 	      my $body_tex_file = join(' ',@tex_content_of_file);
  169: 	      $texfile =~ s/\.tex$/aaaaa\.html/;
  170: 	      $tex_temporary_file = IO::File->new('>'.$texfile); 
  171: 	      print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
  172: 	      print "<br /><br />";
  173: 	      print "<b><big>The link to ";
  174: 	      $texfile=~s/\/home\/httpd//;
  175: 	      print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
  176: 	      print "\n";
  177: 	  }
  178: 
  179:       } elsif ($body_log_file=~m/<inserted text>/) {
  180: 	  my $whereitbegins = index $body_log_file,'<inserted text>';
  181: 	  print "You are running LaTeX in the <b>batch mode</b>.";
  182: 	  while ($whereitbegins != -1) {
  183: 	      my $tempobegin=$whereitbegins;
  184: 	      $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART',$whereitbegins;
  185: 	      my $whereitends = index $body_log_file,'STAMPOFPASSEDRESOURCEEND',$whereitbegins;
  186: 	      print "<br />It has found an error in".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)." <br /> and corrected it.\n";
  187: 	      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";
  188: 	      $whereitbegins = index $body_log_file,'<inserted text>',$tempobegin+10;
  189: 	  }
  190: 	  $name_file =~ s/\.tex/\.dvi/;
  191: 	  my $new_name_file = $name_file;
  192: 	  $new_name_file =~ s/\.dvi/\.ps/;
  193: 	  my $comma = "dvips -Ppdf -G0 -o $new_name_file";
  194: 	  system("$comma $name_file 1>/dev/null 2>/dev/null");
  195: 	  if (-e $new_name_file) {
  196: 	      print "<h1>PDF output file (see link below)</h1>\n";
  197: 	      $new_name_file =~ m/^(.*)\./;
  198: 	      my $tempo_file = $1.'temporar.ps';
  199: 	      my $name_file = $1.'.pdf';
  200: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
  201: 		  $comma = "psnup -2 -s1.0 $new_name_file";
  202: 		  system("$comma $tempo_file 1>/dev/null 2>/dev/null"); 
  203: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  204: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
  205: 		  $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
  206: 		  system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
  207: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  208: 	      } else {
  209: 		  system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
  210: 	      }	    
  211: 	      my $texlog = $texfile;
  212: 	      my $texaux = $texfile;
  213: 	      my $texdvi = $texfile;
  214: 	      my $texps = $texfile;
  215: 	      $texlog =~ s/\.tex/\.log/;
  216: 	      $texaux =~ s/\.tex/\.aux/;
  217: 	      $texdvi =~ s/\.tex/\.dvi/;
  218: 	      $texps =~ s/\.tex/\.ps/;
  219: 	      my @garb = ($texaux,$texdvi,$texps);
  220: #	  unlink @garb;
  221: 	      unlink $duefile;
  222: 	      print "<a href=\"/prtspool/$name_file\">Your PDF document</a>";
  223: 	      if ($advans_role) {  
  224: 		  print "<br /><br />";
  225: 		  print "<b><big>The link to ";
  226: 		  $logfilename=~s/\/home\/httpd//;
  227: 		  print "<a href=\"$logfilename\">Your log file </a></big></b>";
  228: 		  print "\n";
  229: 		  #link tooriginal LaTeX file (included according Michael Hamlin desire)
  230: 		  my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
  231: 		  my @tex_content_of_file = <$tex_temporary_file>;
  232: 		  close $tex_temporary_file; 
  233: 		  my $body_tex_file = join(' ',@tex_content_of_file);
  234: 		  $texfile =~ s/\.tex$/aaaaa\.html/;
  235: 		  $tex_temporary_file = IO::File->new('>'.$texfile); 
  236: 		  print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
  237: 		  print "<br /><br />";
  238: 		  print "<b><big>The link to ";
  239: 		  $texfile=~s/\/home\/httpd//;
  240: 		  print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
  241: 		  print "\n";
  242: 	      }
  243: 	  }
  244:       } else {
  245: 	  #LaTeX successfully parsed tex file 
  246: 	  $name_file =~ s/\.tex/\.dvi/;
  247: 	  my $new_name_file = $name_file;
  248: 	  $new_name_file =~ s/\.dvi/\.ps/;
  249: 	  my $comma = "dvips -Ppdf -G0 -o $new_name_file";
  250: 	  system("$comma $name_file 1>/dev/null 2>/dev/null");
  251: 	  if (-e $new_name_file) {
  252: 	      print "<h1>Successfully created PDF output file (see link below)</h1>\n";
  253: 	      $new_name_file =~ m/^(.*)\./;
  254: 	      my $tempo_file = $1.'temporar.ps';
  255: 	      my $name_file = $1.'.pdf';
  256: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
  257: 		  $comma = "psnup -2 -s1.0 $new_name_file";
  258: 		  system("$comma $tempo_file 1>/dev/null 2>/dev/null"); 
  259: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  260: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
  261: 		  $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
  262: 		  system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
  263: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  264: 	      } else {
  265: 		  system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
  266: 	      }	    
  267: 	      my $texlog = $texfile;
  268: 	      my $texaux = $texfile;
  269: 	      my $texdvi = $texfile;
  270: 	      my $texps = $texfile;
  271: 	      $texlog =~ s/\.tex/\.log/;
  272: 	      $texaux =~ s/\.tex/\.aux/;
  273: 	      $texdvi =~ s/\.tex/\.dvi/;
  274: 	      $texps =~ s/\.tex/\.ps/;
  275: 	      my @garb = ($texlog,$texaux,$texdvi,$texps);
  276: #	  unlink @garb;
  277: 	      unlink $duefile;
  278: 	      print "<a href=\"/prtspool/$name_file\">Your PDF document</a>";
  279: 	      print "\n";
  280: 	  }
  281:       }
  282:   } else {
  283:       print "LaTeX file $texfile was not created successfully";
  284:   }
  285: 
  286: 
  287:   
  288: 
  289: 
  290: 
  291: 
  292: 

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