--- loncom/interface/printout.pl 2002/05/09 15:46:01 1.7 +++ loncom/interface/printout.pl 2024/07/11 12:15:16 1.170 @@ -1,75 +1,1256 @@ #!/usr/bin/perl +# CGI-script to run LaTeX, dvips, ps2ps, ps2pdf etc. +# +# $Id: printout.pl,v 1.170 2024/07/11 12:15:16 raeburn Exp $ +# +# Copyright Michigan State University Board of Trustees +# +# This file is part of the LearningOnline Network with CAPA (LON-CAPA). +# +# LON-CAPA is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# LON-CAPA is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with LON-CAPA; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# /home/httpd/html/adm/gpl.txt +# +# http://www.lon-capa.org/ +# + +use lib '/home/httpd/lib/perl'; +use LONCAPA::loncgi; +use File::Path; +use File::Basename; +use File::Copy; use IO::File; use Image::Magick; -print "Content-type: text/html\n\n"; -print "
\n"; -print "'.&mt("[_1]Return[_2] to resource.", + "","").'
'); + print(''. + &mt("Change Printing Options").'
'."\n"); +} my $figfile = $texfile; -$figfile =~ s/\.tex/\.dat/; +$figfile =~ s/^(.*_printout)_\d+_\d+_\d+\.tex/$1\.dat/; +my $duefile = $texfile; +$duefile =~ s/^(.*_printout)_\d+_\d+_\d+\.tex/$1\.due/; + + +#------------------------------------------------------------------------------------- +# +# Each print may have associated with it a file that contains a set of figures +# that need to be converted to .eps from whatever form they were in when included +# in the resource. The name of the figure file is in $figfile. If it exists, +# it contains the names of the files that need to be converted, one per line. +# + +&debug("Figure file is $figfile"); + if (-e $figfile) { - my $temporary_file=IO::File->new($figfile) || die "Couldn't open file for reading: $!\n"; + &debug( "Figure file exists"); + &debug("$figfile exists"); + my %done_conversion; + my $temporary_file=IO::File->new($figfile) || die "Couldn't open fig file $figfile for reading: $!\n"; my @content_of_file = <$temporary_file>; close $temporary_file; my $noteps; - foreach $not_eps (@content_of_file) { + my %prog_state; + if ($advanced_role) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('',$#content_of_file); } + print('' + .&mt('[_1] - [_2]Your PDF file[_3] is ready for download.', + $link_text,'','') + .'
'."\n"; + } + unlink($missfonts_file); + } + } else { + print + '' + .&mt('The LaTeX file [_1] was not created successfully.', + ''.$texfile.'') + .'
'; + } +} +if ($advanced_role) { &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); } +print "'.&mt('Zip Output:')."\n"); + my %zip_prog_state; + if ($advanced_role) { %zip_prog_state=&Apache::lonhtmlcommon::Create_PrgWin('',$number_of_files); } + my $zipfile=$texfile[0]; + $zipfile=~s/\.tex/\.zip/; + my $zip = Archive::Zip->new(); + my $counter = 0; + foreach my $file (@texfile) { + $file=~s/\.tex/.\pdf/; + my $dest=$file; + $dest=~s{^\Q$perlvar{'lonPrtDir'}\E}{prtspool}; + $zip->addFile($file,$dest); + $dest=~s/^prtspool//; + $counter ++; + if ($advanced_role) { + &Apache::lonhtmlcommon::Update_PrgWin('',\%zip_prog_state, + &mt('[_1] added to zip archive ([_2] of [_3]', + $dest,$counter,$number_of_files)); + } + } + if ($advanced_role) { + &Apache::lonhtmlcommon::Update_PrgWin('',\%zip_prog_state,&mt('Writing zip file')); + } + if ($zip->writeToFileNamed($zipfile) == AZ_OK) { + $zipfile=~s{^\Q$perlvar{'lonPrtDir'}\E}{/prtspool}; + print + '
' + .&mt('A [_1]ZIP file[_2] of all the PDF files is ready for download.', + '','') + .'
'; + } else { + print ''. + &mt('An error occurred creating a ZIP file of all the PDF files'). + '
'; + } + if ($advanced_role) { &Apache::lonhtmlcommon::Close_PrgWin('',\%zip_prog_state); } +} +print(&Apache::loncommon::end_page()); +my $done; + +sub REAPER { + $done=1; +} +# +# Execute a command updating the status window as the command's +# output file builds up (at intervals of a second). +# +# If the timeout argument defined, then if that many seconds +# elapses without an increase in the size of the output file, +# the command will be killed (this deals with the case when +# latex crawls into an infinite loop). +# +sub busy_wait_command { + my ($command,$message,$progress_win,$output_file, $timeout)=@_; + + $SIG{CHLD} = \&REAPER; + $done=0; + my $pid=open(CMD,"$command |"); + if ($advanced_role) { + &Apache::lonhtmlcommon::Update_PrgWin('',$progress_win,$message); + } + my $last_size = 0; + my $unchanged_time = 0; + while(!$done) { + sleep 1; + my $extra_msg; + if ($output_file) { + my $size=(stat($output_file))[7]; + $extra_msg=", $size bytes generated"; + if ($size == $last_size) { + $unchanged_time++; + if ($timeout && ($unchanged_time > $timeout)) { + print ''.&mt('Operation timed out!')."
\n"; + print "Executing $command, the output file $output_file did not grow\n"; + print "after $timeout seconds. This may indicate $command\n"; + print "is in an infinite loop.\n"; + print "See if printing fewer copies helps. Please contact LON-CAPA\n"; + print "support about this in any event."; + print "
"; + kill(9, $pid); # Reaper will do the rest...I hope there's errors in the log. + } + } else { + $last_size = $size; + $unchanged_time = 0; + } + } + if ($advanced_role) { + &Apache::lonhtmlcommon::Update_PrgWin('',$progress_win,$message.$extra_msg); + } + } + $SIG{CHLD}='IGNORE'; + close(CMD); +} + +# Make the dvi file (or rather try to), from the latex file and the +# various bits and pieces that control how the latex file is processed: +# LaTeX is run as many times a needed to make this all happen... this may +# result in several runs of LaTeX that just are errors if the LaTeX is +# bad, but the printing subsystem is _supposed_ to not do that. +# +# Parameters: +# name_file - Name of the LaTeX file to process. +# dvi_file - Name of resulting dvi file. +# tableofcontents - "yes" if we are supposed to make a table of contents. +# tableofindex - "yes" if we are suposed to make an index. +# status_statement - Part of the status statement for ths status window. +# prog_state - Reference to the program state hash. +# busy_wait_timeout- Seconds without any progress that imply a problem. +# +# +sub make_dvi_file { + my ($name_file, + $dvi_file, + $tableofcontents, + $tableofindex, + $status_statement, + $prog_state, + $busy_wait_timeout) = @_; + + + &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null", + "for $status_statement now LaTeXing file", + $prog_state,$dvi_file, $busy_wait_timeout); + + # If the tableof contents was requested, we need to run + # LaTex a couple more times to get all the references sorted out. + + if ($tableofcontents eq 'yes') { + &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null", + "for $status_statement First LaTeX of file for table of contents", + $prog_state,$dvi_file, $busy_wait_timeout); + &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null", + "for $status_statement Second LaTeX of file for table of contents", + $prog_state,$dvi_file,$busy_wait_timeout); + } + + # And makeindex and another run of LaTeX to incorporate it if the index + # is enabled. + + + if ($tableofindex eq 'yes') { + my $idxname=$name_file; + $idxname=~s/\.tex$/\.idx/; + &busy_wait_command("makeindex $idxname", + "making index file", + $prog_state,$idxname); + &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null", + "for $status_statement now LaTeXing file for index section", + $prog_state,$dvi_file, $busy_wait_timeout); + } + +} + + +# Repagninate +# What we need to do: +# - Count the number of pages in each student. +# - Rewrite the latex file replacing the \specials that +# mark the end of student with an appropriate number of newlines. +# parameters: +# psfile - Postscript filename +# latexfile - LaTeX filename +# columns - number of columns. +sub repaginate { + + # We will try to do this in 2 passes through the postscript since + # the postscript is potentially large, to do 2 passes, the first pass + # must be able to calculate the total number of document pages so that + # at the beginning of the second pass we already know how to replace + # %%Pages: + + # Figure out + # 1. Number of pages in the document + # 2. Maximum number of pages in a student + # 3. Number of pages in each student. + + my ($postscript_filename, $latex_filename, $num_columns) = @_; + open(PSFILE, "<$postscript_filename"); + my $line; + my $total_pages; # Total pages in document. + my $seen_pages = 0; # There are several %%Pages only the first is useful + my @pages_in_student; # For each student his/her initial page count. + my $max_pages = 0; # Pages in 'longest' student. + my $page_number = 0; + &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,&mt("Counting pages for student: [_1]",1)); + + while ($line =' + .&mt('An error occurred during the conversion of [_1] to postscript.', + ''.$prettyname.'') + .'
'; + } else { + $pdfs_converted++; # Need to fix ps in last pass. + } + } else { + my @args = ('convert',$not_eps,$eps_f); + system({$args[0]} @args); # Indirect object forces list processing mode. + # See perlfunc documentation for exec(). + if ($? and $advanced_role) { + print ''
+ .&mt('An error occurred during the conversion of [_1].',
+ ''.$prettyname.'')
+ .'
'
+ .&mt('If possible try to save this image using different settings and republish it.')
+ .'
'.$body_log_file.''."\n"; + if ($body_log_file=~m/!\s+Emergency stop/) { + my $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART'; + my $whereitends = rindex $body_log_file,'STAMPOFPASSEDRESOURCEEND'; + my $badresource; + my $badtext; + if ($whereitbegins!=-1 and $whereitends!=-1) { + $badtext = substr($body_log_file,$whereitbegins+26, $whereitends-$whereitbegins-26); + $whereitbegins = rindex $badtext,'located in'; + if ($whereitbegins != -1) { + + $badresource = substr($badtext, $whereitbegins+27, + length($badtext) - $whereitbegins - 48); + # print "
"; + + my $sygnal = 0; + for (my $i=0;$i<=$#content_of_file;$i++) { + if ($content_of_file[$i]=~m/^Runaway argument?/ or $content_of_file[$i]=~m/^!/) { + $sygnal = 1; + } + if ($content_of_file[$i]=~m/Here is how much of/) { + $sygnal = 0; + } + if ($sygnal) { + print "$content_of_file[$i]"; + } + } + print "\n"; + # print "
'.$body_tex_file.''."\n"; + print "
'; + if ($badtext) { + print &mt('There are errors in [_1].',$badtext); + } else { + print &mt('There are errors.'); + } + print '
' + .&mt('These errors prevent this resource from printing correctly.'); + + my $tex_handle = IO::File->new($texfile); + my @tex_contents = <$tex_handle>; + &send_error_mail($identifier, $badresource, $body_log_file, \@tex_contents); + print "" + .&mt('A message has been sent to the instructor describing this failure.') + ."
"; + my $help_text = &Apache::loncommon::help_open_topic("Print_Resource", &mt('Help on printing')); + print ("$help_text"); + + } + + # Either way, an emergency stop does not allow us to continue so: + + return 0; + + # The branch of code below is taken if it appears that + # there was no emergency stop but LaTeX had to correct the + # input file to run. + # In that case we need to provide error feedback, as the correction >may< not be + # sufficient, we can let the game continue as there's a dvi file to process. + + } elsif ($body_log_file=~m/'.$body_tex_file.''."\n"; + print "