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