File:  [LON-CAPA] / loncom / interface / printout.pl
Revision 1.92: download - view: text, annotated - select for diffs
Mon Oct 31 21:48:07 2005 UTC (18 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#4321, stop checnking request.role.adv for wheterh advanced printing can be done, 2 new privledges 'pfo' Print for otheres, and 'pav' advanced printing options (i.e with answers etc.)

    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:  my $bodytag=&Apache::loncommon::bodytag('Creating PDF','','');
  165:  print $bodytag;
  166: 
  167:   my $identifier = $ENV{'QUERY_STRING'};
  168:   my $texfile = $env{'cgi.'.$identifier.'.file'};
  169:   my $laystyle = $env{'cgi.'.$identifier.'.layout'};
  170:   my $numberofcolumns = $env{'cgi.'.$identifier.'.numcol'};
  171:   my $paper = $env{'cgi.'.$identifier.'.paper'};
  172:   my $selectionmade = $env{'cgi.'.$identifier.'.selection'};
  173:   my $tableofcontents = $env{'cgi.'.$identifier.'.tableofcontents'};
  174:   my $tableofindex = $env{'cgi.'.$identifier.'.tableofindex'};
  175:   my $advanced_role = $env{'cgi.'.$identifier.'.role'};
  176:   my $number_of_files = $env{'cgi.'.$identifier.'.numberoffiles'}+1;
  177:   my $student_names = $env{'cgi.'.$identifier.'.studentnames'};
  178:   my $backref = &Apache::lonnet::unescape($env{'cgi.'.$identifier.'.backref'});
  179: 
  180:   
  181:   my @names_pack=();
  182:   if ($student_names=~/_END_/) {  
  183:       @names_pack=split(/_ENDPERSON_/,$student_names);
  184:   }
  185: 
  186:   my $figfile = $texfile;
  187:   $figfile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.dat/;
  188:   my $duefile = $texfile;
  189:   $duefile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.due/;
  190:   #do we have figures?
  191:   # print "Figure file: $figfile\n";
  192:   if (-e $figfile) {
  193:       # print "$figfile exists\n";
  194:       my %done_conversion;
  195:       my $temporary_file=IO::File->new($figfile) || die "Couldn't open fig file $figfile for reading: $!\n";
  196:       my @content_of_file = <$temporary_file>;
  197:       close $temporary_file;  
  198:       my $noteps;
  199:       my %prog_state;
  200:       if ($advanced_role) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Coverting Images to EPS','Picture Conversion Status',$#content_of_file,'inline','80');  }
  201:       foreach my $not_eps (@content_of_file) {
  202: 	  chomp($not_eps);
  203: 	  if ($not_eps ne '') {
  204: 	      # print "Converting $not_eps"; # Debugging.
  205:               my $status_statement='EPS picture for '.$not_eps;
  206: 	      # print "$status_statement\n";
  207: 	      $not_eps=~s|\/\.\/|\/|g;
  208: 	      my $eps_f = $not_eps;
  209: 	      # $eps_f =~ s/\.[^.]*$/\.eps/i;
  210: 	      $eps_f .= '.eps';	# Just append the eps ext.
  211: 	      if ($eps_f=~/\/home\/([^\/]+)\/public_html\//) {
  212:                   $eps_f=~s/\/home\/([^\/]+)\/public_html/$1/;
  213: 		  $eps_f = '/home/httpd/prtspool/'.$eps_f;
  214: 	      } elsif ($eps_f=~/$Apache::lonnet::perlvar{'lonDocRoot'}\/res\//) {
  215: 		  $eps_f=~m/$Apache::lonnet::perlvar{'lonDocRoot'}\/res\/(.+)/;
  216: 		  $eps_f = '/home/httpd/prtspool/'.$1;
  217: 	      } elsif ($eps_f=~/$Apache::lonnet::perlvar{'lonUsersDir'}\//) {
  218: 		  $eps_f=~/$Apache::lonnet::perlvar{'lonUsersDir'}\/([^\/]+)\/\w\/\w\/\w\/(.+)/;
  219: 		  $eps_f = '/home/httpd/prtspool/'.$1.'/'.$2;
  220: 	      }
  221: 	      $eps_f  =~ s/ /\_/g; # Spaces are problematic for system commands and LaTeX.
  222: 	      my $path=$eps_f;
  223: 	      $path =~ s/\/([^\/]+)\.eps$//;
  224: 	      # print "Final file path: $path "; # Debugging
  225: 	      File::Path::mkpath($path,0,0777);
  226: 	      $not_eps =~ s/^\s+//;
  227: 	      $not_eps =~ s/\s+$//;
  228: 	      $not_eps =~ s/ /\\ /g;
  229: 	      if ( exists($done_conversion{$not_eps})) { next; }
  230: 	      if ($advanced_role) {
  231: 		  my $prettyname=$not_eps;
  232: 		  $prettyname=~s|/home/([^/]+)/public_html|/priv/$1|;
  233: 		  $prettyname=~s|$Apache::lonnet::perlvar{'lonDocRoot'}/|/|;
  234: 		  &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,'Converting to EPS '.$prettyname);
  235: 	      }
  236: 	      $done_conversion{$not_eps}=1;
  237: 	      # print "Converting $not_eps -> $eps_f"; # Debugging
  238: 	      system("convert $not_eps $eps_f");
  239:               #check is eps exist in prtspool
  240:               if(not -e $eps_f) {
  241: 		  for (my $i=0;$i<10000;$i++) {
  242: 		      if (-e $eps_f.'.'.$i) {
  243: 			  rename $eps_f.'.'.$i, $eps_f;
  244: 			  last;
  245: 		      }
  246: 		  }
  247: 	      }  
  248: 	  }
  249:       }
  250:       if ($advanced_role) { 
  251: 	  &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); 
  252:       }
  253:       unlink($figfile);
  254:   }
  255:   #print "$texfile\n"; #name of the tex file for debugging only   
  256:   my @texfile=($texfile);
  257:   if ($number_of_files>1) {
  258:       @texfile=();
  259:       for (my $i=1;$i<=$number_of_files;$i++) {
  260: 	  my $new_texfile=$texfile;
  261: 	  $new_texfile=~s/\.tex//;
  262: 	  $new_texfile = sprintf("%s_%03d.tex", $new_texfile,$i);
  263: 	  push @texfile,$new_texfile;
  264:       } 
  265:   }
  266: 
  267: my $ind=-1;
  268: my %prog_state;
  269: print "<a href=\"$backref\"><b>Return</b></a> to last resource.<br /><br />";
  270: if ($advanced_role) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Print Status','Class Print Status',$number_of_files,'inline','80'); }
  271: print "<br />";
  272: my $num_files = @texfile;
  273: foreach $texfile (@texfile) {
  274:   my $status_statement='';
  275:   my $link_text='download PDF';
  276:   $ind++;
  277:   my @stud_info=split(/_END_/,$names_pack[$ind]);
  278:   my @tempo_array=split(/:/,$stud_info[0]);
  279:   my $name;
  280:   my $name_range='';
  281:   if ($tempo_array[3]) {
  282:       $name=$tempo_array[3];
  283:       ($name_range) = split(/,/,$name, 2);
  284:   } else {
  285:       $name=$tempo_array[0].'@'.$tempo_array[1];
  286:       $name_range = $tempo_array[0];
  287:   }
  288:   if (($name ne "") && ($name ne '@') ) { # Could be printing codes...
  289:       $link_text='<b>'.$name.'</b>';
  290:       $status_statement.=$name;
  291:   }
  292:   if ($#stud_info>0) {
  293:       @tempo_array=split(/:/,$stud_info[-1]);
  294:       if ($tempo_array[3]) {
  295: 	  $name=$tempo_array[3];
  296: 	  my ($lastname) = split(/,/, $name,2);
  297: 	  $name_range .= "-".$lastname;
  298:       } else {
  299: 	  $name=$tempo_array[0].'@'.$tempo_array[1];
  300: 	  $name_range .= '-'.$tempo_array[0];
  301:       }
  302:       if (($name ne "") && ($name ne '@')) {
  303: 	  $link_text.=' - <b>'.$name.'</b>';
  304: 	  $status_statement.=' -  '.$name;
  305:   
  306:       }
  307:   }
  308:   if(($num_files > 1) && ($link_text eq 'download PDF')) { # Printing codes
  309:       $link_text = '<b>'.basename($texfile,'.tex').'.pdf</b>';
  310:       $status_statement .= basename($texfile);
  311:   }
  312:   $name_range =~ s/'//g;	# O'Neil -> ONeil e.g.
  313:   print "<br/>";
  314:   if ($advanced_role) { &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,'Creating PDF for '.$status_statement); }
  315:   #  This little piece of dirt puts username ranges into the original tex
  316:   #  Tex filename from which they'll propagate into the other filenames as well.
  317:   #
  318:   if (-e $texfile) {
  319:       if (($name_range ne '') && ($num_files > 1)) {
  320: 	  my $newtexfile = $texfile;
  321: 	  $newtexfile    =~ s/\.tex/$name_range\.tex/;
  322: 	  rename($texfile, $newtexfile);
  323: 	  $texfile       = $newtexfile;
  324:       }
  325:       $texfile =~ m/^(.*)\/([^\/]+)$/; 
  326:       my $name_file = $2;
  327:       my $path_file = $1.'/';
  328:       chdir $path_file;
  329:       my $dvi_file= $name_file; $dvi_file =~ s/\.tex/$name_range\.dvi/;
  330:       &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
  331: 			 "for $status_statement now LaTeXing file",
  332: 			 \%prog_state,$dvi_file);
  333:       if ($tableofcontents eq 'yes') {
  334:       &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
  335: 			 "for $status_statement now LaTeXing file for table of contents",
  336: 			 \%prog_state,$dvi_file);
  337:       } #to create table of contents
  338:       my $idxname=$name_file;
  339:       $idxname=~s/\.tex$/\.idx/;
  340:       if ($tableofindex eq 'yes') {
  341: 	  &busy_wait_command("makeindex $idxname",
  342: 			     "making index file",
  343: 			     \%prog_state,$idxname);
  344: 	  &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
  345: 			     "for $status_statement now LaTeXing file for index section",
  346: 			     \%prog_state,$dvi_file);
  347:       } #to create index
  348:       #Do we have a latex error in the log file?
  349:       my $logfilename = $texfile; $logfilename =~ s/\.tex$/\.log/;
  350:       my $temporary_file=IO::File->new($logfilename) || die "Couldn't open log file $logfilename for reading: $!\n";
  351:       my @content_of_file = <$temporary_file>;
  352:       close $temporary_file; 
  353:       my $body_log_file = join(' ',@content_of_file);
  354:       $logfilename =~ s/\.log$/\.html/;
  355:       $temporary_file = IO::File->new('>'.$logfilename); 
  356:       print $temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_log_file.'</pre></body></html>'."\n";
  357:       if ($body_log_file=~m/!\s+Emergency stop/) {
  358: 	  my $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART';
  359: 	  my $whereitends = rindex $body_log_file,'STAMPOFPASSEDRESOURCEEND';
  360: 	  my $badresource;
  361: 	  my $badtext;
  362: 	  if ($whereitbegins!=-1 and $whereitends!=-1) {
  363: 	      $badtext = substr($body_log_file,$whereitbegins+26, $whereitends-$whereitbegins-26);
  364: 	      $whereitbegins  = rindex $badtext,'located in';
  365: 	      if ($whereitbegins != -1) {
  366: 		  
  367: 		  $badresource = substr($badtext, $whereitbegins+27, 
  368: 					length($badtext) - $whereitbegins - 48);
  369: 		  # print "<br />failing resourcename: $badresource<br />";
  370: 	      }
  371: 	  }
  372: 	  
  373:           if ($advanced_role) {  
  374: 	      #LaTeX failed to parse tex file 
  375: 	      print "<h2>LaTeX could not successfully parse your tex file.</h2>";
  376: 	      print "It probably has errors in it.<br />";
  377: 	      print "With very high probability this error occured in ".$badtext."<br /><br />";
  378: 	      print "Here are the error messages in the LaTeX log file<br /><pre>";
  379: 
  380: 	      my $sygnal = 0;
  381: 	      for (my $i=0;$i<=$#content_of_file;$i++) {
  382: 		  if ($content_of_file[$i]=~m/^Runaway argument?/ or $content_of_file[$i]=~m/^!/) {
  383: 		      $sygnal = 1;
  384: 		  } 
  385: 		  if ($content_of_file[$i]=~m/Here is how much of/) {
  386: 		      $sygnal = 0;
  387: 		  } 
  388: 		  if ($sygnal) {
  389: 		      print "$content_of_file[$i]";
  390: 		  }  
  391: 	      }
  392: 	      print "</pre>\n";
  393: 	      # print "<br /> Advanced role <br />";
  394:               print "<b><big>The link to ";
  395:               $logfilename=~s/\/home\/httpd//;
  396: 	      print "<a href=\"$logfilename\">Your log file </a></big></b>";
  397: 	      print "\n";
  398:               #link tooriginal LaTeX file (included according Michael Hamlin desire)
  399: 	      my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open tex file $texfile for reading: $!\n";
  400: 	      my @tex_content_of_file = <$tex_temporary_file>;
  401: 	      close $tex_temporary_file; 
  402: 	      my $body_tex_file = join(' ',@tex_content_of_file);
  403: 	      $texfile =~ s/\.tex$/aaaaa\.html/;
  404: 	      $tex_temporary_file = IO::File->new('>'.$texfile); 
  405: 	      print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
  406: 	      print "<br /><br />";
  407: 	      print "<b><big>The link to ";
  408: 	      $texfile=~s/\/home\/httpd//;
  409: 	      print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
  410: 	      print "\n";
  411: 	      my $help_text = &Apache::loncommon::help_open_topic("Print_Resource", "Help on printing");
  412: 	      print ("$help_text");
  413: 
  414: 	  } else {		# Student role...
  415: 	      #  at this point:
  416: 	      #    $body_log_file - contains the log file.
  417:               #    $name_file     - is the name of the LaTeX file.
  418:               #    $identifier    - is the unique LaTeX identifier.l
  419: 
  420: 	      print "<br />There are errors in $badtext";
  421: 	      print "<br />These errors prevent this resource from printing correctly";
  422: 	      my $tex_handle = IO::File->new($name_file);
  423: 	      my @tex_contents = <$tex_handle>;
  424: 	      &send_error_mail($identifier, $badresource, $body_log_file, \@tex_contents);
  425: 	      print "<br />A message has been sent to the instructor describing this failure<br />";
  426: 	      my $help_text = &Apache::loncommon::help_open_topic("Print_Resource", "Help on printing");
  427: 	      print  ("$help_text");
  428: 
  429: 	  }
  430: 
  431:       } elsif ($body_log_file=~m/<inserted text>/) {
  432: 	  my $whereitbegins = index $body_log_file,'<inserted text>';
  433: 	  print "You are running LaTeX in <b>batch mode</b>.";
  434: 	  while ($whereitbegins != -1) {
  435: 	      my $tempobegin=$whereitbegins;
  436: 	      $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART',$whereitbegins;
  437: 	      my $whereitends = index $body_log_file,'STAMPOFPASSEDRESOURCEEND',$whereitbegins;
  438: 	      print "<br />It has found an error in".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)." <br /> and corrected it.\n";
  439: 	      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";
  440: 	      $whereitbegins = index $body_log_file,'<inserted text>',$tempobegin+10;
  441: 	  }
  442: 	  $name_file =~ s/\.tex/\.dvi/;
  443: 	  my $new_name_file = $name_file;
  444: 	  $new_name_file =~ s/\.dvi/\.ps/;
  445: 	  my $papera=$paper;
  446: 	  if ($papera eq 'letter') {$papera='';}
  447: 	  if ($papera ne '') {$papera='-t'.$papera;}
  448: 	  my $comma = "dvips $papera -Ppdf -G0 -o $new_name_file";
  449: 	  &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
  450: 			     "for $status_statement now Converting to PS",
  451: 			     \%prog_state,$new_name_file);
  452: 	  if (-e $new_name_file) {
  453: 	      print "<h1>PDF output file (see link below)</h1>\n";
  454: 	      $new_name_file =~ m/^(.*)\./;
  455: 	      my $ps_file = my $tempo_file = $1.'temporar.ps';
  456: 	      my $pdf_file = $1.'.pdf';
  457: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
  458: 		  my $papera=$paper;
  459:                   if ($papera eq 'letter') {$papera='';}
  460: 		  if ($papera ne '') {$papera='-p'.$papera;}
  461: 		  $comma = "psnup $papera -2 -s1.0 $new_name_file";
  462: 		  &busy_wait_command("$comma $tempo_file 1>/dev/null 2>/dev/null",
  463: 				     "for $status_statement now Modifying PS layout",
  464: 				     \%prog_state,$tempo_file); 
  465: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
  466: 		  my $papera=$paper;
  467:                   if ($papera eq 'letter') {$papera='';}
  468: 		  if ($papera ne '') {$papera='-p'.$papera;}
  469: 		  $comma = 'pstops '.$papera.' "2:0+1(0.48w,0)"';
  470: 		  &busy_wait_command("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null",
  471: 				     "for $status_statement now Modifying PS layout",
  472: 				     \%prog_state,$tempo_file); 
  473: 
  474: 	      } else {
  475: 		  $ps_file=$new_name_file;
  476: 	      }	    
  477: 	      &busy_wait_command("ps2pdf $ps_file $pdf_file 1>/dev/null 2>/dev/null",
  478: 				 "for $status_statement now Converting PS to PDF",
  479: 				 \%prog_state, $pdf_file);
  480: 
  481: 	      my $texlog = $texfile;
  482: 	      my $texaux = $texfile;
  483: 	      my $texdvi = $texfile;
  484: 	      my $texps = $texfile;
  485: 	      $texlog =~ s/\.tex/\.log/;
  486: 	      $texaux =~ s/\.tex/\.aux/;
  487: 	      $texdvi =~ s/\.tex/\.dvi/;
  488: 	      $texps =~ s/\.tex/\.ps/;
  489: 	      my @garb = ($texaux,$texdvi,$texps);
  490: #	  unlink @garb;
  491: 	      unlink $duefile;
  492: 	      print "<a href=\"/prtspool/$pdf_file\">Your PDF document</a>";
  493: 	  }
  494: 	  if ($advanced_role) {  
  495: 	      print "<br /><br />";
  496: 	      print "<b><big>The link to ";
  497: 	      $logfilename=~s/\/home\/httpd//;
  498: 	      print "<a href=\"$logfilename\">Your log file </a></big></b>";
  499: 	      print "\n";
  500: 	      #link tooriginal LaTeX file (included according Michael Hamlin desire)
  501: 	      my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open tex file $texfile for reading: $!\n";
  502: 	      my @tex_content_of_file = <$tex_temporary_file>;
  503: 	      close $tex_temporary_file; 
  504: 	      my $body_tex_file = join(' ',@tex_content_of_file);
  505: 	      $texfile =~ s/\.tex$/aaaaa\.html/;
  506: 	      $tex_temporary_file = IO::File->new('>'.$texfile); 
  507: 	      print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
  508: 	      print "<br /><br />";
  509: 	      print "<b><big>The link to ";
  510: 	      $texfile=~s/\/home\/httpd//;
  511: 	      print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
  512: 	      print "\n";
  513: 	  }
  514:       } else {
  515: 	  #LaTeX successfully parsed tex file 
  516: 	  $name_file =~ s/\.tex/\.dvi/;
  517: 	  my $new_name_file = $name_file;
  518: 	  $new_name_file =~ s/\.dvi/\.ps/;
  519: 	  my $papera=$paper;
  520: 	  if ($papera eq 'letter') {$papera='';}
  521: 	  if ($papera ne '') {$papera='-t'.$papera;}
  522: 	  my $comma = "dvips $papera -Ppdf -G0 -o $new_name_file";
  523: 	  &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
  524: 			     "for $status_statement now Converting to PS",
  525: 			     \%prog_state,$new_name_file);
  526: 	  if (-e $new_name_file) {
  527: 	      print "<br />";
  528: 	      $new_name_file =~ m/^(.*)\./;
  529: 	      my $ps_file = my $tempo_file = $1.'temporar.ps';
  530: 	      my $pdf_file = $1.'.pdf';
  531: 	      $papera=~s/t/p/;
  532: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
  533: 		  $comma = "psnup $papera -2 -s1.0 $new_name_file";
  534: 		  &busy_wait_command("$comma $tempo_file 1>/dev/null 2>/dev/null",
  535: 				     "for $status_statement now Modifying PS layout",
  536: 				     \%prog_state,$tempo_file);
  537: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
  538: 		  $comma = 'pstops '.$papera.' "2:0+1(0.48w,0)"';
  539: 		  &busy_wait_command("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null",
  540: 				     "for $status_statement now Modifying PS layout",
  541: 				     \%prog_state,$tempo_file); 
  542: 	      } else {
  543: 		  $ps_file=$new_name_file;
  544: 	      }
  545: 	      my $addtoPSfile={'legal'=>'<< /PageSize [612 1008] >> setpagedevice',
  546:                                'tabloid'=>'<< /PageSize [792 1224] >> setpagedevice',
  547:                                'executive'=>,'<< /PageSize [540 720] >> setpagedevice',
  548:                                'a2'=>'<< /PageSize [1195.02 1690.09] >> setpagedevice',
  549:                                'a3'=>'<< /PageSize [842 1195.02] >> setpagedevice',
  550:                                'a4'=>'<< /PageSize [595.2 842] >> setpagedevice',
  551:                                'a5'=>'<< /PageSize [421.1 595.2] >> setpagedevice',
  552:                                'a6'=>'<< /PageSize [298.75 421.1] >> setpagedevice',
  553: 			   };
  554: 	      if ($paper ne 'letter') {
  555: 		  open(FFH,'<',$ps_file) || die "Couldn't open ps file $ps_file for reading: $!\n";
  556: 		  my $new_ps_file='new'.$ps_file;
  557: 		  open(FFHS,'>',$new_ps_file) || die "Couldn't open new ps file $new_ps_file for reading: $!\n";
  558: 		  print FFHS $addtoPSfile->{$paper}."\n";
  559: 		  while (<FFH>) {
  560: 		      print FFHS $_;
  561: 		  }
  562: 		  close(FFH);
  563: 		  close(FFHS);
  564: 		  $ps_file=$new_ps_file;	  
  565: 	      }
  566: 	      &busy_wait_command("ps2pdf $ps_file $pdf_file 1>/dev/null 2>/dev/null",
  567: 				 "for $status_statement now Converting PS to PDF",
  568: 				 \%prog_state,$pdf_file);
  569: 	    
  570: 	      my $texlog = $texfile;
  571: 	      my $texaux = $texfile;
  572: 	      my $texdvi = $texfile;
  573: 	      my $texps = $texfile;
  574: 	      $texlog =~ s/\.tex/\.log/;
  575: 	      $texaux =~ s/\.tex/\.aux/;
  576: 	      $texdvi =~ s/\.tex/\.dvi/;
  577: 	      $texps =~ s/\.tex/\.ps/;
  578: 	      my @garb = ($texlog,$texaux,$texdvi,$texps);
  579: #	  unlink @garb;
  580: 	      unlink $duefile;
  581: 	      print "<a href=\"/prtspool/$pdf_file\">$link_text - click here to download pdf</a>";
  582: 	      print "\n";
  583: 	  }
  584:       }  
  585:   } else {
  586:       print "LaTeX file $texfile was not created successfully";
  587:   }
  588: }
  589: print "<br />";
  590: if ($number_of_files>1) {
  591:     my $zipfile=$texfile[0];
  592:     $zipfile=~s/\.tex/\.zip/;
  593:     my $statement="zip $zipfile";
  594:     foreach my $file (@texfile) {
  595: 	$file=~s/\.tex/.\pdf/;
  596: 	$statement.=' '.$file; 
  597:     }
  598:     print("<pre>Zip Output:\n");
  599:     system($statement);
  600:     print("</pre>");
  601:     $zipfile=~s/\/home\/httpd//;
  602:     print "<br /> A <a href=\"$zipfile\">ZIP file</a> of all the PDFs.";
  603: }
  604: if ($advanced_role) { &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); }
  605: 
  606: my $done;
  607: sub REAPER {
  608:     $done=1;
  609: }
  610: 
  611: sub busy_wait_command {
  612:     my ($command,$message,$progress_win,$output_file)=@_;
  613:     
  614:     $SIG{CHLD} = \&REAPER;
  615:     $done=0;
  616:     my $pid=open(CMD,"$command |");
  617:     if ($advanced_role) {
  618: 	&Apache::lonhtmlcommon::Update_PrgWin('',$progress_win,$message);
  619:     }
  620:     while(!$done) {
  621: 	sleep 1;
  622: 	my $extra_msg;
  623: 	if ($output_file) {
  624: 	    my $size=(stat($output_file))[7];
  625: 	    $extra_msg=", $size bytes generated";
  626: 	}
  627: 	if ($advanced_role) {
  628: 	    &Apache::lonhtmlcommon::Update_PrgWin('',$progress_win,
  629: 						  $message.$extra_msg);
  630: 	}
  631:     }
  632:     $SIG{CHLD}='IGNORE';
  633:     close(CMD);
  634: }
  635: 
  636: 
  637: 
  638: 

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