Annotation of loncom/interface/printout.pl, revision 1.56
1.1 sakharuk 1: #!/usr/bin/perl
1.37 albertel 2: # CGI-script to run LaTeX, dvips, ps2ps, ps2pdf etc.
3: #
1.56 ! sakharuk 4: # $Id: printout.pl,v 1.55 2004/05/06 06:45:13 albertel Exp $
1.37 albertel 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28:
1.39 sakharuk 29: use lib '/home/httpd/lib/perl';
1.44 sakharuk 30: use Time::Local;
1.39 sakharuk 31: use LONCAPA::loncgi();
1.38 sakharuk 32: use File::Path;
1.6 sakharuk 33: use IO::File;
1.7 sakharuk 34: use Image::Magick;
1.47 sakharuk 35: use Apache::lonhtmlcommon;
1.51 albertel 36: use Apache::loncommon;
37: use Apache::lonlocal;
1.47 sakharuk 38:
1.49 albertel 39: $|=1;
1.46 albertel 40: my %origENV=%ENV;
1.39 sakharuk 41: if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
42: print <<END;
43: Content-type: text/html
44:
45: <html>
46: <head><title>Bad Cookie</title></head>
47: <body>
1.40 sakharuk 48: Your cookie information is incorrect.
1.39 sakharuk 49: </body>
50: </html>
51: END
52: return;
53: }
1.51 albertel 54: &Apache::lonlocal::get_language_handle();
55: &Apache::loncommon::content_type(undef,'text/html');
56: my $bodytag=&Apache::loncommon::bodytag('Creating PDF','','');
57: print $bodytag;
1.39 sakharuk 58:
1.40 sakharuk 59: my $identifier = $ENV{'QUERY_STRING'};
60: my $texfile = $ENV{'cgi.'.$identifier.'.file'};
61: my $laystyle = $ENV{'cgi.'.$identifier.'.layout'};
62: my $numberofcolumns = $ENV{'cgi.'.$identifier.'.numcol'};
63: my $selectionmade = $ENV{'cgi.'.$identifier.'.selection'};
64: my $tableofcontents = $ENV{'cgi.'.$identifier.'tableofcontents'};
65: my $tableofindex = $ENV{'cgi.'.$identifier.'tableofindex'};
66: my $advans_role = $ENV{'cgi.'.$identifier.'role'};
1.43 sakharuk 67: my $number_of_files = $ENV{'cgi.'.$identifier.'numberoffiles'}+1;
1.44 sakharuk 68: my $student_names = $ENV{'cgi.'.$identifier.'studentnames'};
1.47 sakharuk 69: my $backref = $ENV{'cgi.'.$identifier.'backref'};
1.43 sakharuk 70:
1.49 albertel 71: my $adv = $ENV{'request.role.adv'};
72:
1.44 sakharuk 73: my @names_pack=();
74: if ($student_names=~/_END_/) {
75: @names_pack=split(/_ENDPERSON_/,$student_names);
76: }
1.46 albertel 77: #got what we needed reset ENV in case it is to big for system
78: %ENV=%origENV;
1.39 sakharuk 79:
1.14 sakharuk 80: my $figfile = $texfile;
81: $figfile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.dat/;
82: my $duefile = $texfile;
83: $duefile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.due/;
84: #do we have figures?
85: if (-e $figfile) {
1.42 albertel 86: my %done_conversion;
1.14 sakharuk 87: my $temporary_file=IO::File->new($figfile) || die "Couldn't open file for reading: $!\n";
88: my @content_of_file = <$temporary_file>;
89: close $temporary_file;
90: my $noteps;
1.49 albertel 91: my %prog_state;
1.55 albertel 92: if ($adv) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Coverting Images to EPS','Picture Conversion Status',$#content_of_file,'inline','75'); }
1.14 sakharuk 93: foreach $not_eps (@content_of_file) {
1.49 albertel 94: chomp($not_eps);
1.14 sakharuk 95: if ($not_eps ne '') {
1.44 sakharuk 96: my $status_statement='EPS picture for '.$not_eps;
1.41 sakharuk 97: $not_eps=~s|\/\.\/|\/|g;
1.14 sakharuk 98: my $eps_f = $not_eps;
99: $eps_f =~ s/\.[^.]*$/\.eps/i;
1.41 sakharuk 100: if ($eps_f=~/\/home\/([^\/]+)\/public_html\//) {
101: $eps_f=~s/\/home\/([^\/]+)\/public_html/$1/;
102: $eps_f = '/home/httpd/prtspool/'.$eps_f;
1.56 ! sakharuk 103: } elsif ($eps_f=~/\/home\/httpd\/html\/res\//) {
1.41 sakharuk 104: $eps_f=~m/\/home\/httpd\/html\/res\/(.+)/;
105: $eps_f = '/home/httpd/prtspool/'.$1;
1.56 ! sakharuk 106: } elsif ($eps_f=~/\/home\/httpd\/lonUsers\//) {
! 107: $eps_f=~/\/home\/httpd\/lonUsers\/([^\/]+)\/\w\/\w\/\w\/(.+)/;
! 108: $eps_f = '/home/httpd/prtspool/'.$1.'/'.$2;
1.41 sakharuk 109: }
1.38 sakharuk 110: my $path=$eps_f;
1.41 sakharuk 111: $path=~s/\/([^\/]+)\.eps$//;
1.38 sakharuk 112: File::Path::mkpath($path,0,0777);
1.14 sakharuk 113: my $image = Image::Magick->new;
114: $not_eps =~ s/^\s+//;
115: $not_eps =~ s/\s+$//;
1.42 albertel 116: if ( exists($done_conversion{$not_eps})) {
117: next;
118: }
1.49 albertel 119: if ($adv) {
120: my $prettyname=$not_eps;
121: $prettyname=~s|/home/([^/]+)/public_html|/priv/$1|;
122: $prettyname=~s|/home/httpd/html/|/|;
123: &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,'Converting to EPS '.$prettyname); }
1.42 albertel 124: $done_conversion{$not_eps}=1;
1.14 sakharuk 125: $status = $image->Read($not_eps);
126: if ($status) {print " $status ";}
127: $image->Set(page => '+100+200');
128: $status = $image->Write($eps_f);
129: if ($status) {print " $status ";}
1.20 sakharuk 130: #check is eps exist in prtspool
131: if(not -e $eps_f) {
132: for (my $i=0;$i<10000;$i++) {
133: if (-e $eps_f.'.'.$i) {
134: rename $eps_f.'.'.$i, $eps_f;
135: last;
136: }
137: }
138: }
1.14 sakharuk 139: }
140: }
1.49 albertel 141: if ($adv) { &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); }
1.52 albertel 142: unlink($figfile);
1.14 sakharuk 143: }
1.43 sakharuk 144: #print "$texfile\n"; #name of the tex file for debugging only
145: my @texfile=($texfile);
146: if ($number_of_files>1) {
1.49 albertel 147: @texfile=();
148: for (my $i=1;$i<=$number_of_files;$i++) {
1.43 sakharuk 149: my $new_texfile=$texfile;
1.49 albertel 150: $new_texfile=~s/\.tex/_$i\.tex/;
1.43 sakharuk 151: push @texfile,$new_texfile;
152: }
153: }
1.49 albertel 154:
1.44 sakharuk 155: my $ind=-1;
1.49 albertel 156: my %prog_state;
1.55 albertel 157: if ($adv) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Print Status','Class Print Status',$number_of_files,'inline','75'); }
1.54 www 158: my $final_statement="<a href=\"$backref\"><b>Return</b></a> to last resource.<br /><br />Generated PDF File for:<br />";
1.43 sakharuk 159: foreach $texfile (@texfile) {
1.49 albertel 160: my $status_statement='';
161: my $link_text='download PDF';
162: $ind++;
163: my @stud_info=split(/_END_/,$names_pack[$ind]);
164: my @tempo_array=split(/:/,$stud_info[0]);
165: my $name;
166: if ($tempo_array[3]) {
167: $name=$tempo_array[3];
168: } else {
169: $name=$tempo_array[0].'@'.$tempo_array[1];
170: }
171: $link_text='<b>'.$name.'</b> ';
172: $status_statement.=$name;
173: if ($#stud_info>0) {
174: @tempo_array=split(/:/,$stud_info[-1]);
1.48 albertel 175: if ($tempo_array[3]) {
176: $name=$tempo_array[3];
177: } else {
178: $name=$tempo_array[0].'@'.$tempo_array[1];
179: }
1.49 albertel 180: $link_text.='- <b>'.$name.':</b> ';
181: $status_statement.=' - '.$name;
182: }
183: if ($adv) { &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,'Creating PDF for '.$status_statement); }
1.29 sakharuk 184: if (-e $texfile) {
185: $texfile =~ m/^(.*)\/([^\/]+)$/;
186: my $name_file = $2;
187: my $path_file = $1.'/';
188: chdir $path_file;
189: system("latex $name_file 1>/dev/null 2>/dev/null");
1.40 sakharuk 190: if ($tableofcontents eq 'yes') {
191: system("latex $name_file 1>/dev/null 2>/dev/null");
192: } #to create table of contents
1.33 sakharuk 193: my $idxname=$name_file;
194: $idxname=~s/\.tex$/\.idx/;
1.40 sakharuk 195: if ($tableofindex eq 'yes') {
1.33 sakharuk 196: system("makeindex $idxname");
197: system("latex $name_file 1>/dev/null 2>/dev/null");
198: } #to create index
1.29 sakharuk 199: #Do we have a latex error in the log file?
200: my $logfilename = $texfile;
201: $logfilename =~ s/\.tex$/\.log/;
202: my $temporary_file=IO::File->new($logfilename) || die "Couldn't open file for reading: $!\n";
203: my @content_of_file = <$temporary_file>;
204: close $temporary_file;
1.30 sakharuk 205: my $body_log_file = join(' ',@content_of_file);
206: $logfilename =~ s/\.log$/\.html/;
207: $temporary_file = IO::File->new('>'.$logfilename);
208: print $temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_log_file.'</pre></body></html>'."\n";
1.29 sakharuk 209: if ($body_log_file=~m/!\s+Emergency stop/) {
210: #LaTeX failed to parse tex file
211: print "<h2>LaTeX could not successfully parse your tex file.</h2>";
212: print "It probably has errors in it.<br />";
213: my $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART';
214: my $whereitends = rindex $body_log_file,'STAMPOFPASSEDRESOURCEEND';
215: if ($whereitbegins!=-1 and $whereitends!=-1) {
1.44 sakharuk 216: print "With very high probability this error occured in ".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)."<br /><br />";
1.29 sakharuk 217: }
218: print "Here are the error messages in the LaTeX log file</br><br />";
219: my $sygnal = 0;
220: for (my $i=0;$i<=$#content_of_file;$i++) {
221: if ($content_of_file[$i]=~m/^Runaway argument?/ or $content_of_file[$i]=~m/^!/) {
222: $sygnal = 1;
223: }
224: if ($content_of_file[$i]=~m/Here is how much of/) {
225: $sygnal = 0;
226: }
227: if ($sygnal) {
228: print "$content_of_file[$i]<br />";
229: }
1.36 sakharuk 230: }
231: if ($advans_role) {
1.35 sakharuk 232: print "<b><big>The link to ";
233: $logfilename=~s/\/home\/httpd//;
234: print "<a href=\"$logfilename\">Your log file </a></big></b>";
235: print "\n";
236: #link tooriginal LaTeX file (included according Michael Hamlin desire)
237: my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
238: my @tex_content_of_file = <$tex_temporary_file>;
239: close $tex_temporary_file;
240: my $body_tex_file = join(' ',@tex_content_of_file);
241: $texfile =~ s/\.tex$/aaaaa\.html/;
242: $tex_temporary_file = IO::File->new('>'.$texfile);
243: print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
244: print "<br /><br />";
245: print "<b><big>The link to ";
246: $texfile=~s/\/home\/httpd//;
247: print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
248: print "\n";
1.36 sakharuk 249: }
1.35 sakharuk 250:
1.29 sakharuk 251: } elsif ($body_log_file=~m/<inserted text>/) {
252: my $whereitbegins = index $body_log_file,'<inserted text>';
1.31 sakharuk 253: print "You are running LaTeX in the <b>batch mode</b>.";
1.30 sakharuk 254: while ($whereitbegins != -1) {
255: my $tempobegin=$whereitbegins;
256: $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART',$whereitbegins;
257: my $whereitends = index $body_log_file,'STAMPOFPASSEDRESOURCEEND',$whereitbegins;
1.34 sakharuk 258: 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 259: 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";
260: $whereitbegins = index $body_log_file,'<inserted text>',$tempobegin+10;
261: }
1.29 sakharuk 262: $name_file =~ s/\.tex/\.dvi/;
263: my $new_name_file = $name_file;
264: $new_name_file =~ s/\.dvi/\.ps/;
265: my $comma = "dvips -Ppdf -G0 -o $new_name_file";
266: system("$comma $name_file 1>/dev/null 2>/dev/null");
267: if (-e $new_name_file) {
268: print "<h1>PDF output file (see link below)</h1>\n";
269: $new_name_file =~ m/^(.*)\./;
270: my $tempo_file = $1.'temporar.ps';
271: my $name_file = $1.'.pdf';
272: if ($laystyle eq 'album' and $numberofcolumns eq '2') {
273: $comma = "psnup -2 -s1.0 $new_name_file";
274: system("$comma $tempo_file 1>/dev/null 2>/dev/null");
275: system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
276: } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
277: $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
278: system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
279: system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
280: } else {
281: system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
282: }
283: my $texlog = $texfile;
284: my $texaux = $texfile;
285: my $texdvi = $texfile;
286: my $texps = $texfile;
287: $texlog =~ s/\.tex/\.log/;
288: $texaux =~ s/\.tex/\.aux/;
289: $texdvi =~ s/\.tex/\.dvi/;
290: $texps =~ s/\.tex/\.ps/;
1.30 sakharuk 291: my @garb = ($texaux,$texdvi,$texps);
1.29 sakharuk 292: # unlink @garb;
293: unlink $duefile;
294: print "<a href=\"/prtspool/$name_file\">Your PDF document</a>";
1.36 sakharuk 295: if ($advans_role) {
296: print "<br /><br />";
297: print "<b><big>The link to ";
298: $logfilename=~s/\/home\/httpd//;
299: print "<a href=\"$logfilename\">Your log file </a></big></b>";
300: print "\n";
301: #link tooriginal LaTeX file (included according Michael Hamlin desire)
302: my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
303: my @tex_content_of_file = <$tex_temporary_file>;
304: close $tex_temporary_file;
305: my $body_tex_file = join(' ',@tex_content_of_file);
306: $texfile =~ s/\.tex$/aaaaa\.html/;
307: $tex_temporary_file = IO::File->new('>'.$texfile);
308: print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
309: print "<br /><br />";
310: print "<b><big>The link to ";
311: $texfile=~s/\/home\/httpd//;
312: print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
313: print "\n";
314: }
1.29 sakharuk 315: }
316: } else {
317: #LaTeX successfully parsed tex file
318: $name_file =~ s/\.tex/\.dvi/;
319: my $new_name_file = $name_file;
320: $new_name_file =~ s/\.dvi/\.ps/;
321: my $comma = "dvips -Ppdf -G0 -o $new_name_file";
322: system("$comma $name_file 1>/dev/null 2>/dev/null");
323: if (-e $new_name_file) {
1.44 sakharuk 324: print "<br />$final_statement ";
1.49 albertel 325: $final_statement='';
1.29 sakharuk 326: $new_name_file =~ m/^(.*)\./;
327: my $tempo_file = $1.'temporar.ps';
328: my $name_file = $1.'.pdf';
329: if ($laystyle eq 'album' and $numberofcolumns eq '2') {
330: $comma = "psnup -2 -s1.0 $new_name_file";
331: system("$comma $tempo_file 1>/dev/null 2>/dev/null");
332: system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
333: } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
334: $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
335: system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
336: system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
337: } else {
338: system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
339: }
340: my $texlog = $texfile;
341: my $texaux = $texfile;
342: my $texdvi = $texfile;
343: my $texps = $texfile;
344: $texlog =~ s/\.tex/\.log/;
345: $texaux =~ s/\.tex/\.aux/;
346: $texdvi =~ s/\.tex/\.dvi/;
347: $texps =~ s/\.tex/\.ps/;
348: my @garb = ($texlog,$texaux,$texdvi,$texps);
1.22 sakharuk 349: # unlink @garb;
1.29 sakharuk 350: unlink $duefile;
1.49 albertel 351: print "<a href=\"/prtspool/$name_file\">$link_text</a>";
1.29 sakharuk 352: print "\n";
353: }
1.14 sakharuk 354: }
1.29 sakharuk 355: } else {
356: print "LaTeX file $texfile was not created successfully";
1.14 sakharuk 357: }
1.43 sakharuk 358: }
1.45 sakharuk 359: print "<br />";
1.44 sakharuk 360: if ($number_of_files>1) {
1.45 sakharuk 361: my $zipfile=$texfile[0];
362: $zipfile=~s/\.tex/\.zip/;
363: my $statement="zip $zipfile";
1.44 sakharuk 364: foreach my $file (@texfile) {
1.45 sakharuk 365: $file=~s/\.tex/.\pdf/;
366: $statement.=' '.$file;
1.44 sakharuk 367: }
1.49 albertel 368: print("<pre>Zip Output:\n");
369: system($statement);
370: print("</pre>");
1.45 sakharuk 371: $zipfile=~s/\/home\/httpd//;
1.49 albertel 372: print "<br /> A <a href=\"$zipfile\">ZIP file</a> of all the PDFs.";
1.44 sakharuk 373: }
1.49 albertel 374: if ($adv) { &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); }
1.1 sakharuk 375:
376:
377:
378:
1.4 sakharuk 379:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>