--- loncom/interface/printout.pl 2009/11/17 01:13:51 1.142.6.2 +++ loncom/interface/printout.pl 2024/11/09 16:08:15 1.177 @@ -1,7 +1,7 @@ #!/usr/bin/perl # CGI-script to run LaTeX, dvips, ps2ps, ps2pdf etc. # -# $Id: printout.pl,v 1.142.6.2 2009/11/17 01:13:51 raeburn Exp $ +# $Id: printout.pl,v 1.177 2024/11/09 16:08:15 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -40,11 +40,13 @@ use Apache::lonlocal; use Apache::lonmsg(); use LONCAPA::Enrollment; use LONCAPA::Configuration; +use LONCAPA; +use Archive::Zip qw( :ERROR_CODES ); use strict; my $busy_wait_timeout = 30; - +my $pdfs_converted = 0; # non zero if PDF includes (need to fixps). my $debugging = 0; @@ -127,8 +129,8 @@ sub send_error_mail { # Todo: Convert badurl into a url from file path: - my $subject = "Error [$badurl] Print failed for $user".'@'.$domain; - my $message .= "Print failed to render LaTeX for $user".'@'."$domain\n"; + my $subject = "Error [$badurl] Print failed for $user".':'.$domain; + my $message .= "Print failed to render LaTeX for $user".':'."$domain\n"; $message .= " User was attempting to print: \n"; foreach my $resource (split(/:/,$resources)) { $message .= " $resource\n"; @@ -159,6 +161,36 @@ sub send_error_mail { } } +sub get_pstops_offsets { + my ($paper) = @_; + my ($origwidth,$origheight,$origunits,$ptype,$xoff,$yoffl,$yoffr); + $ptype = "-p$paper"; + if ($paper eq 'letter') { + $origwidth = 11.0; + $origheight = 8.5; + $origunits = 'in'; + $xoff = $origheight - 0.9; + $yoffl = 0.0; + } elsif ($paper eq 'legal') { + $ptype = ''; + $origwidth = 14.0; + $origheight = 8.5; + $origunits = 'in'; + $xoff = $origheight - 3.0; + $yoffl = 0.5; + } elsif ($paper eq 'a4') { + $origwidth = 29.7; + $origheight = 21.0; + $origunits = 'cm'; + $xoff = $origheight; + $yoffl = 0.7; + } + if (($origwidth ne '') && ($yoffl ne '')) { + $yoffr = $origwidth/2 + $yoffl; + } + return ($ptype,$xoff,$yoffl,$yoffr,$origunits); +} + $|=1; if (! &LONCAPA::loncgi::check_cookie_and_load_env()) { print < '', + 'text' => 'Helper'}, #FIXME: Different origin possible than print out helper? + {'href' => '', + 'text' => 'Preparing Printout'}, + {'href' => '', + 'text' => 'Creating PDF'}]; +print(&Apache::loncommon::start_page('Creating PDF', + undef, + {'bread_crumbs' => $brcrum,})); my $identifier = $ENV{'QUERY_STRING'}; my $texfile = $env{'cgi.'.$identifier.'.file'}; @@ -191,7 +233,7 @@ my $tableofindex = $env{'cgi.'.$identifi my $advanced_role = $env{'cgi.'.$identifier.'.role'}; my $number_of_files = $env{'cgi.'.$identifier.'.numberoffiles'}+1; my $student_names = $env{'cgi.'.$identifier.'.studentnames'}; -my $backref = &Apache::lonnet::unescape($env{'cgi.'.$identifier.'.backref'}); +my $backref = &unescape($env{'cgi.'.$identifier.'.backref'}); my @names_pack=(); @@ -199,8 +241,13 @@ if ($student_names=~/_END_/) { @names_pack=split(/_ENDPERSON_/,$student_names); } if ($backref) { - print('

'.&mt("[_1]Return[_2] to editing resource.", + if ($backref =~ m{^(/uploaded/$LONCAPA::match_domain/$LONCAPA::match_courseid/default_\d+.page)}) { + $backref = $1; + } + print('

'.&mt("[_1]Return[_2] to resource.", "","").'

'); + print('

'. + &mt("Change Printing Options").'

