File:  [LON-CAPA] / loncom / interface / printout.pl
Revision 1.54: download - view: text, annotated - select for diffs
Fri Apr 23 19:47:38 2004 UTC (20 years ago) by www
Branches: MAIN
CVS tags: HEAD
Adding missing ">", had leaking link

    1: #!/usr/bin/perl
    2: # CGI-script to run LaTeX, dvips, ps2ps, ps2pdf etc.
    3: #
    4: # $Id: printout.pl,v 1.54 2004/04/23 19:47:38 www 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 Time::Local;
   31: use LONCAPA::loncgi();
   32: use File::Path;
   33: use IO::File;
   34: use Image::Magick;
   35: use Apache::lonhtmlcommon;
   36: use Apache::loncommon;
   37: use Apache::lonlocal;
   38: 
   39: $|=1;
   40: my %origENV=%ENV;
   41: if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
   42:     print <<END;
   43: Content-type: text/html
   44: 
   45: <html>
   46: <head><title>Bad Cookie</title></head>
   47: <body>
   48: Your cookie information is incorrect.
   49: </body>
   50: </html>
   51: END
   52:     return;
   53: }
   54:  &Apache::lonlocal::get_language_handle();
   55:  &Apache::loncommon::content_type(undef,'text/html');
   56:  my $bodytag=&Apache::loncommon::bodytag('Creating PDF','','');
   57:  print $bodytag;
   58: 
   59:   my $identifier = $ENV{'QUERY_STRING'};
   60:   my $texfile = $ENV{'cgi.'.$identifier.'.file'};
   61:   my $laystyle = $ENV{'cgi.'.$identifier.'.layout'};
   62:   my $numberofcolumns = $ENV{'cgi.'.$identifier.'.numcol'};
   63:   my $selectionmade = $ENV{'cgi.'.$identifier.'.selection'};
   64:   my $tableofcontents = $ENV{'cgi.'.$identifier.'tableofcontents'};
   65:   my $tableofindex = $ENV{'cgi.'.$identifier.'tableofindex'};
   66:   my $advans_role = $ENV{'cgi.'.$identifier.'role'};
   67:   my $number_of_files = $ENV{'cgi.'.$identifier.'numberoffiles'}+1;
   68:   my $student_names = $ENV{'cgi.'.$identifier.'studentnames'};
   69:   my $backref = $ENV{'cgi.'.$identifier.'backref'};
   70: 
   71:   my $adv = $ENV{'request.role.adv'};
   72:   
   73:   my @names_pack=();
   74:   if ($student_names=~/_END_/) {  
   75:       @names_pack=split(/_ENDPERSON_/,$student_names);
   76:   }
   77:   #got what we needed reset ENV in case it is to big for system
   78:   %ENV=%origENV;
   79: 
   80:   my $figfile = $texfile;
   81:   $figfile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.dat/;
   82:   my $duefile = $texfile;
   83:   $duefile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.due/;
   84:   #do we have figures?
   85:   if (-e $figfile) {
   86:       my %done_conversion;
   87:       my $temporary_file=IO::File->new($figfile) || die "Couldn't open file for reading: $!\n";
   88:       my @content_of_file = <$temporary_file>;
   89:       close $temporary_file;  
   90:       my $noteps;
   91:       my %prog_state;
   92:       if ($adv) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Coverting Images to EPS','Picture Conversion Status',$#content_of_file,'inline');  }
   93:       foreach $not_eps (@content_of_file) {
   94: 	  chomp($not_eps);
   95: 	  if ($not_eps ne '') {
   96:               my $status_statement='EPS picture for '.$not_eps;
   97: 	      $not_eps=~s|\/\.\/|\/|g;
   98: 	      my $eps_f = $not_eps;
   99: 	      $eps_f =~ s/\.[^.]*$/\.eps/i;
  100: 	      if ($eps_f=~/\/home\/([^\/]+)\/public_html\//) {
  101:                   $eps_f=~s/\/home\/([^\/]+)\/public_html/$1/;
  102: 		  $eps_f = '/home/httpd/prtspool/'.$eps_f;
  103: 	      } else {
  104: 		  $eps_f=~m/\/home\/httpd\/html\/res\/(.+)/;
  105: 		  $eps_f = '/home/httpd/prtspool/'.$1;
  106: 	      }
  107: 	      my $path=$eps_f;
  108: 	      $path=~s/\/([^\/]+)\.eps$//;
  109: 	      File::Path::mkpath($path,0,0777);
  110: 	      my $image = Image::Magick->new;
  111: 	      $not_eps =~ s/^\s+//;
  112: 	      $not_eps =~ s/\s+$//;
  113: 	      if ( exists($done_conversion{$not_eps})) {
  114: 		  next;
  115: 	      }
  116: 	      if ($adv) {
  117: 		  my $prettyname=$not_eps;
  118: 		  $prettyname=~s|/home/([^/]+)/public_html|/priv/$1|;
  119: 		  $prettyname=~s|/home/httpd/html/|/|;
  120: 		  &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,'Converting to EPS '.$prettyname); }
  121: 	      $done_conversion{$not_eps}=1;
  122: 	      $status = $image->Read($not_eps);
  123: 	      if ($status) {print "  $status  ";}
  124: 	      $image->Set(page => '+100+200'); 
  125: 	      $status = $image->Write($eps_f);	    
  126: 	      if ($status) {print "  $status  ";}
  127:               #check is eps exist in prtspool
  128:               if(not -e $eps_f) {
  129: 		  for (my $i=0;$i<10000;$i++) {
  130: 		      if (-e $eps_f.'.'.$i) {
  131: 			  rename $eps_f.'.'.$i, $eps_f;
  132: 			  last;
  133: 		      }
  134: 		  }
  135: 	      }  
  136: 	  }
  137:       }
  138:       if ($adv) { &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); }
  139:       unlink($figfile);
  140:   }
  141:   #print "$texfile\n"; #name of the tex file for debugging only   
  142:   my @texfile=($texfile);
  143:   if ($number_of_files>1) {
  144:       @texfile=();
  145:       for (my $i=1;$i<=$number_of_files;$i++) {
  146: 	  my $new_texfile=$texfile;
  147: 	  $new_texfile=~s/\.tex/_$i\.tex/;
  148: 	  push @texfile,$new_texfile;
  149:       } 
  150:   }
  151: 
  152: my $ind=-1;
  153: my %prog_state;
  154: if ($adv) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Print Status','Class Print Status',$number_of_files,'inline'); }
  155: my $final_statement="<a href=\"$backref\"><b>Return</b></a> to last resource.<br /><br />Generated PDF File for:<br />";
  156: foreach $texfile (@texfile) {
  157:   my $status_statement='';
  158:   my $link_text='download PDF';
  159:   $ind++;
  160:   my @stud_info=split(/_END_/,$names_pack[$ind]);
  161:   my @tempo_array=split(/:/,$stud_info[0]);
  162:   my $name;
  163:   if ($tempo_array[3]) {
  164:       $name=$tempo_array[3];
  165:   } else {
  166:       $name=$tempo_array[0].'@'.$tempo_array[1];
  167:   }
  168:   $link_text='<b>'.$name.'</b> ';
  169:   $status_statement.=$name;
  170:   if ($#stud_info>0) {
  171:       @tempo_array=split(/:/,$stud_info[-1]);
  172:       if ($tempo_array[3]) {
  173: 	  $name=$tempo_array[3];
  174:       } else {
  175: 	  $name=$tempo_array[0].'@'.$tempo_array[1];
  176:       }
  177:       $link_text.='- <b>'.$name.':</b>  ';
  178:       $status_statement.=' -  '.$name;
  179:   } 
  180:   if ($adv) { &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,'Creating PDF for '.$status_statement); }
  181:   if (-e $texfile) {
  182:       $texfile =~ m/^(.*)\/([^\/]+)$/; 
  183:       my $name_file = $2;
  184:       my $path_file = $1.'/';
  185:       chdir $path_file;
  186:       system("latex $name_file 1>/dev/null 2>/dev/null");
  187:       if ($tableofcontents eq 'yes') {
  188: 	  system("latex $name_file 1>/dev/null 2>/dev/null");
  189:       } #to create table of contents
  190:       my $idxname=$name_file;
  191:       $idxname=~s/\.tex$/\.idx/;
  192:       if ($tableofindex eq 'yes') {
  193: 	  system("makeindex $idxname");
  194: 	  system("latex $name_file 1>/dev/null 2>/dev/null");
  195:       } #to create index
  196:       #Do we have a latex error in the log file?
  197:       my $logfilename = $texfile;
  198:       $logfilename =~ s/\.tex$/\.log/;
  199:       my $temporary_file=IO::File->new($logfilename) || die "Couldn't open file for reading: $!\n";
  200:       my @content_of_file = <$temporary_file>;
  201:       close $temporary_file; 
  202:       my $body_log_file = join(' ',@content_of_file);
  203:       $logfilename =~ s/\.log$/\.html/;
  204:       $temporary_file = IO::File->new('>'.$logfilename); 
  205:       print $temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_log_file.'</pre></body></html>'."\n";
  206:       if ($body_log_file=~m/!\s+Emergency stop/) {
  207: 	  #LaTeX failed to parse tex file 
  208: 	  print "<h2>LaTeX could not successfully parse your tex file.</h2>";
  209: 	  print "It probably has errors in it.<br />";
  210: 	  my $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART';
  211: 	  my $whereitends = rindex $body_log_file,'STAMPOFPASSEDRESOURCEEND';
  212: 	  if ($whereitbegins!=-1 and $whereitends!=-1) {
  213:  	      print "With very high probability this error occured in ".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)."<br /><br />";
  214: 	  }
  215: 	  print "Here are the error messages in the LaTeX log file</br><br />";
  216: 	  my $sygnal = 0;
  217: 	  for (my $i=0;$i<=$#content_of_file;$i++) {
  218: 	      if ($content_of_file[$i]=~m/^Runaway argument?/ or $content_of_file[$i]=~m/^!/) {
  219: 		  $sygnal = 1;
  220: 	      } 
  221: 	      if ($content_of_file[$i]=~m/Here is how much of/) {
  222: 		  $sygnal = 0;
  223: 	      } 
  224: 	      if ($sygnal) {
  225: 		  print "$content_of_file[$i]<br />";
  226: 	      }  
  227: 	  }
  228:           if ($advans_role) {  
  229:               print "<b><big>The link to ";
  230:               $logfilename=~s/\/home\/httpd//;
  231: 	      print "<a href=\"$logfilename\">Your log file </a></big></b>";
  232: 	      print "\n";
  233:               #link tooriginal LaTeX file (included according Michael Hamlin desire)
  234: 	      my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
  235: 	      my @tex_content_of_file = <$tex_temporary_file>;
  236: 	      close $tex_temporary_file; 
  237: 	      my $body_tex_file = join(' ',@tex_content_of_file);
  238: 	      $texfile =~ s/\.tex$/aaaaa\.html/;
  239: 	      $tex_temporary_file = IO::File->new('>'.$texfile); 
  240: 	      print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
  241: 	      print "<br /><br />";
  242: 	      print "<b><big>The link to ";
  243: 	      $texfile=~s/\/home\/httpd//;
  244: 	      print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
  245: 	      print "\n";
  246: 	  }
  247: 
  248:       } elsif ($body_log_file=~m/<inserted text>/) {
  249: 	  my $whereitbegins = index $body_log_file,'<inserted text>';
  250: 	  print "You are running LaTeX in the <b>batch mode</b>.";
  251: 	  while ($whereitbegins != -1) {
  252: 	      my $tempobegin=$whereitbegins;
  253: 	      $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART',$whereitbegins;
  254: 	      my $whereitends = index $body_log_file,'STAMPOFPASSEDRESOURCEEND',$whereitbegins;
  255: 	      print "<br />It has found an error in".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)." <br /> and corrected it.\n";
  256: 	      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";
  257: 	      $whereitbegins = index $body_log_file,'<inserted text>',$tempobegin+10;
  258: 	  }
  259: 	  $name_file =~ s/\.tex/\.dvi/;
  260: 	  my $new_name_file = $name_file;
  261: 	  $new_name_file =~ s/\.dvi/\.ps/;
  262: 	  my $comma = "dvips -Ppdf -G0 -o $new_name_file";
  263: 	  system("$comma $name_file 1>/dev/null 2>/dev/null");
  264: 	  if (-e $new_name_file) {
  265: 	      print "<h1>PDF output file (see link below)</h1>\n";
  266: 	      $new_name_file =~ m/^(.*)\./;
  267: 	      my $tempo_file = $1.'temporar.ps';
  268: 	      my $name_file = $1.'.pdf';
  269: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
  270: 		  $comma = "psnup -2 -s1.0 $new_name_file";
  271: 		  system("$comma $tempo_file 1>/dev/null 2>/dev/null"); 
  272: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  273: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
  274: 		  $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
  275: 		  system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
  276: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  277: 	      } else {
  278: 		  system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
  279: 	      }	    
  280: 	      my $texlog = $texfile;
  281: 	      my $texaux = $texfile;
  282: 	      my $texdvi = $texfile;
  283: 	      my $texps = $texfile;
  284: 	      $texlog =~ s/\.tex/\.log/;
  285: 	      $texaux =~ s/\.tex/\.aux/;
  286: 	      $texdvi =~ s/\.tex/\.dvi/;
  287: 	      $texps =~ s/\.tex/\.ps/;
  288: 	      my @garb = ($texaux,$texdvi,$texps);
  289: #	  unlink @garb;
  290: 	      unlink $duefile;
  291: 	      print "<a href=\"/prtspool/$name_file\">Your PDF document</a>";
  292: 	      if ($advans_role) {  
  293: 		  print "<br /><br />";
  294: 		  print "<b><big>The link to ";
  295: 		  $logfilename=~s/\/home\/httpd//;
  296: 		  print "<a href=\"$logfilename\">Your log file </a></big></b>";
  297: 		  print "\n";
  298: 		  #link tooriginal LaTeX file (included according Michael Hamlin desire)
  299: 		  my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
  300: 		  my @tex_content_of_file = <$tex_temporary_file>;
  301: 		  close $tex_temporary_file; 
  302: 		  my $body_tex_file = join(' ',@tex_content_of_file);
  303: 		  $texfile =~ s/\.tex$/aaaaa\.html/;
  304: 		  $tex_temporary_file = IO::File->new('>'.$texfile); 
  305: 		  print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
  306: 		  print "<br /><br />";
  307: 		  print "<b><big>The link to ";
  308: 		  $texfile=~s/\/home\/httpd//;
  309: 		  print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
  310: 		  print "\n";
  311: 	      }
  312: 	  }
  313:       } else {
  314: 	  #LaTeX successfully parsed tex file 
  315: 	  $name_file =~ s/\.tex/\.dvi/;
  316: 	  my $new_name_file = $name_file;
  317: 	  $new_name_file =~ s/\.dvi/\.ps/;
  318: 	  my $comma = "dvips -Ppdf -G0 -o $new_name_file";
  319: 	  system("$comma $name_file 1>/dev/null 2>/dev/null");
  320: 	  if (-e $new_name_file) {
  321: 	      print "<br />$final_statement ";
  322: 	      $final_statement='';
  323: 	      $new_name_file =~ m/^(.*)\./;
  324: 	      my $tempo_file = $1.'temporar.ps';
  325: 	      my $name_file = $1.'.pdf';
  326: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
  327: 		  $comma = "psnup -2 -s1.0 $new_name_file";
  328: 		  system("$comma $tempo_file 1>/dev/null 2>/dev/null"); 
  329: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  330: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
  331: 		  $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
  332: 		  system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
  333: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  334: 	      } else {
  335: 		  system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
  336: 	      }	    
  337: 	      my $texlog = $texfile;
  338: 	      my $texaux = $texfile;
  339: 	      my $texdvi = $texfile;
  340: 	      my $texps = $texfile;
  341: 	      $texlog =~ s/\.tex/\.log/;
  342: 	      $texaux =~ s/\.tex/\.aux/;
  343: 	      $texdvi =~ s/\.tex/\.dvi/;
  344: 	      $texps =~ s/\.tex/\.ps/;
  345: 	      my @garb = ($texlog,$texaux,$texdvi,$texps);
  346: #	  unlink @garb;
  347: 	      unlink $duefile;
  348: 	      print "<a href=\"/prtspool/$name_file\">$link_text</a>";
  349: 	      print "\n";
  350: 	  }
  351:       }
  352:   } else {
  353:       print "LaTeX file $texfile was not created successfully";
  354:   }
  355: }
  356: print "<br />";
  357: if ($number_of_files>1) {
  358:     my $zipfile=$texfile[0];
  359:     $zipfile=~s/\.tex/\.zip/;
  360:     my $statement="zip $zipfile";
  361:     foreach my $file (@texfile) {
  362: 	$file=~s/\.tex/.\pdf/;
  363: 	$statement.=' '.$file; 
  364:     }
  365:     print("<pre>Zip Output:\n");
  366:     system($statement);
  367:     print("</pre>");
  368:     $zipfile=~s/\/home\/httpd//;
  369:     print "<br /> A <a href=\"$zipfile\">ZIP file</a> of all the PDFs.";
  370: }
  371: if ($adv) { &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); }
  372: 
  373: 
  374: 
  375: 
  376: 

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