File:  [LON-CAPA] / loncom / interface / printout.pl
Revision 1.94: download - view: text, annotated - select for diffs
Thu Dec 15 18:41:21 2005 UTC (18 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: version_2_1_X, version_2_1_3, version_2_1_2, version_2_1_1, version_2_1_0, version_2_0_99_1, HEAD
- some small look and feel improvements to the print results screen

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

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