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