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