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