File:  [LON-CAPA] / loncom / interface / printout.pl
Revision 1.47: download - view: text, annotated - select for diffs
Fri Feb 13 21:17:50 2004 UTC (20 years, 4 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
Changes: 1. Status window from lonhtmlcommon.pm is used now;
         2. Link to original resource (starting point for printing) is added.
The last change can be temporary until I'll find and implement more reasonable mechanism.

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

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