Annotation of loncom/interface/printout.pl, revision 1.50

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

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