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

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

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