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

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

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