1: #!/usr/bin/perl
2: # CGI-script to run LaTeX, dvips, ps2ps, ps2pdf etc.
3: #
4: # $Id: printout.pl,v 1.45 2004/02/06 14:16:39 sakharuk Exp $
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:
29: use lib '/home/httpd/lib/perl';
30: use Time::Local;
31: use LONCAPA::loncgi();
32: use File::Path;
33: use IO::File;
34: use Image::Magick;
35:
36: if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
37: print <<END;
38: Content-type: text/html
39:
40: <html>
41: <head><title>Bad Cookie</title></head>
42: <body>
43: Your cookie information is incorrect.
44: </body>
45: </html>
46: END
47: return;
48: }
49:
50: print "Content-type: text/html\n\n";
51: print "<body bgcolor=\"#FFFFFF\">\n";
52:
53: my $identifier = $ENV{'QUERY_STRING'};
54: my $texfile = $ENV{'cgi.'.$identifier.'.file'};
55: my $laystyle = $ENV{'cgi.'.$identifier.'.layout'};
56: my $numberofcolumns = $ENV{'cgi.'.$identifier.'.numcol'};
57: my $selectionmade = $ENV{'cgi.'.$identifier.'.selection'};
58: my $tableofcontents = $ENV{'cgi.'.$identifier.'tableofcontents'};
59: my $tableofindex = $ENV{'cgi.'.$identifier.'tableofindex'};
60: my $advans_role = $ENV{'cgi.'.$identifier.'role'};
61: my $back_ref = $ENV{'cgi.'.$identifier.'backref'};
62: my $number_of_files = $ENV{'cgi.'.$identifier.'numberoffiles'}+1;
63: my $student_names = $ENV{'cgi.'.$identifier.'studentnames'};
64:
65: my @names_pack=();
66: if ($student_names=~/_END_/) {
67: @names_pack=split(/_ENDPERSON_/,$student_names);
68: }
69:
70: my $figfile = $texfile;
71: $figfile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.dat/;
72: my $duefile = $texfile;
73: $duefile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.due/;
74: #do we have figures?
75: if (-e $figfile) {
76: my %done_conversion;
77: my $temporary_file=IO::File->new($figfile) || die "Couldn't open file for reading: $!\n";
78: my @content_of_file = <$temporary_file>;
79: close $temporary_file;
80: my $noteps;
81: &Create_StatWin ('Starting eps pictures creation', 'Pictires Status window');
82: foreach $not_eps (@content_of_file) {
83: if ($not_eps ne '') {
84: my $status_statement='EPS picture for '.$not_eps;
85: &Update_StaWin ($status_statement);
86: $not_eps=~s|\/\.\/|\/|g;
87: my $eps_f = $not_eps;
88: $eps_f =~ s/\.[^.]*$/\.eps/i;
89: if ($eps_f=~/\/home\/([^\/]+)\/public_html\//) {
90: $eps_f=~s/\/home\/([^\/]+)\/public_html/$1/;
91: $eps_f = '/home/httpd/prtspool/'.$eps_f;
92: } else {
93: $eps_f=~m/\/home\/httpd\/html\/res\/(.+)/;
94: $eps_f = '/home/httpd/prtspool/'.$1;
95: }
96: my $path=$eps_f;
97: $path=~s/\/([^\/]+)\.eps$//;
98: File::Path::mkpath($path,0,0777);
99: my $image = Image::Magick->new;
100: $not_eps =~ s/^\s+//;
101: $not_eps =~ s/\s+$//;
102: if ( exists($done_conversion{$not_eps})) {
103: next;
104: }
105: $done_conversion{$not_eps}=1;
106: $status = $image->Read($not_eps);
107: if ($status) {print " $status ";}
108: $image->Set(page => '+100+200');
109: $status = $image->Write($eps_f);
110: if ($status) {print " $status ";}
111: #check is eps exist in prtspool
112: if(not -e $eps_f) {
113: for (my $i=0;$i<10000;$i++) {
114: if (-e $eps_f.'.'.$i) {
115: rename $eps_f.'.'.$i, $eps_f;
116: last;
117: }
118: }
119: }
120: }
121: }
122: &Close_StatWin();
123: }
124: #print "$texfile\n"; #name of the tex file for debugging only
125: my @texfile=($texfile);
126: if ($number_of_files>1) {
127: for (my $i=1;$i<=$number_of_files-1;$i++) {
128: my $new_texfile=$texfile;
129: $new_texfile=~s/\.tex/_add$i\.tex/;
130: push @texfile,$new_texfile;
131: }
132: }
133: my $ind=-1;
134: &Create_StatWin ('Starting PDF production for students', 'PDF Status window');
135: foreach $texfile (@texfile) {
136: my $final_statement="<b>Link to your PDF document:</b> ";
137: my $status_statement='PDF document for ';
138: if ($number_of_files>1) {
139: $ind++;
140: my @stud_info=split(/_END_/,$names_pack[$ind]);
141: my @tempo_array=split(/:/,$stud_info[0]);
142: $final_statement='Link to PDF document for <b>'.$tempo_array[3].'</b> ';
143: $status_statement.=$tempo_array[3];
144: if ($#stud_info>0) {
145: @tempo_array=split(/:/,$stud_info[-1]);
146: $final_statement.='- <b>'.$tempo_array[3].':</b> ';
147: $status_statement.=' - '.$tempo_array[3];
148: }
149: }
150: &Update_StaWin ($status_statement);
151: if (-e $texfile) {
152: $texfile =~ m/^(.*)\/([^\/]+)$/;
153: my $name_file = $2;
154: my $path_file = $1.'/';
155: chdir $path_file;
156: system("latex $name_file 1>/dev/null 2>/dev/null");
157: if ($tableofcontents eq 'yes') {
158: system("latex $name_file 1>/dev/null 2>/dev/null");
159: } #to create table of contents
160: my $idxname=$name_file;
161: $idxname=~s/\.tex$/\.idx/;
162: if ($tableofindex eq 'yes') {
163: system("makeindex $idxname");
164: system("latex $name_file 1>/dev/null 2>/dev/null");
165: } #to create index
166: #Do we have a latex error in the log file?
167: my $logfilename = $texfile;
168: $logfilename =~ s/\.tex$/\.log/;
169: my $temporary_file=IO::File->new($logfilename) || die "Couldn't open file for reading: $!\n";
170: my @content_of_file = <$temporary_file>;
171: close $temporary_file;
172: my $body_log_file = join(' ',@content_of_file);
173: $logfilename =~ s/\.log$/\.html/;
174: $temporary_file = IO::File->new('>'.$logfilename);
175: print $temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_log_file.'</pre></body></html>'."\n";
176: if ($body_log_file=~m/!\s+Emergency stop/) {
177: #LaTeX failed to parse tex file
178: print "<h2>LaTeX could not successfully parse your tex file.</h2>";
179: print "It probably has errors in it.<br />";
180: my $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART';
181: my $whereitends = rindex $body_log_file,'STAMPOFPASSEDRESOURCEEND';
182: if ($whereitbegins!=-1 and $whereitends!=-1) {
183: print "With very high probability this error occured in ".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)."<br /><br />";
184: }
185: print "Here are the error messages in the LaTeX log file</br><br />";
186: my $sygnal = 0;
187: for (my $i=0;$i<=$#content_of_file;$i++) {
188: if ($content_of_file[$i]=~m/^Runaway argument?/ or $content_of_file[$i]=~m/^!/) {
189: $sygnal = 1;
190: }
191: if ($content_of_file[$i]=~m/Here is how much of/) {
192: $sygnal = 0;
193: }
194: if ($sygnal) {
195: print "$content_of_file[$i]<br />";
196: }
197: }
198: if ($advans_role) {
199: print "<b><big>The link to ";
200: $logfilename=~s/\/home\/httpd//;
201: print "<a href=\"$logfilename\">Your log file </a></big></b>";
202: print "\n";
203: #link tooriginal LaTeX file (included according Michael Hamlin desire)
204: my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
205: my @tex_content_of_file = <$tex_temporary_file>;
206: close $tex_temporary_file;
207: my $body_tex_file = join(' ',@tex_content_of_file);
208: $texfile =~ s/\.tex$/aaaaa\.html/;
209: $tex_temporary_file = IO::File->new('>'.$texfile);
210: print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
211: print "<br /><br />";
212: print "<b><big>The link to ";
213: $texfile=~s/\/home\/httpd//;
214: print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
215: print "\n";
216: }
217:
218: } elsif ($body_log_file=~m/<inserted text>/) {
219: my $whereitbegins = index $body_log_file,'<inserted text>';
220: print "You are running LaTeX in the <b>batch mode</b>.";
221: while ($whereitbegins != -1) {
222: my $tempobegin=$whereitbegins;
223: $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART',$whereitbegins;
224: my $whereitends = index $body_log_file,'STAMPOFPASSEDRESOURCEEND',$whereitbegins;
225: print "<br />It has found an error in".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)." <br /> and corrected it.\n";
226: 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";
227: $whereitbegins = index $body_log_file,'<inserted text>',$tempobegin+10;
228: }
229: $name_file =~ s/\.tex/\.dvi/;
230: my $new_name_file = $name_file;
231: $new_name_file =~ s/\.dvi/\.ps/;
232: my $comma = "dvips -Ppdf -G0 -o $new_name_file";
233: system("$comma $name_file 1>/dev/null 2>/dev/null");
234: if (-e $new_name_file) {
235: print "<h1>PDF output file (see link below)</h1>\n";
236: $new_name_file =~ m/^(.*)\./;
237: my $tempo_file = $1.'temporar.ps';
238: my $name_file = $1.'.pdf';
239: if ($laystyle eq 'album' and $numberofcolumns eq '2') {
240: $comma = "psnup -2 -s1.0 $new_name_file";
241: system("$comma $tempo_file 1>/dev/null 2>/dev/null");
242: system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
243: } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
244: $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
245: system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
246: system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
247: } else {
248: system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
249: }
250: my $texlog = $texfile;
251: my $texaux = $texfile;
252: my $texdvi = $texfile;
253: my $texps = $texfile;
254: $texlog =~ s/\.tex/\.log/;
255: $texaux =~ s/\.tex/\.aux/;
256: $texdvi =~ s/\.tex/\.dvi/;
257: $texps =~ s/\.tex/\.ps/;
258: my @garb = ($texaux,$texdvi,$texps);
259: # unlink @garb;
260: unlink $duefile;
261: print "<a href=\"/prtspool/$name_file\">Your PDF document</a>";
262: if ($advans_role) {
263: print "<br /><br />";
264: print "<b><big>The link to ";
265: $logfilename=~s/\/home\/httpd//;
266: print "<a href=\"$logfilename\">Your log file </a></big></b>";
267: print "\n";
268: #link tooriginal LaTeX file (included according Michael Hamlin desire)
269: my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
270: my @tex_content_of_file = <$tex_temporary_file>;
271: close $tex_temporary_file;
272: my $body_tex_file = join(' ',@tex_content_of_file);
273: $texfile =~ s/\.tex$/aaaaa\.html/;
274: $tex_temporary_file = IO::File->new('>'.$texfile);
275: print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
276: print "<br /><br />";
277: print "<b><big>The link to ";
278: $texfile=~s/\/home\/httpd//;
279: print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
280: print "\n";
281: }
282: }
283: } else {
284: #LaTeX successfully parsed tex file
285: $name_file =~ s/\.tex/\.dvi/;
286: my $new_name_file = $name_file;
287: $new_name_file =~ s/\.dvi/\.ps/;
288: my $comma = "dvips -Ppdf -G0 -o $new_name_file";
289: system("$comma $name_file 1>/dev/null 2>/dev/null");
290: if (-e $new_name_file) {
291: print "<br />$final_statement ";
292: $new_name_file =~ m/^(.*)\./;
293: my $tempo_file = $1.'temporar.ps';
294: my $name_file = $1.'.pdf';
295: if ($laystyle eq 'album' and $numberofcolumns eq '2') {
296: $comma = "psnup -2 -s1.0 $new_name_file";
297: system("$comma $tempo_file 1>/dev/null 2>/dev/null");
298: system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
299: } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
300: $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
301: system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
302: system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
303: } else {
304: system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
305: }
306: my $texlog = $texfile;
307: my $texaux = $texfile;
308: my $texdvi = $texfile;
309: my $texps = $texfile;
310: $texlog =~ s/\.tex/\.log/;
311: $texaux =~ s/\.tex/\.aux/;
312: $texdvi =~ s/\.tex/\.dvi/;
313: $texps =~ s/\.tex/\.ps/;
314: my @garb = ($texlog,$texaux,$texdvi,$texps);
315: # unlink @garb;
316: unlink $duefile;
317: print "<a href=\"/prtspool/$name_file\">Your PDF document</a>";
318: print "\n";
319: }
320: }
321: } else {
322: print "LaTeX file $texfile was not created successfully";
323: }
324: }
325: print "<br />";
326: if ($number_of_files>1) {
327: my $zipfile=$texfile[0];
328: $zipfile=~s/\.tex/\.zip/;
329: my $statement="zip $zipfile";
330: foreach my $file (@texfile) {
331: $file=~s/\.tex/.\pdf/;
332: $statement.=' '.$file;
333: }
334: system("$statement");
335: $zipfile=~s/\/home\/httpd//;
336: print "<br /> <a href=\"$zipfile\">Your ZIP file is here</a>";
337: }
338: &Close_StatWin();
339:
340:
341: sub Create_StatWin {
342: my ($title, $heading)=@_;
343: print('<script>'.
344: "popwin=window.open('','popwin','width=400,height=100');".
345: "popwin.document.writeln('<html><head><title>$title</title></head>".
346: "<body bgcolor=\"#88DDFF\">".
347: "<h4>$heading</h4>".
348: "<form name=popremain>".
349: '<input type="text" size="55" name="remaining" value="'.
350: 'Starting'.'"></form>'.
351: "</body></html>');".
352: "popwin.document.close();".
353: "</script>");
354: }
355:
356: sub Update_StaWin {
357: my ($info)=@_;
358: print('<script>popwin.document.popremain.remaining.value="'.
359: $info.'";'.'</script>');
360: }
361:
362: sub Close_StatWin {
363: print('<script>popwin.window.close()</script>'."\n");
364:
365: }
366:
367:
368:
369:
370:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>