File:  [LON-CAPA] / loncom / interface / printout.pl
Revision 1.85: download - view: text, annotated - select for diffs
Fri Jul 22 11:42:50 2005 UTC (18 years, 9 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Defect 4246 - retain information about the converted file's initial
filetype in case a single job requires e.g. file.jpg and file.gif to
be converted.

    1: #!/usr/bin/perl
    2: # CGI-script to run LaTeX, dvips, ps2ps, ps2pdf etc.
    3: #
    4: #
    5: # Copyright Michigan State University Board of Trustees
    6: #
    7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    8: #
    9: # LON-CAPA is free software; you can redistribute it and/or modify
   10: # it under the terms of the GNU General Public License as published by
   11: # the Free Software Foundation; either version 2 of the License, or
   12: # (at your option) any later version.
   13: #
   14: # LON-CAPA is distributed in the hope that it will be useful,
   15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17: # GNU General Public License for more details.
   18: #
   19: # You should have received a copy of the GNU General Public License
   20: # along with LON-CAPA; if not, write to the Free Software
   21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   22: #
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: 
   28: use lib '/home/httpd/lib/perl';
   29: use LONCAPA::loncgi;
   30: use File::Path;
   31: use IO::File;
   32: use Image::Magick;
   33: use Apache::lonhtmlcommon;
   34: use Apache::lonnet;
   35: use Apache::loncommon;
   36: use Apache::lonlocal;
   37: use Apache::lonmsg;
   38: use LONCAPA::Enrollment;
   39: 
   40: use strict;
   41: 
   42: 
   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 {
   55:     return (! $env{'request.role.adv'});
   56: }
   57: 
   58: #
   59: #   Debugging:  Dump the environment for debugging.
   60: #
   61: sub dumpenv  {
   62:     print "<br />-------------------<br />";
   63:     foreach my $key (sort (keys %env)) {
   64: 	print "<br />$key -> $env{$key}";
   65:     }
   66:     print "<br />-------------------<br />";
   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.
   75: #    badresource-  Filepath to most likely failing 
   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 {
   89:     my ($identifier, $badresource, $logfile, $texfile) = @_;
   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: 
   96:     #  resource file->URL
   97:     #
   98:     my $badurl = &Apache::lonnet::declutter($badresource);
   99: 
  100:     # &dumpenv();
  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:
  116: 	    # print "sending message to course coordinators.<br />";
  117: 
  118: 	    # Todo: Convert badurl into a url from file path:
  119: 
  120: 	    my $subject  = "Error [$badurl] Print failed for $user".'@'.$domain;
  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: 	    }
  131: 	    my (undef, %receivers) = &Apache::lonfeedback::decide_receiver(undef, 0,
  132: 									  1,1,1);
  133: 	    # print "<br /> sending...section:  $env{'request.course.sec'}";
  134: 	    foreach my $dest (keys %receivers) {
  135: 		# print "<br /> dest is $dest";
  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: 
  150: $|=1;
  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>
  158: Your cookie information is incorrect.
  159: </body>
  160: </html>
  161: END
  162:     return;
  163: }
  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;
  168: 
  169:   my $identifier = $ENV{'QUERY_STRING'};
  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'};
  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'});
  181: 
  182: 
  183:   my $adv = $env{'request.role.adv'};
  184:   
  185:   my @names_pack=();
  186:   if ($student_names=~/_END_/) {  
  187:       @names_pack=split(/_ENDPERSON_/,$student_names);
  188:   }
  189: 
  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?
  195:   # print "Figure file: $figfile\n";
  196:   if (-e $figfile) {
  197:       # print "$figfile exists\n";
  198:       my %done_conversion;
  199:       my $temporary_file=IO::File->new($figfile) || die "Couldn't open fig file $figfile for reading: $!\n";
  200:       my @content_of_file = <$temporary_file>;
  201:       close $temporary_file;  
  202:       my $noteps;
  203:       my %prog_state;
  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) {
  206: 	  chomp($not_eps);
  207: 	  if ($not_eps ne '') {
  208: 	      # print "Converting $not_eps"; # Debugging.
  209:               my $status_statement='EPS picture for '.$not_eps;
  210: 	      # print "$status_statement\n";
  211: 	      $not_eps=~s|\/\.\/|\/|g;
  212: 	      my $eps_f = $not_eps;
  213: 	      # $eps_f =~ s/\.[^.]*$/\.eps/i;
  214: 	      $eps_f .= '.eps';	# Just append the eps ext.
  215: 	      if ($eps_f=~/\/home\/([^\/]+)\/public_html\//) {
  216:                   $eps_f=~s/\/home\/([^\/]+)\/public_html/$1/;
  217: 		  $eps_f = '/home/httpd/prtspool/'.$eps_f;
  218: 	      } elsif ($eps_f=~/$Apache::lonnet::perlvar{'lonDocRoot'}\/res\//) {
  219: 		  $eps_f=~m/$Apache::lonnet::perlvar{'lonDocRoot'}\/res\/(.+)/;
  220: 		  $eps_f = '/home/httpd/prtspool/'.$1;
  221: 	      } elsif ($eps_f=~/$Apache::lonnet::perlvar{'lonUsersDir'}\//) {
  222: 		  $eps_f=~/$Apache::lonnet::perlvar{'lonUsersDir'}\/([^\/]+)\/\w\/\w\/\w\/(.+)/;
  223: 		  $eps_f = '/home/httpd/prtspool/'.$1.'/'.$2;
  224: 	      }
  225: 	      $eps_f  =~ s/ /\_/g; # Spaces are problematic for system commands and LaTeX.
  226: 	      my $path=$eps_f;
  227: 	      $path =~ s/\/([^\/]+)\.eps$//;
  228: 	      # print "Final file path: $path "; # Debugging
  229: 	      File::Path::mkpath($path,0,0777);
  230: 	      $not_eps =~ s/^\s+//;
  231: 	      $not_eps =~ s/\s+$//;
  232: 	      $not_eps =~ s/ /\\ /g;
  233: 	      if ( exists($done_conversion{$not_eps})) { next; }
  234: 	      if ($adv) {
  235: 		  my $prettyname=$not_eps;
  236: 		  $prettyname=~s|/home/([^/]+)/public_html|/priv/$1|;
  237: 		  $prettyname=~s|$Apache::lonnet::perlvar{'lonDocRoot'}/|/|;
  238: 		  &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,'Converting to EPS '.$prettyname);
  239: 	      }
  240: 	      $done_conversion{$not_eps}=1;
  241: 	      # print "Converting $not_eps -> $eps_f"; # Debugging
  242: 	      system("convert $not_eps $eps_f");
  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: 	      }  
  252: 	  }
  253:       }
  254:       if ($adv) { 
  255: 	  &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); 
  256:       }
  257:       unlink($figfile);
  258:   }
  259:   #print "$texfile\n"; #name of the tex file for debugging only   
  260:   my @texfile=($texfile);
  261:   if ($number_of_files>1) {
  262:       @texfile=();
  263:       for (my $i=1;$i<=$number_of_files;$i++) {
  264: 	  my $new_texfile=$texfile;
  265: 	  $new_texfile=~s/\.tex/_$i\.tex/;
  266: 	  push @texfile,$new_texfile;
  267:       } 
  268:   }
  269: 
  270: my $ind=-1;
  271: my %prog_state;
  272: print "<a href=\"$backref\"><b>Return</b></a> to last resource.<br /><br />";
  273: if ($adv) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Print Status','Class Print Status',$number_of_files,'inline','80'); }
  274: print "<br />";
  275: foreach $texfile (@texfile) {
  276:   my $status_statement='';
  277:   my $link_text='download PDF';
  278:   $ind++;
  279:   my @stud_info=split(/_END_/,$names_pack[$ind]);
  280:   my @tempo_array=split(/:/,$stud_info[0]);
  281:   my $name;
  282:   if ($tempo_array[3]) {
  283:       $name=$tempo_array[3];
  284:   } else {
  285:       $name=$tempo_array[0].'@'.$tempo_array[1];
  286:   }
  287:   $link_text='<b>'.$name.'</b>';
  288:   $status_statement.=$name;
  289:   if ($#stud_info>0) {
  290:       @tempo_array=split(/:/,$stud_info[-1]);
  291:       if ($tempo_array[3]) {
  292: 	  $name=$tempo_array[3];
  293:       } else {
  294: 	  $name=$tempo_array[0].'@'.$tempo_array[1];
  295:       }
  296:       $link_text.=' - <b>'.$name.'</b>';
  297:       $status_statement.=' -  '.$name;
  298:   } 
  299:   if ($adv) { &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,'Creating PDF for '.$status_statement); }
  300:   if (-e $texfile) {
  301:       $texfile =~ m/^(.*)\/([^\/]+)$/; 
  302:       my $name_file = $2;
  303:       my $path_file = $1.'/';
  304:       chdir $path_file;
  305:       my $dvi_file= $name_file; $dvi_file =~ s/\.tex/\.dvi/;
  306:       &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
  307: 			 "for $status_statement now LaTeXing file",
  308: 			 \%prog_state,$dvi_file);
  309:       if ($tableofcontents eq 'yes') {
  310:       &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
  311: 			 "for $status_statement now LaTeXing file for table of contents",
  312: 			 \%prog_state,$dvi_file);
  313:       } #to create table of contents
  314:       my $idxname=$name_file;
  315:       $idxname=~s/\.tex$/\.idx/;
  316:       if ($tableofindex eq 'yes') {
  317: 	  &busy_wait_command("makeindex $idxname",
  318: 			     "making index file",
  319: 			     \%prog_state,$idxname);
  320: 	  &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
  321: 			     "for $status_statement now LaTeXing file for index section",
  322: 			     \%prog_state,$dvi_file);
  323:       } #to create index
  324:       #Do we have a latex error in the log file?
  325:       my $logfilename = $texfile; $logfilename =~ s/\.tex$/\.log/;
  326:       my $temporary_file=IO::File->new($logfilename) || die "Couldn't open log file $logfilename for reading: $!\n";
  327:       my @content_of_file = <$temporary_file>;
  328:       close $temporary_file; 
  329:       my $body_log_file = join(' ',@content_of_file);
  330:       $logfilename =~ s/\.log$/\.html/;
  331:       $temporary_file = IO::File->new('>'.$logfilename); 
  332:       print $temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_log_file.'</pre></body></html>'."\n";
  333:       if ($body_log_file=~m/!\s+Emergency stop/) {
  334: 	  my $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART';
  335: 	  my $whereitends = rindex $body_log_file,'STAMPOFPASSEDRESOURCEEND';
  336: 	  my $badresource;
  337: 	  my $badtext;
  338: 	  if ($whereitbegins!=-1 and $whereitends!=-1) {
  339: 	      $badtext = substr($body_log_file,$whereitbegins+26, $whereitends-$whereitbegins-26);
  340: 	      $whereitbegins  = rindex $badtext,'located in';
  341: 	      if ($whereitbegins != -1) {
  342: 		  
  343: 		  $badresource = substr($badtext, $whereitbegins+27, 
  344: 					length($badtext) - $whereitbegins - 48);
  345: 		  # print "<br />failing resourcename: $badresource<br />";
  346: 	      }
  347: 	  }
  348: 	  
  349:           if ($advanced_role) {  
  350: 	      #LaTeX failed to parse tex file 
  351: 	      print "<h2>LaTeX could not successfully parse your tex file.</h2>";
  352: 	      print "It probably has errors in it.<br />";
  353: 	      print "With very high probability this error occured in ".$badtext."<br /><br />";
  354: 	      print "Here are the error messages in the LaTeX log file<br /><pre>";
  355: 	      my $sygnal = 0;
  356: 	      for (my $i=0;$i<=$#content_of_file;$i++) {
  357: 		  if ($content_of_file[$i]=~m/^Runaway argument?/ or $content_of_file[$i]=~m/^!/) {
  358: 		      $sygnal = 1;
  359: 		  } 
  360: 		  if ($content_of_file[$i]=~m/Here is how much of/) {
  361: 		      $sygnal = 0;
  362: 		  } 
  363: 		  if ($sygnal) {
  364: 		      print "$content_of_file[$i]";
  365: 		  }  
  366: 	      }
  367: 	      print "</pre>\n";
  368: 	      # print "<br /> Advanced role <br />";
  369:               print "<b><big>The link to ";
  370:               $logfilename=~s/\/home\/httpd//;
  371: 	      print "<a href=\"$logfilename\">Your log file </a></big></b>";
  372: 	      print "\n";
  373:               #link tooriginal LaTeX file (included according Michael Hamlin desire)
  374: 	      my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open tex file $texfile for reading: $!\n";
  375: 	      my @tex_content_of_file = <$tex_temporary_file>;
  376: 	      close $tex_temporary_file; 
  377: 	      my $body_tex_file = join(' ',@tex_content_of_file);
  378: 	      $texfile =~ s/\.tex$/aaaaa\.html/;
  379: 	      $tex_temporary_file = IO::File->new('>'.$texfile); 
  380: 	      print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
  381: 	      print "<br /><br />";
  382: 	      print "<b><big>The link to ";
  383: 	      $texfile=~s/\/home\/httpd//;
  384: 	      print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
  385: 	      print "\n";
  386: 	  } else {		# Student role...
  387: 	      #  at this point:
  388: 	      #    $body_log_file - contains the log file.
  389:               #    $name_file     - is the name of the LaTeX file.
  390:               #    $identifier    - is the unique LaTeX identifier.l
  391: 
  392: 	      print "<br />There are errors in $badtext";
  393: 	      print "<br />These errors prevent this resource from printing correctly";
  394: 	      my $tex_handle = IO::File->new($name_file);
  395: 	      my @tex_contents = <$tex_handle>;
  396: 	      &send_error_mail($identifier, $badresource, $body_log_file, \@tex_contents);
  397: 	      print "<br />A message has been sent to the instructor describing this failure<br />"
  398: 	  }
  399: 
  400:       } elsif ($body_log_file=~m/<inserted text>/) {
  401: 	  my $whereitbegins = index $body_log_file,'<inserted text>';
  402: 	  print "You are running LaTeX in <b>batch mode</b>.";
  403: 	  while ($whereitbegins != -1) {
  404: 	      my $tempobegin=$whereitbegins;
  405: 	      $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART',$whereitbegins;
  406: 	      my $whereitends = index $body_log_file,'STAMPOFPASSEDRESOURCEEND',$whereitbegins;
  407: 	      print "<br />It has found an error in".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)." <br /> and corrected it.\n";
  408: 	      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";
  409: 	      $whereitbegins = index $body_log_file,'<inserted text>',$tempobegin+10;
  410: 	  }
  411: 	  $name_file =~ s/\.tex/\.dvi/;
  412: 	  my $new_name_file = $name_file;
  413: 	  $new_name_file =~ s/\.dvi/\.ps/;
  414: 	  my $papera=$paper;
  415: 	  if ($papera eq 'letter') {$papera='';}
  416: 	  if ($papera ne '') {$papera='-t'.$papera;}
  417: 	  my $comma = "dvips $papera -Ppdf -G0 -o $new_name_file";
  418: 	  &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
  419: 			     "for $status_statement now Converting to PS",
  420: 			     \%prog_state,$new_name_file);
  421: 	  if (-e $new_name_file) {
  422: 	      print "<h1>PDF output file (see link below)</h1>\n";
  423: 	      $new_name_file =~ m/^(.*)\./;
  424: 	      my $ps_file = my $tempo_file = $1.'temporar.ps';
  425: 	      my $pdf_file = $1.'.pdf';
  426: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
  427: 		  my $papera=$paper;
  428:                   if ($papera eq 'letter') {$papera='';}
  429: 		  if ($papera ne '') {$papera='-p'.$papera;}
  430: 		  $comma = "psnup $papera -2 -s1.0 $new_name_file";
  431: 		  &busy_wait_command("$comma $tempo_file 1>/dev/null 2>/dev/null",
  432: 				     "for $status_statement now Modifying PS layout",
  433: 				     \%prog_state,$tempo_file); 
  434: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
  435: 		  my $papera=$paper;
  436:                   if ($papera eq 'letter') {$papera='';}
  437: 		  if ($papera ne '') {$papera='-p'.$papera;}
  438: 		  $comma = 'pstops '.$papera.' "2:0+1(0.48w,0)"';
  439: 		  &busy_wait_command("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null",
  440: 				     "for $status_statement now Modifying PS layout",
  441: 				     \%prog_state,$tempo_file); 
  442: 
  443: 	      } else {
  444: 		  $ps_file=$new_name_file;
  445: 	      }	    
  446: 	      &busy_wait_command("ps2pdf $ps_file $pdf_file 1>/dev/null 2>/dev/null",
  447: 				 "for $status_statement now Converting PS to PDF",
  448: 				 \%prog_state, $pdf_file);
  449: 
  450: 	      my $texlog = $texfile;
  451: 	      my $texaux = $texfile;
  452: 	      my $texdvi = $texfile;
  453: 	      my $texps = $texfile;
  454: 	      $texlog =~ s/\.tex/\.log/;
  455: 	      $texaux =~ s/\.tex/\.aux/;
  456: 	      $texdvi =~ s/\.tex/\.dvi/;
  457: 	      $texps =~ s/\.tex/\.ps/;
  458: 	      my @garb = ($texaux,$texdvi,$texps);
  459: #	  unlink @garb;
  460: 	      unlink $duefile;
  461: 	      print "<a href=\"/prtspool/$pdf_file\">Your PDF document</a>";
  462: 	  }
  463: 	  if ($advanced_role) {  
  464: 	      print "<br /><br />";
  465: 	      print "<b><big>The link to ";
  466: 	      $logfilename=~s/\/home\/httpd//;
  467: 	      print "<a href=\"$logfilename\">Your log file </a></big></b>";
  468: 	      print "\n";
  469: 	      #link tooriginal LaTeX file (included according Michael Hamlin desire)
  470: 	      my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open tex file $texfile for reading: $!\n";
  471: 	      my @tex_content_of_file = <$tex_temporary_file>;
  472: 	      close $tex_temporary_file; 
  473: 	      my $body_tex_file = join(' ',@tex_content_of_file);
  474: 	      $texfile =~ s/\.tex$/aaaaa\.html/;
  475: 	      $tex_temporary_file = IO::File->new('>'.$texfile); 
  476: 	      print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
  477: 	      print "<br /><br />";
  478: 	      print "<b><big>The link to ";
  479: 	      $texfile=~s/\/home\/httpd//;
  480: 	      print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
  481: 	      print "\n";
  482: 	  }
  483:       } else {
  484: 	  #LaTeX successfully parsed tex file 
  485: 	  $name_file =~ s/\.tex/\.dvi/;
  486: 	  my $new_name_file = $name_file;
  487: 	  $new_name_file =~ s/\.dvi/\.ps/;
  488: 	  my $papera=$paper;
  489: 	  if ($papera eq 'letter') {$papera='';}
  490: 	  if ($papera ne '') {$papera='-t'.$papera;}
  491: 	  my $comma = "dvips $papera -Ppdf -G0 -o $new_name_file";
  492: 	  &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
  493: 			     "for $status_statement now Converting to PS",
  494: 			     \%prog_state,$new_name_file);
  495: 	  if (-e $new_name_file) {
  496: 	      print "<br />";
  497: 	      $new_name_file =~ m/^(.*)\./;
  498: 	      my $ps_file = my $tempo_file = $1.'temporar.ps';
  499: 	      my $pdf_file = $1.'.pdf';
  500: 	      $papera=~s/t/p/;
  501: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
  502: 		  $comma = "psnup $papera -2 -s1.0 $new_name_file";
  503: 		  &busy_wait_command("$comma $tempo_file 1>/dev/null 2>/dev/null",
  504: 				     "for $status_statement now Modifying PS layout",
  505: 				     \%prog_state,$tempo_file);
  506: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
  507: 		  $comma = 'pstops '.$papera.' "2:0+1(0.48w,0)"';
  508: 		  &busy_wait_command("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null",
  509: 				     "for $status_statement now Modifying PS layout",
  510: 				     \%prog_state,$tempo_file); 
  511: 	      } else {
  512: 		  $ps_file=$new_name_file;
  513: 	      }
  514: 	      my $addtoPSfile={'legal'=>'<< /PageSize [612 1008] >> setpagedevice',
  515:                                'tabloid'=>'<< /PageSize [792 1224] >> setpagedevice',
  516:                                'executive'=>,'<< /PageSize [540 720] >> setpagedevice',
  517:                                'a2'=>'<< /PageSize [1195.02 1690.09] >> setpagedevice',
  518:                                'a3'=>'<< /PageSize [842 1195.02] >> setpagedevice',
  519:                                'a4'=>'<< /PageSize [595.2 842] >> setpagedevice',
  520:                                'a5'=>'<< /PageSize [421.1 595.2] >> setpagedevice',
  521:                                'a6'=>'<< /PageSize [298.75 421.1] >> setpagedevice',
  522: 			   };
  523: 	      if ($paper ne 'letter') {
  524: 		  open(FFH,'<',$ps_file) || die "Couldn't open ps file $ps_file for reading: $!\n";
  525: 		  my $new_ps_file='new'.$ps_file;
  526: 		  open(FFHS,'>',$new_ps_file) || die "Couldn't open new ps file $new_ps_file for reading: $!\n";
  527: 		  print FFHS $addtoPSfile->{$paper}."\n";
  528: 		  while (<FFH>) {
  529: 		      print FFHS $_;
  530: 		  }
  531: 		  close(FFH);
  532: 		  close(FFHS);
  533: 		  $ps_file=$new_ps_file;	  
  534: 	      }
  535: 	      &busy_wait_command("ps2pdf $ps_file $pdf_file 1>/dev/null 2>/dev/null",
  536: 				 "for $status_statement now Converting PS to PDF",
  537: 				 \%prog_state,$pdf_file);
  538: 	    
  539: 	      my $texlog = $texfile;
  540: 	      my $texaux = $texfile;
  541: 	      my $texdvi = $texfile;
  542: 	      my $texps = $texfile;
  543: 	      $texlog =~ s/\.tex/\.log/;
  544: 	      $texaux =~ s/\.tex/\.aux/;
  545: 	      $texdvi =~ s/\.tex/\.dvi/;
  546: 	      $texps =~ s/\.tex/\.ps/;
  547: 	      my @garb = ($texlog,$texaux,$texdvi,$texps);
  548: #	  unlink @garb;
  549: 	      unlink $duefile;
  550: 	      print "<a href=\"/prtspool/$pdf_file\">$link_text - click here to download pdf</a>";
  551: 	      print "\n";
  552: 	  }
  553:       }  
  554:   } else {
  555:       print "LaTeX file $texfile was not created successfully";
  556:   }
  557: }
  558: print "<br />";
  559: if ($number_of_files>1) {
  560:     my $zipfile=$texfile[0];
  561:     $zipfile=~s/\.tex/\.zip/;
  562:     my $statement="zip $zipfile";
  563:     foreach my $file (@texfile) {
  564: 	$file=~s/\.tex/.\pdf/;
  565: 	$statement.=' '.$file; 
  566:     }
  567:     print("<pre>Zip Output:\n");
  568:     system($statement);
  569:     print("</pre>");
  570:     $zipfile=~s/\/home\/httpd//;
  571:     print "<br /> A <a href=\"$zipfile\">ZIP file</a> of all the PDFs.";
  572: }
  573: if ($adv) { &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); }
  574: 
  575: my $done;
  576: sub REAPER {
  577:     $done=1;
  578: }
  579: 
  580: sub busy_wait_command {
  581:     my ($command,$message,$progress_win,$output_file)=@_;
  582:     
  583:     $SIG{CHLD} = \&REAPER;
  584:     $done=0;
  585:     my $pid=open(CMD,"$command |");
  586:     if ($adv) {
  587: 	&Apache::lonhtmlcommon::Update_PrgWin('',$progress_win,$message);
  588:     }
  589:     while(!$done) {
  590: 	sleep 1;
  591: 	my $extra_msg;
  592: 	if ($output_file) {
  593: 	    my $size=(stat($output_file))[7];
  594: 	    $extra_msg=", $size bytes generated";
  595: 	}
  596: 	if ($adv) {
  597: 	    &Apache::lonhtmlcommon::Update_PrgWin('',$progress_win,
  598: 						  $message.$extra_msg);
  599: 	}
  600:     }
  601:     $SIG{CHLD}='IGNORE';
  602:     close(CMD);
  603: }
  604: 
  605: 
  606: 
  607: 

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