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