'."\n"); } my $figfile = $texfile; $figfile =~ s/^(.*_printout)_\d+_\d+_\d+\.tex/$1\.dat/; @@ -227,7 +274,7 @@ if (-e $figfile) { close $temporary_file; my $noteps; my %prog_state; - if ($advanced_role) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Converting Images to EPS','Picture Conversion Status',$#content_of_file,'inline','80'); } + if ($advanced_role) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('',$#content_of_file); } print('
'); foreach my $not_eps (@content_of_file) { chomp($not_eps); @@ -273,7 +320,7 @@ my $ind=-1; my %prog_state; if ($advanced_role) { - %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Print Status',&mt('Class Print Status'),$number_of_files,'inline','80'); + %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('',$number_of_files); } print "
"; my $num_files = @texfile; # How does this differ from $number_of_files , can that be 0? @@ -298,14 +345,23 @@ foreach $texfile (@texfile) { my $name; my $name_range=''; - # $name -> Either user's full name or username@domain - # $name_range -> Either user's last name or usrname. + # $name -> Either user's full name or username:domain + # $name_range -> Either user's last name or username. if ($tempo_array[3]) { $name=$tempo_array[3]; - ($name_range) = split(/,/,$name, 2); + $name =~ s{^\s+|\s+$}{}g; + if ($name =~ /,/) { + ($name_range) = split(/,/,$name, 2); + } elsif ($name =~ /\s/) { + $name_range = $name; + $name_range =~ s/\s+/_/; + } else { + $name_range = $name; + } + $name_range =~ s/[^\w\:\-]+//g; } else { - $name=$tempo_array[0].'@'.$tempo_array[1]; + $name=$tempo_array[0].':'.$tempo_array[1]; $name_range = $tempo_array[0]; } @@ -313,7 +369,7 @@ foreach $texfile (@texfile) { # user is getting printed. # - if (($name ne "") && ($name ne '@') ) { # Could be printing codes... + if (($name ne "") && ($name ne ':') ) { # Could be printing codes... $link_text=''.$name.''; $status_statement.=$name; } @@ -326,13 +382,23 @@ foreach $texfile (@texfile) { @tempo_array=split(/:/,$stud_info[-1]); if ($tempo_array[3]) { $name=$tempo_array[3]; - my ($lastname) = split(/,/, $name,2); + $name =~ s{^\s+|\s+$}{}g; + my $lastname; + if ($name =~ /,/) { + ($lastname) = split(/,/, $name,2); + } elsif ($name =~ /\s/) { + $lastname = $name; + $lastname =~ s/\s+/_/; + } else { + $lastname = $name; + } $name_range .= "-".$lastname; + $name_range =~ s/[^\w\:\-]+//g; } else { - $name=$tempo_array[0].'@'.$tempo_array[1]; + $name=$tempo_array[0].':'.$tempo_array[1]; $name_range .= '-'.$tempo_array[0]; } - if (($name ne "") && ($name ne '@')) { + if (($name ne "") && ($name ne ':')) { $link_text.=' - '.$name.''; $status_statement.=' - '.$name; @@ -362,7 +428,7 @@ foreach $texfile (@texfile) { print "
"; if ($advanced_role) { - &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,'Creating PDF for '.$status_statement); + &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,&mt('Creating PDF for: [_1]',$status_statement)); } if (-e $texfile) { # Ensure the tex file exists: @@ -409,9 +475,13 @@ foreach $texfile (@texfile) { $name_file =~ s/\.tex/\.dvi/; my $new_name_file = $name_file; $new_name_file =~ s/\.dvi/\.ps/; - my $papera=$paper; - if ($papera eq 'letter') {$papera='';} - if ($papera ne '') {$papera='-t'.$papera;} +# Explicitly include a switch for papertype, otherwise dvips will default +# to whatever is listed first in config.ps (which in most cases is a4). + my $papera; + unless (($paper eq '') || + (($laystyle eq 'album') && ($numberofcolumns eq '1'))) { + $papera='-t'.$paper; + } my $extra_ps_header = $perlvar{'lonLib'} .'/includepsheader.ps'; my $comma = "dvips $papera -h $extra_ps_header -Ppdf -G0 -o $new_name_file"; &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null", @@ -466,15 +536,100 @@ foreach $texfile (@texfile) { $new_name_file =~ m/^(.*)\./; my $ps_file = my $tempo_file = $1.'temporar.ps'; my $pdf_file = $1.'.pdf'; - $papera=~s/t/p/; - $comma = "fixps --force $new_name_file"; - &debug("FIXPS command: $comma"); - &busy_wait_command("$comma 1>/dev/null 2>/dev/null", - "for $status_statement now validating PS", - \%prog_state,$tempo_file); + unless ($paper eq '') { + $papera='-p'.$paper; + } +#---- +# The code below uses fixps to make pdf include in sequences work. +# +# $comma = "fixps --force $new_name_file"; +# &debug("FIXPS command: $comma"); +# &busy_wait_command("$comma 1>$tempo_file 2>/dev/null", +# "for $status_statement now validating PS", +# \%prog_state,$tempo_file); + +#--- +# The code below uses gs to make pdf includes in sequences work + + # Use gs to fix the postscript -> level 1.5 + # .. if pdfs were included + # + # pswrite device was removed from ghostscript 9.09 and later, + # (ps2write device is used instead). + # check which device is available, and use as the value + # passed via -sDEVICE= arg in gs call to fix the postscript. + # + + if ($pdfs_converted > 0) { + my @possdevices = qw(ps2write pswrite); + my $device; + foreach my $poss (@possdevices) { + if (open(PIPE,"gs -h |grep ' $poss ' 2>&1 |")) { + my $output = ; + close(PIPE); + chomp($output); + if ($output =~ /\Q $poss \E/) { + $device = $poss; + } + } + last if ($device ne ''); + } + if ($device ne '') { + my ($major,$minor); + if (open(PIPE,"gs -v |grep 'GPL Ghostscript' 2>&1 |")) { + my $info = ; + close(PIPE); + chomp($info); + if ($info =~ /Ghostscript\s+(\d+)\.(\d+)/) { + ($major,$minor) = ($1,$2); + } + } + $comma = "gs -sDEVICE=$device -dLanguageLevel=1.5 "; + if (($major > 9) || (($major == 9) && ($minor >= 50))) { + $comma .= '--permit-file-read=* '; + } + &busy_wait_command("$comma -o $tempo_file $new_name_file 2>/dev/null 1>/dev/null", + "for $status_statement now validating PS", + \%prog_state, $tempo_file); + +#--- + if (-e $tempo_file) { + &busy_wait_command("mv $tempo_file $new_name_file", + 'File move', \%prog_state, $new_name_file); + } + } + } if ($laystyle eq 'album' and $numberofcolumns eq '2') { - $comma = "psnup $papera -2 -s1.0 $new_name_file"; - &debug("PSNUP command: $comma"); + my $canscale; + if (open(PIPE,"psnup --version 2>&1 |")) { + while () { + chomp(); + next if (/pstops:\s+invalid\s+option/); + if (/^psnup\s+release\s+(\d+)\s+patchlevel\s+(\d+)/) { + if (($1 == 1) && ($2 < 90)) { + $canscale = 1; + } + last; + } elsif (/^psnup\s+(\d+)\.(\d+)/) { + if (($1 == 1) && ($2 < 90)) { + $canscale = 1; + } + last; + } + } + close(PIPE); + } + if ($canscale) { + $comma = "psnup $papera -2 -s1.0 $new_name_file"; + &debug("PSNUP command: $comma"); + } elsif (($paper eq 'letter') || ($paper eq 'legal') || ($paper eq 'a4')) { + my ($ptype,$xoff,$yoffl,$yoffr,$units) = &get_pstops_offsets($paper); + $comma = "pstops $ptype '2:0L\@1.0($xoff$units,$yoffl$units)+1L\@1.0($xoff$units,$yoffr$units)' $new_name_file"; + &debug("PSTOPS command: $comma"); + } else { + $comma = "psnup $papera -2 $new_name_file"; + &debug("PSNUP command: $comma"); + } &busy_wait_command("$comma $tempo_file 1>/dev/null 2>/dev/null", "for $status_statement now Modifying PS layout", \%prog_state,$tempo_file); @@ -487,7 +642,8 @@ foreach $texfile (@texfile) { } else { $ps_file=$new_name_file; } - my $addtoPSfile={'legal'=>'<< /PageSize [612 1008] >> setpagedevice', + my $addtoPSfile={'letter'=>'<< /PageSize [612 792] >> setpagedevice', + 'legal'=>'<< /PageSize [612 1008] >> setpagedevice', 'tabloid'=>'<< /PageSize [792 1224] >> setpagedevice', 'executive'=>,'<< /PageSize [540 720] >> setpagedevice', 'a2'=>'<< /PageSize [1195.02 1690.09] >> setpagedevice', @@ -495,19 +651,17 @@ foreach $texfile (@texfile) { 'a4'=>'<< /PageSize [595.2 842] >> setpagedevice', 'a5'=>'<< /PageSize [421.1 595.2] >> setpagedevice', 'a6'=>'<< /PageSize [298.75 421.1] >> setpagedevice', - }; - if ($paper ne 'letter') { - open(FFH,'<',$ps_file) || die "Couldn't open ps file $ps_file for reading: $!\n"; - my $new_ps_file='new'.$ps_file; - open(FFHS,'>',$new_ps_file) || die "Couldn't open new ps file $new_ps_file for reading: $!\n"; - print FFHS $addtoPSfile->{$paper}."\n"; - while () { - print FFHS $_; - } - close(FFH); - close(FFHS); - $ps_file=$new_ps_file; + }; + open(FFH,'<',$ps_file) || die "Couldn't open ps file $ps_file for reading: $!\n"; + my $new_ps_file='new'.$ps_file; + open(FFHS,'>',$new_ps_file) || die "Couldn't open new ps file $new_ps_file for reading: $!\n"; + print FFHS $addtoPSfile->{$paper}."\n"; + while () { + print FFHS $_; } + close(FFH); + close(FFHS); + $ps_file=$new_ps_file; &busy_wait_command("ps2pdf13 $ps_file $pdf_file 1>/dev/null 2>/dev/null", "for $status_statement now Converting PS to PDF", \%prog_state,$pdf_file); @@ -523,32 +677,63 @@ foreach $texfile (@texfile) { my @garb = ($texlog,$texaux,$texdvi,$texps); # unlink @garb; unlink($duefile); - print "$link_text - click here to download pdf"; - print "\n"; + print + '

' + .&mt('[_1] - [_2]Your PDF file[_3] is ready for download.', + $link_text,'','') + .'

'."\n"; } unlink($missfonts_file); } } else { - print "LaTeX file $texfile was not created successfully"; + print + '

' + .&mt('The LaTeX file [_1] was not created successfully.', + ''.$texfile.'') + .'

'; } } +if ($advanced_role) { &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); } print "
"; if ($number_of_files>1) { + 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 $statement="zip $zipfile"; + my $zip = Archive::Zip->new(); + my $counter = 0; foreach my $file (@texfile) { - $file=~s/\.tex/.\pdf/; - $statement.=' '.$file; + $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')); } - print("

Zip Output:\n");
-    system($statement);
-    print("
"); - $zipfile=~s{^\Q$perlvar{'lonPrtDir'}\E}{/prtspool}; - print "
A ZIP file of all the PDFs."; + 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); } } -if ($advanced_role) { &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); } print(&Apache::loncommon::end_page()); my $done; @@ -584,7 +769,7 @@ sub busy_wait_command { if ($size == $last_size) { $unchanged_time++; if ($timeout && ($unchanged_time > $timeout)) { - print "

Operation timed out!

\n"; + 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"; @@ -599,8 +784,7 @@ sub busy_wait_command { } } if ($advanced_role) { - &Apache::lonhtmlcommon::Update_PrgWin('',$progress_win, - $message.$extra_msg); + &Apache::lonhtmlcommon::Update_PrgWin('',$progress_win,$message.$extra_msg); } } $SIG{CHLD}='IGNORE'; @@ -697,8 +881,7 @@ sub repaginate { 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)); + &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,&mt("Counting pages for student: [_1]",1)); while ($line = ) { @@ -726,8 +909,7 @@ sub repaginate { # - save the page_number, reset and, if necessary # update max_pages. push(@pages_in_student, $page_number); - &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state, - &mt("Counting pages for student: [_1]", scalar(@pages_in_student))); + &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,&mt("Counting pages for student: [_1]", scalar(@pages_in_student))); if ($page_number > $max_pages) { $max_pages = $page_number; } @@ -739,8 +921,7 @@ sub repaginate { } # file ended so one more student push(@pages_in_student, $page_number); - &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state, - &mt("Counting pages for student: [_1]",scalar(@pages_in_student))); + &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,&mt("Counting pages for student: [_1]",scalar(@pages_in_student))); if ($page_number > $max_pages) { $max_pages = $page_number; } @@ -767,8 +948,7 @@ sub repaginate { my $student_number = 0; # Index of student we're working on. - &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state, - "Repaginating student ".$student_number+1); + &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,&mt("Repaginating student: [_1]",$student_number+1)); while (my $line = ) { if ($line eq "\\special{ps:ENDOFSTUDENTSTAMP}\n") { @@ -785,8 +965,7 @@ sub repaginate { } $student_number++; - &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state, - "Repaginating student ".$student_number+1); + &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,&mt("Repaginating student: [_1]",$student_number+1)); } else { print LATEXOUT $line; @@ -867,8 +1046,8 @@ sub convert_figure { my $status_statement='EPS picture for '.$not_eps; my $eps_f = $not_eps; - if ($eps_f=~/\/home\/([^\/]+)\/public_html\//) { - $eps_f=~s/\/home\/([^\/]+)\/public_html/$1/; + if ($eps_f=~/\/home\/httpd\/html\/priv\/[^\/]+\/([^\/]+)\//) { + $eps_f=~s/\/home\/httpd\/html\/priv\/[^\/]+\/([^\/]+)/$1/; } elsif ($eps_f=~/$perlvar{'lonDocRoot'}\/res\//) { $eps_f=~ s/$perlvar{'lonDocRoot'}\/res\/(.+)/$1/; } elsif ($eps_f=~/$perlvar{'lonUsersDir'}\//) { @@ -879,7 +1058,7 @@ sub convert_figure { # Spaces are problematic for system commands and LaTeX, replace with _ - $eps_f =~ s/ /\_/g; + $eps_f =~ s/ /\_/g; # # If the file is already an .eps or .ps file (eps_f still has the original @@ -898,13 +1077,10 @@ sub convert_figure { &File::Path::mkpath($path,0,0777); $not_eps =~ s/^\s+//; $not_eps =~ s/\s+$//; - $not_eps =~ s/ /\\ /g; + my $prettyname=$not_eps; if ($advanced_role) { - my $prettyname=$not_eps; - $prettyname=~s|/home/([^/]+)/public_html|/priv/$1|; $prettyname=~s|$perlvar{'lonDocRoot'}/|/|; - &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state, - 'Converting to EPS '.$prettyname); + &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,&mt('Converting to EPS: [_1]',$prettyname)); } # # If the file is a PDF, need to use pdftops to convert it to a ps file. @@ -920,9 +1096,29 @@ sub convert_figure { $eps_f = $perlvar{'lonPrtDir'}.'/'.$eps_f; &debug("Converting pdf $not_eps to postscript: $eps_f"); - system("pdftops $not_eps $eps_f"); + my @args = ('pdftops',$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] to postscript.', + ''.$prettyname.'') + .'

'; + } else { + $pdfs_converted++; # Need to fix ps in last pass. + } } else { - system("convert $not_eps $eps_f"); + 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.') + .'

'; + } } if (not -e $eps_f) { @@ -1010,7 +1206,7 @@ sub analyze_logfile { print "

".&mt('LaTeX could not successfully parse your TeX file.')."

"; print &mt('It probably has errors in it.')."
"; if ($badtext) { - print &mt('With very high probability this error occured in [_1].',$badtext) + print &mt('With very high probability this error occurred in [_1].',$badtext) ."

"; } print &mt('Here are the error messages in the LaTeX log file:') @@ -1099,7 +1295,7 @@ sub analyze_logfile { my $whereitends = index $body_log_file,'STAMPOFPASSEDRESOURCEEND',$whereitbegins; print "
" .&mt('It has found an error in [_1][_2]and corrected it.',substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26),"
")."\n"; - print &mt('Usually this correction is valid but you probably need to check the indicated resource one more time and implement neccessary corrections by yourself.')."\n"; + print &mt('Usually this correction is valid but you probably need to check the indicated resource one more time and implement necessary corrections by yourself.')."\n"; $whereitbegins = index $body_log_file,'',$tempobegin+10; }