1: #!/usr/bin/perl
2: # CGI-script to run LaTeX, dvips, ps2ps, ps2pdf etc.
3: #
4: # $Id: printout.pl,v 1.72 2005/02/03 21:37:48 albertel 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 LONCAPA::loncgi();
31: use File::Path;
32: use IO::File;
33: use Image::Magick;
34: use Apache::lonhtmlcommon;
35: use Apache::lonnet();
36: use Apache::loncommon;
37: use Apache::lonlocal;
38:
39: use strict;
40: $|=1;
41: my %origENV=%ENV;
42: if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
43: print <<END;
44: Content-type: text/html
45:
46: <html>
47: <head><title>Bad Cookie</title></head>
48: <body>
49: Your cookie information is incorrect.
50: </body>
51: </html>
52: END
53: return;
54: }
55: &Apache::lonlocal::get_language_handle();
56: &Apache::loncommon::content_type(undef,'text/html');
57: my $bodytag=&Apache::loncommon::bodytag('Creating PDF','','');
58: print $bodytag;
59:
60: my $identifier = $ENV{'QUERY_STRING'};
61: my $texfile = $ENV{'cgi.'.$identifier.'.file'};
62: my $laystyle = $ENV{'cgi.'.$identifier.'.layout'};
63: my $numberofcolumns = $ENV{'cgi.'.$identifier.'.numcol'};
64: my $paper = $ENV{'cgi.'.$identifier.'.paper'};
65: my $selectionmade = $ENV{'cgi.'.$identifier.'.selection'};
66: my $tableofcontents = $ENV{'cgi.'.$identifier.'tableofcontents'};
67: my $tableofindex = $ENV{'cgi.'.$identifier.'tableofindex'};
68: my $advans_role = $ENV{'cgi.'.$identifier.'role'};
69: my $number_of_files = $ENV{'cgi.'.$identifier.'numberoffiles'}+1;
70: my $student_names = $ENV{'cgi.'.$identifier.'studentnames'};
71: my $backref = &Apache::lonnet::unescape($ENV{'cgi.'.$identifier.'backref'});
72:
73:
74: my $adv = $ENV{'request.role.adv'};
75:
76: my @names_pack=();
77: if ($student_names=~/_END_/) {
78: @names_pack=split(/_ENDPERSON_/,$student_names);
79: }
80: #got what we needed reset ENV in case it is to big for system
81: %ENV=%origENV;
82:
83: my $figfile = $texfile;
84: $figfile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.dat/;
85: my $duefile = $texfile;
86: $duefile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.due/;
87: #do we have figures?
88: if (-e $figfile) {
89: my %done_conversion;
90: my $temporary_file=IO::File->new($figfile) || die "Couldn't open fig file $figfile for reading: $!\n";
91: my @content_of_file = <$temporary_file>;
92: close $temporary_file;
93: my $noteps;
94: my %prog_state;
95: if ($adv) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Coverting Images to EPS','Picture Conversion Status',$#content_of_file,'inline','80'); }
96: foreach my $not_eps (@content_of_file) {
97: chomp($not_eps);
98: if ($not_eps ne '') {
99: my $status_statement='EPS picture for '.$not_eps;
100: $not_eps=~s|\/\.\/|\/|g;
101: my $eps_f = $not_eps;
102: $eps_f =~ s/\.[^.]*$/\.eps/i;
103: if ($eps_f=~/\/home\/([^\/]+)\/public_html\//) {
104: $eps_f=~s/\/home\/([^\/]+)\/public_html/$1/;
105: $eps_f = '/home/httpd/prtspool/'.$eps_f;
106: } elsif ($eps_f=~/$Apache::lonnet::perlvar{'lonDocRoot'}\/res\//) {
107: $eps_f=~m/$Apache::lonnet::perlvar{'lonDocRoot'}\/res\/(.+)/;
108: $eps_f = '/home/httpd/prtspool/'.$1;
109: } elsif ($eps_f=~/$Apache::lonnet::perlvar{'lonUsersDir'}\//) {
110: $eps_f=~/$Apache::lonnet::perlvar{'lonUsersDir'}\/([^\/]+)\/\w\/\w\/\w\/(.+)/;
111: $eps_f = '/home/httpd/prtspool/'.$1.'/'.$2;
112: }
113: my $path=$eps_f;
114: $path=~s/\/([^\/]+)\.eps$//;
115: File::Path::mkpath($path,0,0777);
116: $not_eps =~ s/^\s+//;
117: $not_eps =~ s/\s+$//;
118: if ( exists($done_conversion{$not_eps})) { next; }
119: if ($adv) {
120: my $prettyname=$not_eps;
121: $prettyname=~s|/home/([^/]+)/public_html|/priv/$1|;
122: $prettyname=~s|$Apache::lonnet::perlvar{'lonDocRoot'}/|/|;
123: &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,'Converting to EPS '.$prettyname);
124: }
125: $done_conversion{$not_eps}=1;
126: system("convert $not_eps $eps_f");
127: #check is eps exist in prtspool
128: if(not -e $eps_f) {
129: for (my $i=0;$i<10000;$i++) {
130: if (-e $eps_f.'.'.$i) {
131: rename $eps_f.'.'.$i, $eps_f;
132: last;
133: }
134: }
135: }
136: }
137: }
138: if ($adv) { &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); }
139: unlink($figfile);
140: }
141: #print "$texfile\n"; #name of the tex file for debugging only
142: my @texfile=($texfile);
143: if ($number_of_files>1) {
144: @texfile=();
145: for (my $i=1;$i<=$number_of_files;$i++) {
146: my $new_texfile=$texfile;
147: $new_texfile=~s/\.tex/_$i\.tex/;
148: push @texfile,$new_texfile;
149: }
150: }
151:
152: my $ind=-1;
153: my %prog_state;
154: print "<a href=\"$backref\"><b>Return</b></a> to last resource.<br /><br />";
155: if ($adv) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Print Status','Class Print Status',$number_of_files,'inline','80'); }
156: print "<br />";
157: foreach $texfile (@texfile) {
158: my $status_statement='';
159: my $link_text='download PDF';
160: $ind++;
161: my @stud_info=split(/_END_/,$names_pack[$ind]);
162: my @tempo_array=split(/:/,$stud_info[0]);
163: my $name;
164: if ($tempo_array[3]) {
165: $name=$tempo_array[3];
166: } else {
167: $name=$tempo_array[0].'@'.$tempo_array[1];
168: }
169: $link_text='<b>'.$name.'</b>';
170: $status_statement.=$name;
171: if ($#stud_info>0) {
172: @tempo_array=split(/:/,$stud_info[-1]);
173: if ($tempo_array[3]) {
174: $name=$tempo_array[3];
175: } else {
176: $name=$tempo_array[0].'@'.$tempo_array[1];
177: }
178: $link_text.=' - <b>'.$name.'</b>';
179: $status_statement.=' - '.$name;
180: }
181: if ($adv) { &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,'Creating PDF for '.$status_statement); }
182: if (-e $texfile) {
183: $texfile =~ m/^(.*)\/([^\/]+)$/;
184: my $name_file = $2;
185: my $path_file = $1.'/';
186: chdir $path_file;
187: my $dvi_file= $name_file; $dvi_file =~ s/\.tex/\.dvi/;
188: &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
189: "for $status_statement now LaTeXing file",
190: \%prog_state,$dvi_file);
191: if ($tableofcontents eq 'yes') {
192: &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
193: "for $status_statement now LaTeXing file for table of contents",
194: \%prog_state,$dvi_file);
195: } #to create table of contents
196: my $idxname=$name_file;
197: $idxname=~s/\.tex$/\.idx/;
198: if ($tableofindex eq 'yes') {
199: &busy_wait_command("makeindex $idxname",
200: "making index file",
201: \%prog_state,$idxname);
202: &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
203: "for $status_statement now LaTeXing file for index section",
204: \%prog_state,$dvi_file);
205: } #to create index
206: #Do we have a latex error in the log file?
207: my $logfilename = $texfile; $logfilename =~ s/\.tex$/\.log/;
208: my $temporary_file=IO::File->new($logfilename) || die "Couldn't open log file $logfilename for reading: $!\n";
209: my @content_of_file = <$temporary_file>;
210: close $temporary_file;
211: my $body_log_file = join(' ',@content_of_file);
212: $logfilename =~ s/\.log$/\.html/;
213: $temporary_file = IO::File->new('>'.$logfilename);
214: print $temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_log_file.'</pre></body></html>'."\n";
215: if ($body_log_file=~m/!\s+Emergency stop/) {
216: #LaTeX failed to parse tex file
217: print "<h2>LaTeX could not successfully parse your tex file.</h2>";
218: print "It probably has errors in it.<br />";
219: my $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART';
220: my $whereitends = rindex $body_log_file,'STAMPOFPASSEDRESOURCEEND';
221: if ($whereitbegins!=-1 and $whereitends!=-1) {
222: print "With very high probability this error occured in ".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)."<br /><br />";
223: }
224: print "Here are the error messages in the LaTeX log file</br><br /><pre>";
225: my $sygnal = 0;
226: for (my $i=0;$i<=$#content_of_file;$i++) {
227: if ($content_of_file[$i]=~m/^Runaway argument?/ or $content_of_file[$i]=~m/^!/) {
228: $sygnal = 1;
229: }
230: if ($content_of_file[$i]=~m/Here is how much of/) {
231: $sygnal = 0;
232: }
233: if ($sygnal) {
234: print "$content_of_file[$i]";
235: }
236: }
237: print "</pre>\n";
238: if ($advans_role) {
239: print "<b><big>The link to ";
240: $logfilename=~s/\/home\/httpd//;
241: print "<a href=\"$logfilename\">Your log file </a></big></b>";
242: print "\n";
243: #link tooriginal LaTeX file (included according Michael Hamlin desire)
244: my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open tex file $texfile for reading: $!\n";
245: my @tex_content_of_file = <$tex_temporary_file>;
246: close $tex_temporary_file;
247: my $body_tex_file = join(' ',@tex_content_of_file);
248: $texfile =~ s/\.tex$/aaaaa\.html/;
249: $tex_temporary_file = IO::File->new('>'.$texfile);
250: print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
251: print "<br /><br />";
252: print "<b><big>The link to ";
253: $texfile=~s/\/home\/httpd//;
254: print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
255: print "\n";
256: }
257:
258: } elsif ($body_log_file=~m/<inserted text>/) {
259: my $whereitbegins = index $body_log_file,'<inserted text>';
260: print "You are running LaTeX in <b>batch mode</b>.";
261: while ($whereitbegins != -1) {
262: my $tempobegin=$whereitbegins;
263: $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART',$whereitbegins;
264: my $whereitends = index $body_log_file,'STAMPOFPASSEDRESOURCEEND',$whereitbegins;
265: print "<br />It has found an error in".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)." <br /> and corrected it.\n";
266: 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";
267: $whereitbegins = index $body_log_file,'<inserted text>',$tempobegin+10;
268: }
269: $name_file =~ s/\.tex/\.dvi/;
270: my $new_name_file = $name_file;
271: $new_name_file =~ s/\.dvi/\.ps/;
272: my $papera=$paper;
273: if ($papera eq 'letter') {$papera='';}
274: if ($papera ne '') {$papera='-t'.$papera;}
275: my $comma = "dvips $papera -Ppdf -G0 -o $new_name_file";
276: &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
277: "for $status_statement now Converting to PS",
278: \%prog_state,$new_name_file);
279: if (-e $new_name_file) {
280: print "<h1>PDF output file (see link below)</h1>\n";
281: $new_name_file =~ m/^(.*)\./;
282: my $ps_file = my $tempo_file = $1.'temporar.ps';
283: my $pdf_file = $1.'.pdf';
284: if ($laystyle eq 'album' and $numberofcolumns eq '2') {
285: $comma = "psnup -2 -s1.0 $new_name_file";
286: &busy_wait_command("$comma $tempo_file 1>/dev/null 2>/dev/null",
287: "for $status_statement now Modifying PS layout",
288: \%prog_state,$tempo_file);
289: } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
290: my $papera=$paper;
291: if ($papera eq 'letter') {$papera='';}
292: if ($papera ne '') {$papera='-p'.$papera;}
293: $comma = 'pstops '.$papera.' "2:0+1(0.48w,0)"';
294: &busy_wait_command("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null",
295: "for $status_statement now Modifying PS layout",
296: \%prog_state,$tempo_file);
297:
298: } else {
299: $ps_file=$new_name_file;
300: }
301: &busy_wait_command("ps2pdf $ps_file $pdf_file 1>/dev/null 2>/dev/null",
302: "for $status_statement now Converting PS to PDF",
303: \%prog_state, $pdf_file);
304:
305: my $texlog = $texfile;
306: my $texaux = $texfile;
307: my $texdvi = $texfile;
308: my $texps = $texfile;
309: $texlog =~ s/\.tex/\.log/;
310: $texaux =~ s/\.tex/\.aux/;
311: $texdvi =~ s/\.tex/\.dvi/;
312: $texps =~ s/\.tex/\.ps/;
313: my @garb = ($texaux,$texdvi,$texps);
314: # unlink @garb;
315: unlink $duefile;
316: print "<a href=\"/prtspool/$pdf_file\">Your PDF document</a>";
317: if ($advans_role) {
318: print "<br /><br />";
319: print "<b><big>The link to ";
320: $logfilename=~s/\/home\/httpd//;
321: print "<a href=\"$logfilename\">Your log file </a></big></b>";
322: print "\n";
323: #link tooriginal LaTeX file (included according Michael Hamlin desire)
324: my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open tex file $texfile for reading: $!\n";
325: my @tex_content_of_file = <$tex_temporary_file>;
326: close $tex_temporary_file;
327: my $body_tex_file = join(' ',@tex_content_of_file);
328: $texfile =~ s/\.tex$/aaaaa\.html/;
329: $tex_temporary_file = IO::File->new('>'.$texfile);
330: print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
331: print "<br /><br />";
332: print "<b><big>The link to ";
333: $texfile=~s/\/home\/httpd//;
334: print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
335: print "\n";
336: }
337: }
338: } else {
339: #LaTeX successfully parsed tex file
340: $name_file =~ s/\.tex/\.dvi/;
341: my $new_name_file = $name_file;
342: $new_name_file =~ s/\.dvi/\.ps/;
343: my $papera=$paper;
344: if ($papera eq 'letter') {$papera='';}
345: if ($papera ne '') {$papera='-t'.$papera;}
346: my $comma = "dvips $papera -Ppdf -G0 -o $new_name_file";
347: &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
348: "for $status_statement now Converting to PS",
349: \%prog_state,$new_name_file);
350: if (-e $new_name_file) {
351: print "<br />";
352: $new_name_file =~ m/^(.*)\./;
353: my $ps_file = my $tempo_file = $1.'temporar.ps';
354: my $pdf_file = $1.'.pdf';
355: if ($laystyle eq 'album' and $numberofcolumns eq '2') {
356: $comma = "psnup -2 -s1.0 $new_name_file";
357: &busy_wait_command("$comma $tempo_file 1>/dev/null 2>/dev/null",
358: "for $status_statement now Modifying PS layout",
359: \%prog_state,$tempo_file);
360: } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
361: $papera=~s/t/p/;
362: $comma = 'pstops '.$papera.' "2:0+1(0.48w,0)"';
363: &busy_wait_command("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null",
364: "for $status_statement now Modifying PS layout",
365: \%prog_state,$tempo_file);
366: } else {
367: $ps_file=$new_name_file;
368: }
369: my $addtoPSfile={'legal'=>'<< /PageSize [612 1008] >> setpagedevice',
370: 'tabloid'=>'<< /PageSize [792 1224] >> setpagedevice',
371: 'executive'=>,'<< /PageSize [540 720] >> setpagedevice',
372: 'a2'=>'<< /PageSize [1195.02 1690.09] >> setpagedevice',
373: 'a3'=>'<< /PageSize [842 1195.02] >> setpagedevice',
374: 'a4'=>'<< /PageSize [595.2 842] >> setpagedevice',
375: 'a5'=>'<< /PageSize [421.1 595.2] >> setpagedevice',
376: 'a6'=>'<< /PageSize [298.75 421.1] >> setpagedevice',
377: };
378: if ($paper ne 'letter') {
379: open(FFH,'<',$ps_file) || die "Couldn't open ps file $ps_file for reading: $!\n";
380: my $new_ps_file='new'.$ps_file;
381: open(FFHS,'>',$new_ps_file) || die "Couldn't open new ps file $new_ps_file for reading: $!\n";
382: print FFHS $addtoPSfile->{$paper}."\n";
383: while (<FFH>) {
384: print FFHS $_;
385: }
386: close(FFH);
387: close(FFHS);
388: $ps_file=$new_ps_file;
389: }
390: &busy_wait_command("ps2pdf $ps_file $pdf_file 1>/dev/null 2>/dev/null",
391: "for $status_statement now Converting PS to PDF",
392: \%prog_state,$pdf_file);
393:
394: my $texlog = $texfile;
395: my $texaux = $texfile;
396: my $texdvi = $texfile;
397: my $texps = $texfile;
398: $texlog =~ s/\.tex/\.log/;
399: $texaux =~ s/\.tex/\.aux/;
400: $texdvi =~ s/\.tex/\.dvi/;
401: $texps =~ s/\.tex/\.ps/;
402: my @garb = ($texlog,$texaux,$texdvi,$texps);
403: # unlink @garb;
404: unlink $duefile;
405: print "<a href=\"/prtspool/$pdf_file\">$link_text - click here to download pdf</a>";
406: print "\n";
407: }
408: }
409: } else {
410: print "LaTeX file $texfile was not created successfully";
411: }
412: }
413: print "<br />";
414: if ($number_of_files>1) {
415: my $zipfile=$texfile[0];
416: $zipfile=~s/\.tex/\.zip/;
417: my $statement="zip $zipfile";
418: foreach my $file (@texfile) {
419: $file=~s/\.tex/.\pdf/;
420: $statement.=' '.$file;
421: }
422: print("<pre>Zip Output:\n");
423: system($statement);
424: print("</pre>");
425: $zipfile=~s/\/home\/httpd//;
426: print "<br /> A <a href=\"$zipfile\">ZIP file</a> of all the PDFs.";
427: }
428: if ($adv) { &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); }
429:
430: my $done;
431: sub REAPER {
432: $done=1;
433: }
434:
435: sub busy_wait_command {
436: my ($command,$message,$progress_win,$output_file)=@_;
437:
438: $SIG{CHLD} = \&REAPER;
439: $done=0;
440: my $pid=open(CMD,"$command |");
441: if ($adv) {
442: &Apache::lonhtmlcommon::Update_PrgWin('',$progress_win,$message);
443: }
444: while(!$done) {
445: sleep 1;
446: my $extra_msg;
447: if ($output_file) {
448: my $size=(stat($output_file))[7];
449: $extra_msg=", $size bytes generated";
450: }
451: if ($adv) {
452: &Apache::lonhtmlcommon::Update_PrgWin('',$progress_win,
453: $message.$extra_msg);
454: }
455: }
456: $SIG{CHLD}='IGNORE';
457: close(CMD);
458: }
459:
460:
461:
462:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>