File:  [LON-CAPA] / loncom / interface / printout.pl
Revision 1.99: download - view: text, annotated - select for diffs
Tue Jun 6 11:04:18 2006 UTC (18 years, 1 month ago) by foxr
Branches: MAIN
CVS tags: HEAD
strip the STARTOFSTUDENT/ENDOFSTUDENT stamps from the initial postscript
This is an intermediate development stage of request 4786... later
this will be extended to count the pages between each of these stamps
and subsequently ensure that the output for all student assignments in
a single printout will be the same number of page/columns.

    1: #!/usr/bin/perl
    2: # CGI-script to run LaTeX, dvips, ps2ps, ps2pdf etc.
    3: #
    4: #
    5: # Copyright Michigan State University Board of Trustees
    6: #
    7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    8: #
    9: # LON-CAPA is free software; you can redistribute it and/or modify
   10: # it under the terms of the GNU General Public License as published by
   11: # the Free Software Foundation; either version 2 of the License, or
   12: # (at your option) any later version.
   13: #
   14: # LON-CAPA is distributed in the hope that it will be useful,
   15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17: # GNU General Public License for more details.
   18: #
   19: # You should have received a copy of the GNU General Public License
   20: # along with LON-CAPA; if not, write to the Free Software
   21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   22: #
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: 
   28: use lib '/home/httpd/lib/perl';
   29: use LONCAPA::loncgi;
   30: use File::Path;
   31: use File::Basename;
   32: use IO::File;
   33: use Image::Magick;
   34: use Apache::lonhtmlcommon();
   35: use Apache::lonnet;
   36: use Apache::loncommon();
   37: use Apache::lonlocal;
   38: use Apache::lonmsg();
   39: use LONCAPA::Enrollment;
   40: 
   41: use strict;
   42: 
   43: 
   44: #   Determine if a user is operating as a student for this course/domain.
   45: #Parameters:
   46: #    none
   47: #Implicit:
   48: #    $env{request.role} contains the role under which this user operated this
   49: #                       this request.
   50: sub is_student {
   51:     return ($env{'request.role'}=~/^st\./);
   52: }
   53: 
   54: #
   55: #   Debugging:  Dump the environment for debugging.
   56: #
   57: sub dumpenv  {
   58:     print "<br />-------------------<br />";
   59:     foreach my $key (sort (keys %env)) {
   60: 	print "<br />$key -> $env{$key}";
   61:     }
   62:     print "<br />-------------------<br />";
   63: }
   64: 
   65: #
   66: #   This sub sends a message to the appropriate person if there was an error
   67: #   rendering the latex  At present, there's only one case to consider:
   68: #   a student printing inside a course results in messages to the course coordinator.
   69: #Parmaeters:
   70: #    identifier -  The unique identifier of this cgi request.
   71: #    badresource-  Filepath to most likely failing 
   72: #    logfile    -  The contents of the log file (included in the message).
   73: #    texfile    -  reference to an array containing the LaTeX input file
   74: #                  (included in the message).
   75: #Implicit inputs:
   76: #   From the environment:
   77: #       cgi.$identifier.user     - User doing the printing.
   78: #       cgi.$identifier.domain   - Domain the user is logged in on with printing.
   79: #       cgi.$identifier.courseid - Id of the course (if this is a course).
   80: #       cgi.$identifier.coursedom- Domain in which course was constituted.
   81: #       cgi.$identifier.resources - List of resource URL's for which the print
   82: #                                  was attempted.
   83: # 
   84: sub send_error_mail {
   85:     my ($identifier, $badresource, $logfile, $texfile) = @_;
   86:     my $user     = $env{"cgi.$identifier.user"};
   87:     my $domain   = $env{"cgi.$identifier.domain"};
   88:     my $courseid = $env{"cgi.$identifier.courseid"};
   89:     my $coursedom= $env{"cgi.$identifier.coursedom"};
   90:     my $resources= $env{"cgi.$identifier.resources"};
   91: 
   92:     #  resource file->URL
   93:     #
   94:     my $badurl = &Apache::lonnet::declutter($badresource);
   95: 
   96:     # &dumpenv();
   97: 
   98: 
   99: 
  100:     #  Only continue if there is a user, domain, courseid, course domain
  101:     #  and resources:
  102: 
  103:     if(defined($user)       && defined($domain) && defined($courseid) &&
  104:        defined($coursedom)  && defined($resources) ){
  105: 	   
  106: 	#  Only mail if the conditions are ripe for it:
  107: 	#  The user is a student in the course:
  108: 	#
  109: 	
  110: 	if (&is_student()) {
  111: 	    # build the subject and message body:
  112: 	    # print "sending message to course coordinators.<br />";
  113: 
  114: 	    # Todo: Convert badurl into a url from file path:
  115: 
  116: 	    my $subject  = "Error [$badurl] Print failed for $user".'@'.$domain;
  117: 	    my $message .= "Print failed to render LaTeX for $user".'@'."$domain\n";
  118: 	    $message    .= "  User was attempting to print: \n";
  119: 	    foreach my $resource (split(/:/,$resources)) {
  120: 		$message    .= "       $resource\n";
  121: 	    }
  122: 	    $message    .= "--------------------LaTeX logfile:------------ \n";
  123: 	    $message    .= $logfile;
  124: 	    $message    .= "-----------------LaTeX source file: ------------\n";
  125: 	    
  126: 	    foreach my $line (@$texfile) {
  127: 		$message .= "$line\n";
  128: 	    }
  129: 	    my (undef, %receivers) = &Apache::lonfeedback::decide_receiver(undef, 0,
  130: 									  1,1,1);
  131: 	    # print "<br /> sending...section:  $env{'request.course.sec'}";
  132: 	    foreach my $dest (keys %receivers) {
  133: 		# print "<br /> dest is $dest";
  134: 		my @destinfo = split(/:/,$dest);
  135: 		my $user = $destinfo[0];
  136: 		my $dom  = $destinfo[1];
  137: 
  138: 		&Apache::lonmsg::user_normal_msg($user, $dom,
  139: 						 $subject, $message);
  140: 		
  141: 		# No point in looking at the return status as there's no good
  142: 		# error action I can think of right now (log maybe?).
  143: 	    }
  144: 	}
  145:     }
  146: }
  147: 
  148: $|=1;
  149: if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
  150:     print <<END;
  151: Content-type: text/html
  152: 
  153: <html>
  154: <head><title>Bad Cookie</title></head>
  155: <body>
  156: Your cookie information is incorrect.
  157: </body>
  158: </html>
  159: END
  160:     return;
  161: }
  162:  &Apache::lonlocal::get_language_handle();
  163:  &Apache::loncommon::content_type(undef,'text/html');
  164:  print(&Apache::loncommon::start_page('Creating PDF'));
  165: 
  166:   my $identifier = $ENV{'QUERY_STRING'};
  167:   my $texfile = $env{'cgi.'.$identifier.'.file'};
  168:   my $laystyle = $env{'cgi.'.$identifier.'.layout'};
  169:   my $numberofcolumns = $env{'cgi.'.$identifier.'.numcol'};
  170:   my $paper = $env{'cgi.'.$identifier.'.paper'};
  171:   my $selectionmade = $env{'cgi.'.$identifier.'.selection'};
  172:   my $tableofcontents = $env{'cgi.'.$identifier.'.tableofcontents'};
  173:   my $tableofindex = $env{'cgi.'.$identifier.'.tableofindex'};
  174:   my $advanced_role = $env{'cgi.'.$identifier.'.role'};
  175:   my $number_of_files = $env{'cgi.'.$identifier.'.numberoffiles'}+1;
  176:   my $student_names = $env{'cgi.'.$identifier.'.studentnames'};
  177:   my $backref = &Apache::lonnet::unescape($env{'cgi.'.$identifier.'.backref'});
  178: 
  179:   
  180:   my @names_pack=();
  181:   if ($student_names=~/_END_/) {  
  182:       @names_pack=split(/_ENDPERSON_/,$student_names);
  183:   }
  184: 
  185: print "<a href=\"$backref\"><b>Return</b></a> to last resource.<br /><br />";
  186: 
  187:   my $figfile = $texfile;
  188:   $figfile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.dat/;
  189:   my $duefile = $texfile;
  190:   $duefile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.due/;
  191:   #do we have figures?
  192:   # print "Figure file: $figfile\n";
  193:   if (-e $figfile) {
  194:       # print "$figfile exists\n";
  195:       my %done_conversion;
  196:       my $temporary_file=IO::File->new($figfile) || die "Couldn't open fig file $figfile for reading: $!\n";
  197:       my @content_of_file = <$temporary_file>;
  198:       close $temporary_file;  
  199:       my $noteps;
  200:       my %prog_state;
  201:       if ($advanced_role) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Coverting Images to EPS','Picture Conversion Status',$#content_of_file,'inline','80');  }
  202:       print('<br />');
  203:       foreach my $not_eps (@content_of_file) {
  204: 	  chomp($not_eps);
  205: 	  if ($not_eps ne '') {
  206: 	      # print "Converting $not_eps"; # Debugging.
  207:               my $status_statement='EPS picture for '.$not_eps;
  208: 	      # print "$status_statement\n";
  209: 	      $not_eps=~s|\/\.\/|\/|g;
  210: 	      my $eps_f = $not_eps;
  211: 	      # $eps_f =~ s/\.[^.]*$/\.eps/i;
  212: 	      $eps_f .= '.eps';	# Just append the eps ext.
  213: 	      if ($eps_f=~/\/home\/([^\/]+)\/public_html\//) {
  214:                   $eps_f=~s/\/home\/([^\/]+)\/public_html/$1/;
  215: 		  $eps_f = '/home/httpd/prtspool/'.$eps_f;
  216: 	      } elsif ($eps_f=~/$Apache::lonnet::perlvar{'lonDocRoot'}\/res\//) {
  217: 		  $eps_f=~m/$Apache::lonnet::perlvar{'lonDocRoot'}\/res\/(.+)/;
  218: 		  $eps_f = '/home/httpd/prtspool/'.$1;
  219: 	      } elsif ($eps_f=~/$Apache::lonnet::perlvar{'lonUsersDir'}\//) {
  220: 		  $eps_f=~/$Apache::lonnet::perlvar{'lonUsersDir'}\/([^\/]+)\/\w\/\w\/\w\/(.+)/;
  221: 		  $eps_f = '/home/httpd/prtspool/'.$1.'/'.$2;
  222: 	      }
  223: 	      $eps_f  =~ s/ /\_/g; # Spaces are problematic for system commands and LaTeX.
  224: 	      my $path=$eps_f;
  225: 	      $path =~ s/\/([^\/]+)\.eps$//;
  226: 	      # print "Final file path: $path "; # Debugging
  227: 	      File::Path::mkpath($path,0,0777);
  228: 	      $not_eps =~ s/^\s+//;
  229: 	      $not_eps =~ s/\s+$//;
  230: 	      $not_eps =~ s/ /\\ /g;
  231: 	      if ( exists($done_conversion{$not_eps})) { next; }
  232: 	      if ($advanced_role) {
  233: 		  my $prettyname=$not_eps;
  234: 		  $prettyname=~s|/home/([^/]+)/public_html|/priv/$1|;
  235: 		  $prettyname=~s|$Apache::lonnet::perlvar{'lonDocRoot'}/|/|;
  236: 		  &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,'Converting to EPS '.$prettyname);
  237: 	      }
  238: 	      $done_conversion{$not_eps}=1;
  239: 	      # print "Converting $not_eps -> $eps_f"; # Debugging
  240: 	      system("convert $not_eps $eps_f");
  241:               # check is eps exist in prtspool
  242:               if (not -e $eps_f) {
  243: 		  # converting an animated gif creates either:
  244:                   # anim.gif.eps.0
  245:                   # or
  246:                   # anim.gif-0.eps
  247: 		  for (my $i=0;$i<10000;$i++) {
  248: 		      if (-e $eps_f.'.'.$i) {
  249: 			  rename($eps_f.'.'.$i, $eps_f);
  250: 			  last;
  251: 		      }
  252: 		      my $anim_eps = $eps_f;
  253: 		      $anim_eps =~ s/(\.[^.]*)\.eps$/$1-$i\.eps/i;
  254: 		      if (-e $anim_eps) {
  255: 			  rename($anim_eps, $eps_f);
  256: 			  last;
  257: 		      }
  258: 		  }
  259: 	      }
  260: 	      # imagemagick 6.2.0-6.2.7 fails to properly handle
  261:               # convert anim.gif anim.gif.eps
  262:               # it creates anim.eps instead. 
  263:               if (not -e $eps_f) {
  264: 		  my $eps_f2 = $eps_f;
  265: 		  $eps_f2 =~ s/\.[^.]*\.eps$/\.eps/i;
  266: 		  if(-e $eps_f2) {
  267: 		      rename($eps_f2,$eps_f);
  268: 		  }
  269: 	      }
  270: 
  271: 	  }
  272:       }
  273:       if ($advanced_role) { 
  274: 	  &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); 
  275:       }
  276:       unlink($figfile);
  277:   }
  278:   #print "$texfile\n"; #name of the tex file for debugging only   
  279:   my @texfile=($texfile);
  280:   if ($number_of_files>1) {
  281:       @texfile=();
  282:       for (my $i=1;$i<=$number_of_files;$i++) {
  283: 	  my $new_texfile=$texfile;
  284: 	  $new_texfile=~s/\.tex//;
  285: 	  $new_texfile = sprintf("%s_%03d.tex", $new_texfile,$i);
  286: 	  push @texfile,$new_texfile;
  287:       } 
  288:   }
  289: 
  290: my $ind=-1;
  291: my %prog_state;
  292: if ($advanced_role) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Print Status','Class Print Status',$number_of_files,'inline','80'); }
  293: print "<br />";
  294: my $num_files = @texfile;
  295: foreach $texfile (@texfile) {
  296:   my $status_statement='';
  297:   my $link_text='download PDF';
  298:   $ind++;
  299:   my @stud_info=split(/_END_/,$names_pack[$ind]);
  300:   my @tempo_array=split(/:/,$stud_info[0]);
  301:   my $name;
  302:   my $name_range='';
  303:   if ($tempo_array[3]) {
  304:       $name=$tempo_array[3];
  305:       ($name_range) = split(/,/,$name, 2);
  306:   } else {
  307:       $name=$tempo_array[0].'@'.$tempo_array[1];
  308:       $name_range = $tempo_array[0];
  309:   }
  310:   if (($name ne "") && ($name ne '@') ) { # Could be printing codes...
  311:       $link_text='<b>'.$name.'</b>';
  312:       $status_statement.=$name;
  313:   }
  314:   if ($#stud_info>0) {
  315:       @tempo_array=split(/:/,$stud_info[-1]);
  316:       if ($tempo_array[3]) {
  317: 	  $name=$tempo_array[3];
  318: 	  my ($lastname) = split(/,/, $name,2);
  319: 	  $name_range .= "-".$lastname;
  320:       } else {
  321: 	  $name=$tempo_array[0].'@'.$tempo_array[1];
  322: 	  $name_range .= '-'.$tempo_array[0];
  323:       }
  324:       if (($name ne "") && ($name ne '@')) {
  325: 	  $link_text.=' - <b>'.$name.'</b>';
  326: 	  $status_statement.=' -  '.$name;
  327:   
  328:       }
  329:   }
  330:   if(($num_files > 1) && ($link_text eq 'download PDF')) { # Printing codes
  331:       $link_text = '<b>'.basename($texfile,'.tex').'.pdf</b>';
  332:       $status_statement .= basename($texfile);
  333:   }
  334:   $name_range =~ s/'//g;	# O'Neil -> ONeil e.g.
  335:   print "<br/>";
  336:   if ($advanced_role) { &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,'Creating PDF for '.$status_statement); }
  337:   #  This little piece of dirt puts username ranges into the original tex
  338:   #  Tex filename from which they'll propagate into the other filenames as well.
  339:   #
  340:   if (-e $texfile) {
  341:       if (($name_range ne '') && ($num_files > 1)) {
  342: 	  my $newtexfile = $texfile;
  343: 	  $newtexfile    =~ s/\.tex/$name_range\.tex/;
  344: 	  rename($texfile, $newtexfile);
  345: 	  $texfile       = $newtexfile;
  346:       }
  347:       $texfile =~ m/^(.*)\/([^\/]+)$/; 
  348:       my $name_file = $2;
  349:       my $path_file = $1.'/';
  350:       chdir $path_file;
  351:       my $dvi_file= $name_file; $dvi_file =~ s/\.tex/$name_range\.dvi/;
  352:       &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
  353: 			 "for $status_statement now LaTeXing file",
  354: 			 \%prog_state,$dvi_file);
  355:       if ($tableofcontents eq 'yes') {
  356:       &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
  357: 			 "for $status_statement First LaTeX of file for table of contents",
  358: 			 \%prog_state,$dvi_file);
  359:       &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
  360: 			 "for $status_statement Second LaTeX of file for table of contents",
  361: 			 \%prog_state,$dvi_file);
  362:       } #to create table of contents
  363:       my $idxname=$name_file;
  364:       $idxname=~s/\.tex$/\.idx/;
  365:       if ($tableofindex eq 'yes') {
  366: 	  &busy_wait_command("makeindex $idxname",
  367: 			     "making index file",
  368: 			     \%prog_state,$idxname);
  369: 	  &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
  370: 			     "for $status_statement now LaTeXing file for index section",
  371: 			     \%prog_state,$dvi_file);
  372:       } #to create index
  373:       #Do we have a latex error in the log file?
  374:       my $logfilename = $texfile; $logfilename =~ s/\.tex$/\.log/;
  375:       my $temporary_file=IO::File->new($logfilename) || die "Couldn't open log file $logfilename for reading: $!\n";
  376:       my @content_of_file = <$temporary_file>;
  377:       close $temporary_file; 
  378:       my $body_log_file = join(' ',@content_of_file);
  379:       $logfilename =~ s/\.log$/\.html/;
  380:       $temporary_file = IO::File->new('>'.$logfilename); 
  381:       print $temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_log_file.'</pre></body></html>'."\n";
  382:       if ($body_log_file=~m/!\s+Emergency stop/) {
  383: 	  my $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART';
  384: 	  my $whereitends = rindex $body_log_file,'STAMPOFPASSEDRESOURCEEND';
  385: 	  my $badresource;
  386: 	  my $badtext;
  387: 	  if ($whereitbegins!=-1 and $whereitends!=-1) {
  388: 	      $badtext = substr($body_log_file,$whereitbegins+26, $whereitends-$whereitbegins-26);
  389: 	      $whereitbegins  = rindex $badtext,'located in';
  390: 	      if ($whereitbegins != -1) {
  391: 		  
  392: 		  $badresource = substr($badtext, $whereitbegins+27, 
  393: 					length($badtext) - $whereitbegins - 48);
  394: 		  # print "<br />failing resourcename: $badresource<br />";
  395: 	      }
  396: 	  }
  397: 	  
  398:           if ($advanced_role) {  
  399: 	      #LaTeX failed to parse tex file 
  400: 	      print "<h2>LaTeX could not successfully parse your tex file.</h2>";
  401: 	      print "It probably has errors in it.<br />";
  402: 	      print "With very high probability this error occured in ".$badtext."<br /><br />";
  403: 	      print "Here are the error messages in the LaTeX log file<br /><pre>";
  404: 
  405: 	      my $sygnal = 0;
  406: 	      for (my $i=0;$i<=$#content_of_file;$i++) {
  407: 		  if ($content_of_file[$i]=~m/^Runaway argument?/ or $content_of_file[$i]=~m/^!/) {
  408: 		      $sygnal = 1;
  409: 		  } 
  410: 		  if ($content_of_file[$i]=~m/Here is how much of/) {
  411: 		      $sygnal = 0;
  412: 		  } 
  413: 		  if ($sygnal) {
  414: 		      print "$content_of_file[$i]";
  415: 		  }  
  416: 	      }
  417: 	      print "</pre>\n";
  418: 	      # print "<br /> Advanced role <br />";
  419:               print "<b><big>The link to ";
  420:               $logfilename=~s/\/home\/httpd//;
  421: 	      print "<a href=\"$logfilename\">Your log file </a></big></b>";
  422: 	      print "\n";
  423:               #link tooriginal LaTeX file (included according Michael Hamlin desire)
  424: 	      my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open tex file $texfile for reading: $!\n";
  425: 	      my @tex_content_of_file = <$tex_temporary_file>;
  426: 	      close $tex_temporary_file; 
  427: 	      my $body_tex_file = join(' ',@tex_content_of_file);
  428: 	      $texfile =~ s/\.tex$/aaaaa\.html/;
  429: 	      $tex_temporary_file = IO::File->new('>'.$texfile); 
  430: 	      print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
  431: 	      print "<br /><br />";
  432: 	      print "<b><big>The link to ";
  433: 	      $texfile=~s/\/home\/httpd//;
  434: 	      print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
  435: 	      print "\n";
  436: 	      my $help_text = &Apache::loncommon::help_open_topic("Print_Resource", "Help on printing");
  437: 	      print ("$help_text");
  438: 
  439: 	  } else {		# Student role...
  440: 	      #  at this point:
  441: 	      #    $body_log_file - contains the log file.
  442:               #    $name_file     - is the name of the LaTeX file.
  443:               #    $identifier    - is the unique LaTeX identifier.l
  444: 
  445: 	      print "<br />There are errors in $badtext";
  446: 	      print "<br />These errors prevent this resource from printing correctly";
  447: 	      my $tex_handle = IO::File->new($name_file);
  448: 	      my @tex_contents = <$tex_handle>;
  449: 	      &send_error_mail($identifier, $badresource, $body_log_file, \@tex_contents);
  450: 	      print "<br />A message has been sent to the instructor describing this failure<br />";
  451: 	      my $help_text = &Apache::loncommon::help_open_topic("Print_Resource", "Help on printing");
  452: 	      print  ("$help_text");
  453: 
  454: 	  }
  455: 
  456:       } elsif ($body_log_file=~m/<inserted text>/) {
  457: 	  my $whereitbegins = index $body_log_file,'<inserted text>';
  458: 	  print "You are running LaTeX in <b>batch mode</b>.";
  459: 	  while ($whereitbegins != -1) {
  460: 	      my $tempobegin=$whereitbegins;
  461: 	      $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART',$whereitbegins;
  462: 	      my $whereitends = index $body_log_file,'STAMPOFPASSEDRESOURCEEND',$whereitbegins;
  463: 	      print "<br />It has found an error in".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)." <br /> and corrected it.\n";
  464: 	      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";
  465: 	      $whereitbegins = index $body_log_file,'<inserted text>',$tempobegin+10;
  466: 	  }
  467: 	  $name_file =~ s/\.tex/\.dvi/;
  468: 	  my $new_name_file = $name_file;
  469: 	  $new_name_file =~ s/\.dvi/\.ps/;
  470: 	  my $papera=$paper;
  471: 	  if ($papera eq 'letter') {$papera='';}
  472: 	  if ($papera ne '') {$papera='-t'.$papera;}
  473: 	  my $comma = "dvips $papera -Ppdf -G0 -o $new_name_file";
  474: 	  &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
  475: 			     "for $status_statement now Converting to PS",
  476: 			     \%prog_state,$new_name_file);
  477: 	  if (-e $new_name_file) {
  478: 	      &repaginate_postscript($new_name_file);
  479: 	      print "<h1>PDF output file (see link below)</h1>\n";
  480: 	      $new_name_file =~ m/^(.*)\./;
  481: 	      my $ps_file = my $tempo_file = $1.'temporar.ps';
  482: 	      my $pdf_file = $1.'.pdf';
  483: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
  484: 		  my $papera=$paper;
  485:                   if ($papera eq 'letter') {$papera='';}
  486: 		  if ($papera ne '') {$papera='-p'.$papera;}
  487: 		  $comma = "psnup $papera -2 -s1.0 $new_name_file";
  488: 		  &busy_wait_command("$comma $tempo_file 1>/dev/null 2>/dev/null",
  489: 				     "for $status_statement now Modifying PS layout",
  490: 				     \%prog_state,$tempo_file); 
  491: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
  492: 		  my $papera=$paper;
  493:                   if ($papera eq 'letter') {$papera='';}
  494: 		  if ($papera ne '') {$papera='-p'.$papera;}
  495: 		  $comma = 'pstops '.$papera.' "2:0+1(0.48w,0)"';
  496: 		  &busy_wait_command("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null",
  497: 				     "for $status_statement now Modifying PS layout",
  498: 				     \%prog_state,$tempo_file); 
  499: 
  500: 	      } else {
  501: 		  $ps_file=$new_name_file;
  502: 	      }	    
  503: 	      &busy_wait_command("ps2pdf $ps_file $pdf_file 1>/dev/null 2>/dev/null",
  504: 				 "for $status_statement now Converting PS to PDF",
  505: 				 \%prog_state, $pdf_file);
  506: 
  507: 	      my $texlog = $texfile;
  508: 	      my $texaux = $texfile;
  509: 	      my $texdvi = $texfile;
  510: 	      my $texps = $texfile;
  511: 	      $texlog =~ s/\.tex/\.log/;
  512: 	      $texaux =~ s/\.tex/\.aux/;
  513: 	      $texdvi =~ s/\.tex/\.dvi/;
  514: 	      $texps =~ s/\.tex/\.ps/;
  515: 	      my @garb = ($texaux,$texdvi,$texps);
  516: #	  unlink @garb;
  517: 	      unlink $duefile;
  518: 	      print "<a href=\"/prtspool/$pdf_file\">Your PDF document</a>";
  519: 	  }
  520: 	  if ($advanced_role) {  
  521: 	      print "<br /><br />";
  522: 	      print "<b><big>The link to ";
  523: 	      $logfilename=~s/\/home\/httpd//;
  524: 	      print "<a href=\"$logfilename\">Your log file </a></big></b>";
  525: 	      print "\n";
  526: 	      #link tooriginal LaTeX file (included according Michael Hamlin desire)
  527: 	      my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open tex file $texfile for reading: $!\n";
  528: 	      my @tex_content_of_file = <$tex_temporary_file>;
  529: 	      close $tex_temporary_file; 
  530: 	      my $body_tex_file = join(' ',@tex_content_of_file);
  531: 	      $texfile =~ s/\.tex$/aaaaa\.html/;
  532: 	      $tex_temporary_file = IO::File->new('>'.$texfile); 
  533: 	      print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
  534: 	      print "<br /><br />";
  535: 	      print "<b><big>The link to ";
  536: 	      $texfile=~s/\/home\/httpd//;
  537: 	      print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
  538: 	      print "\n";
  539: 	  }
  540:       } else {
  541: 	  #LaTeX successfully parsed tex file 
  542: 	  $name_file =~ s/\.tex/\.dvi/;
  543: 	  my $new_name_file = $name_file;
  544: 	  $new_name_file =~ s/\.dvi/\.ps/;
  545: 	  my $papera=$paper;
  546: 	  if ($papera eq 'letter') {$papera='';}
  547: 	  if ($papera ne '') {$papera='-t'.$papera;}
  548: 	  my $comma = "dvips $papera -Ppdf -G0 -o $new_name_file";
  549: 	  &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
  550: 			     "for $status_statement now Converting to PS",
  551: 			     \%prog_state,$new_name_file);
  552: 	  if (-e $new_name_file) {
  553: 	      &repaginate_postscript($new_name_file);
  554: 	      print "<br />";
  555: 	      $new_name_file =~ m/^(.*)\./;
  556: 	      my $ps_file = my $tempo_file = $1.'temporar.ps';
  557: 	      my $pdf_file = $1.'.pdf';
  558: 	      $papera=~s/t/p/;
  559: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
  560: 		  $comma = "psnup $papera -2 -s1.0 $new_name_file";
  561: 		  &busy_wait_command("$comma $tempo_file 1>/dev/null 2>/dev/null",
  562: 				     "for $status_statement now Modifying PS layout",
  563: 				     \%prog_state,$tempo_file);
  564: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
  565: 		  $comma = 'pstops '.$papera.' "2:0+1(0.48w,0)"';
  566: 		  &busy_wait_command("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null",
  567: 				     "for $status_statement now Modifying PS layout",
  568: 				     \%prog_state,$tempo_file); 
  569: 	      } else {
  570: 		  $ps_file=$new_name_file;
  571: 	      }
  572: 	      my $addtoPSfile={'legal'=>'<< /PageSize [612 1008] >> setpagedevice',
  573:                                'tabloid'=>'<< /PageSize [792 1224] >> setpagedevice',
  574:                                'executive'=>,'<< /PageSize [540 720] >> setpagedevice',
  575:                                'a2'=>'<< /PageSize [1195.02 1690.09] >> setpagedevice',
  576:                                'a3'=>'<< /PageSize [842 1195.02] >> setpagedevice',
  577:                                'a4'=>'<< /PageSize [595.2 842] >> setpagedevice',
  578:                                'a5'=>'<< /PageSize [421.1 595.2] >> setpagedevice',
  579:                                'a6'=>'<< /PageSize [298.75 421.1] >> setpagedevice',
  580: 			   };
  581: 	      if ($paper ne 'letter') {
  582: 		  open(FFH,'<',$ps_file) || die "Couldn't open ps file $ps_file for reading: $!\n";
  583: 		  my $new_ps_file='new'.$ps_file;
  584: 		  open(FFHS,'>',$new_ps_file) || die "Couldn't open new ps file $new_ps_file for reading: $!\n";
  585: 		  print FFHS $addtoPSfile->{$paper}."\n";
  586: 		  while (<FFH>) {
  587: 		      print FFHS $_;
  588: 		  }
  589: 		  close(FFH);
  590: 		  close(FFHS);
  591: 		  $ps_file=$new_ps_file;	  
  592: 	      }
  593: 	      &busy_wait_command("ps2pdf $ps_file $pdf_file 1>/dev/null 2>/dev/null",
  594: 				 "for $status_statement now Converting PS to PDF",
  595: 				 \%prog_state,$pdf_file);
  596: 	    
  597: 	      my $texlog = $texfile;
  598: 	      my $texaux = $texfile;
  599: 	      my $texdvi = $texfile;
  600: 	      my $texps = $texfile;
  601: 	      $texlog =~ s/\.tex/\.log/;
  602: 	      $texaux =~ s/\.tex/\.aux/;
  603: 	      $texdvi =~ s/\.tex/\.dvi/;
  604: 	      $texps =~ s/\.tex/\.ps/;
  605: 	      my @garb = ($texlog,$texaux,$texdvi,$texps);
  606: #	  unlink @garb;
  607: 	      unlink $duefile;
  608: 	      print "<a href=\"/prtspool/$pdf_file\">$link_text - click here to download pdf</a>";
  609: 	      print "\n";
  610: 	  }
  611:       }  
  612:   } else {
  613:       print "LaTeX file $texfile was not created successfully";
  614:   }
  615: }
  616: print "<br />";
  617: if ($number_of_files>1) {
  618:     my $zipfile=$texfile[0];
  619:     $zipfile=~s/\.tex/\.zip/;
  620:     my $statement="zip $zipfile";
  621:     foreach my $file (@texfile) {
  622: 	$file=~s/\.tex/.\pdf/;
  623: 	$statement.=' '.$file; 
  624:     }
  625:     print("<pre>Zip Output:\n");
  626:     system($statement);
  627:     print("</pre>");
  628:     $zipfile=~s/\/home\/httpd//;
  629:     print "<br /> A <a href=\"$zipfile\">ZIP file</a> of all the PDFs.";
  630: }
  631: if ($advanced_role) { &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); }
  632: print(&Apache::loncommon::end_page());
  633: my $done;
  634: sub REAPER {
  635:     $done=1;
  636: }
  637: 
  638: sub busy_wait_command {
  639:     my ($command,$message,$progress_win,$output_file)=@_;
  640:     
  641:     $SIG{CHLD} = \&REAPER;
  642:     $done=0;
  643:     my $pid=open(CMD,"$command |");
  644:     if ($advanced_role) {
  645: 	&Apache::lonhtmlcommon::Update_PrgWin('',$progress_win,$message);
  646:     }
  647:     while(!$done) {
  648: 	sleep 1;
  649: 	my $extra_msg;
  650: 	if ($output_file) {
  651: 	    my $size=(stat($output_file))[7];
  652: 	    $extra_msg=", $size bytes generated";
  653: 	}
  654: 	if ($advanced_role) {
  655: 	    &Apache::lonhtmlcommon::Update_PrgWin('',$progress_win,
  656: 						  $message.$extra_msg);
  657: 	}
  658:     }
  659:     $SIG{CHLD}='IGNORE';
  660:     close(CMD);
  661: }
  662: 
  663: 
  664: #  Repagninate a postscript file.
  665: #  What we need to do:
  666: #   - Count the number of pages in each student.
  667: #   - Add pages between each student so that each student's output is
  668: #     the maximum number of pages.
  669: #   
  670: sub repaginate_postscript {
  671:     # For now just strip out the STARTOFSTUDENTSTAMP
  672:     # ENDOFSTUDENTSTAMP markers in the postscript.
  673: 
  674:     my ($postscript_filename) = @_;
  675:     open(PSFILE, "<$postscript_filename");
  676:     my @psfilelines = <PSFILE>;
  677:     close(PSFILE);
  678:     my $psbody = join('', @psfilelines);
  679:     $psbody =~ s/STARTOFSTUDENTSTAMP//g;
  680:     $psbody =~ s/ENDOFSTUDENTSTAMP//g;
  681: 
  682:     open(PSFILE, ">$postscript_filename");
  683:     print PSFILE $psbody;
  684: 
  685:     close PSFILE;
  686: }
  687: 

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