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

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

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