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