[system] / trunk / webwork-modperl / lib / WeBWorK / ContentGenerator / Hardcopy.pm Repository:
ViewVC logotype

Diff of /trunk/webwork-modperl/lib/WeBWorK/ContentGenerator/Hardcopy.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1155 Revision 1156
62 } 62 }
63 63
64 unless ($self->{generationError}) { 64 unless ($self->{generationError}) {
65 if ($r->param("generateHardcopy")) { 65 if ($r->param("generateHardcopy")) {
66 my ($tempDir, $fileName,$errors) = eval { $self->generateHardcopy() }; 66 my ($tempDir, $fileName,$errors) = eval { $self->generateHardcopy() };
67 warn "tempDir $tempDir fileName $fileName ";
67 if ($@) { 68 if ($@) {
68 $self->{generationError} = $@; 69 $self->{generationError} = $@;
69 # In this case no pdf file was generated, so there is not much more that can be done 70 # In this case no correct pdf file was generated.
71 # there is not much more that can be done
70 # throw the error up higher. 72 # throw the error up higher.
71 # FIXME: this leaves the temp dir around. 73 # The error is reported in body.
72 # perhaps: rmtree($tempdir) here? 74 # the tempDir was removed in generateHardcopy
73 75
74 76
75 } else { 77 } else {
76 my $filePath = "$tempDir/$fileName"; 78 my $filePath = "$tempDir/$fileName";
77 # FIXME this is taking up server time 79 # FIXME this is taking up server time
78 # why not move the file to the tempDir and let the browser pick it up on redirect? 80 # why not move the file to the tempDir and let the browser pick it up on redirect?
81 # my $hardcopyFilePath = $self->{hardcopyFilePath};
82 # my $hardcopyFileURL = $self->{hardcopyFileURL};
79 if ($errors eq '') { 83 if ($errors eq '') {
80 $r->content_type("application/x-pdf"); 84 $r->content_type("application/x-pdf");
81 # as per RFC2183: 85 # as per RFC2183:
82 $r->header_out("Content-Disposition", "attachment; filename=$fileName"); 86 $r->header_out("Content-Disposition", "attachment; filename=$fileName");
83 $r->send_http_header(); 87 $r->send_http_header();
136 return ""; 140 return "";
137 } elsif ($disposition eq "FAIL") { 141 } elsif ($disposition eq "FAIL") {
138 print $self->errorOutput(@rest); 142 print $self->errorOutput(@rest);
139 return ""; 143 return "";
140 } elsif ($disposition eq "RETRY") { 144 } elsif ($disposition eq "RETRY") {
141 print $self->errorOutput(@rest);
142 } elsif ($disposition eq "ERROR_REPORT") {
143 print "There were errors while producing the pdf file. You can download the ".
144 CGI::a({-href =>$self->{finalFile}} , "output that was produced.");
145 print $self->errorOutput(@rest); 145 print $self->errorOutput(@rest);
146 } else { # a "simple" error 146 } else { # a "simple" error
147 print CGI::p(CGI::font({-color=>"red"}, @rest)); 147 print CGI::p(CGI::font({-color=>"red"}, @rest));
148 } 148 }
149 } else { 149 } else {
365 my $errors = ''; 365 my $errors = '';
366 eval { $self->latex2pdf($tex, $tempDir, $fileName) }; 366 eval { $self->latex2pdf($tex, $tempDir, $fileName) };
367 if ($@) { 367 if ($@) {
368 $errors = $@; 368 $errors = $@;
369 #$errors =~ s/\n/<br>/g; # make this readable on HTML FIXME make this a Utils. filter (Error2HTML) 369 #$errors =~ s/\n/<br>/g; # make this readable on HTML FIXME make this a Utils. filter (Error2HTML)
370 # clean up temp directory
371 rmtree($tempDir);
370 die ["FAIL", "Failed to generate PDF from tex", $errors]; #throw error to subroutine body 372 die ["FAIL", "Failed to generate PDF from tex", $errors]; #throw error to subroutine body
371 } 373 }
372 374
373 return $tempDir, $fileName, $errors; 375 return $tempDir, $fileName;
374} 376}
375 377
376# ----- 378# -----
377 379
378sub latex2pdf { 380sub latex2pdf {
383 my $ce = $self->{ce}; 385 my $ce = $self->{ce};
384 386
385 # Location for hardcopy file to be downloaded 387 # Location for hardcopy file to be downloaded
386 # FIXME this should use surePathToTmpFile 388 # FIXME this should use surePathToTmpFile
387 my $hardcopyTempDirectory = $ce->{courseDirs}->{html_temp}."/hardcopy"; 389 my $hardcopyTempDirectory = $ce->{courseDirs}->{html_temp}."/hardcopy";
388 warn " tmpDirectory $hardcopyTempDirectory";
389 mkdir ($hardcopyTempDirectory) or die "Unable to make $hardcopyTempDirectory" unless -e $hardcopyTempDirectory; 390 mkdir ($hardcopyTempDirectory) or die "Unable to make $hardcopyTempDirectory" unless -e $hardcopyTempDirectory;
390 my $hardcopyFilePath = "$hardcopyTempDirectory/$fileName"; 391 my $hardcopyFilePath = "$hardcopyTempDirectory/$fileName";
391 my $hardcopyFileURL = $ce->{courseURLs}->{html_temp}."/hardcopy/$fileName"; 392 my $hardcopyFileURL = $ce->{courseURLs}->{html_temp}."/hardcopy/$fileName";
392 393 $self->{hardcopyFilePath} = $hardcopyFilePath;
394 $self->{hardcopyFileURL} = $hardcopyFileURL;
393 ## create a temporary directory for tex to shit in 395 ## create a temporary directory for tex to shit in
394 #my $wd = tempdir("webwork-hardcopy-XXXXXXXX", TMPDIR => 1); 396 #my $wd = tempdir("webwork-hardcopy-XXXXXXXX", TMPDIR => 1);
395 # - we're using the existing temp dir. now 397 # - we're using the existing temp dir. now
396 398
397 my $wd = $tempDir; 399 my $wd = $tempDir;
411 my $pdflatexResult = system "cd $wd && $pdflatex $texFile"; 413 my $pdflatexResult = system "cd $wd && $pdflatex $texFile";
412 414
413 # Even with errors there may be a valid pdfFile. Move it to where we can get it. 415 # Even with errors there may be a valid pdfFile. Move it to where we can get it.
414 if (-e $pdfFile) { 416 if (-e $pdfFile) {
415 # move resulting PDF file to appropriate location 417 # move resulting PDF file to appropriate location
418 # FIXME don't fix everything at once :-)
416# system "/bin/mv", $pdfFile, $finalFile 419 system "/bin/mv", $pdfFile, $finalFile
417# and die "Failed to mv: $pdfFile to $finalFile<br>\n Quite likely this means that there ". 420 and die "Failed to mv: $pdfFile to $finalFile<br>\n Quite likely this means that there ".
418# "is not sufficient write permission for some directory.<br>$!<br>\n"; 421 "is not sufficient write permission for some directory.<br>$!<br>\n";
419 # moving to course tmp/hardcopy directory 422 # moving to course tmp/hardcopy directory
420 system "/bin/mv", $pdfFile, $hardcopyFilePath 423# system "/bin/mv", $pdfFile, $hardcopyFilePath
421 and die "Failed to mv: $pdfFile to $hardcopyFilePath<br> Quite likely this means that there ". 424# and die "Failed to mv: $pdfFile to $hardcopyFilePath<br> Quite likely this means that there ".
422 "is not sufficient write permission for some directory.<br>$!\n".CGI::br(); 425# "is not sufficient write permission for some directory.<br>$!\n".CGI::br();
423 } 426 }
424 # Alert the world that the tex file did not process perfectly. 427 # Alert the world that the tex file did not process perfectly.
425 if ($pdflatexResult) { 428 if ($pdflatexResult) {
426 # something bad happened 429 # something bad happened
427 my $textErrorMessage = "Call to $pdflatex failed: $!\n".CGI::br(); 430 my $textErrorMessage = "Call to $pdflatex failed: $!\n".CGI::br();
431
432 # move what output there is to someplace where it can be read
433 system "/bin/mv", $finalFile, $hardcopyFilePath
434 and die "Failed to mv: $pdfFile to $hardcopyFilePath<br> Quite likely this means that there ".
435 "is not sufficient write permission for some directory.<br>$!\n".CGI::br();
428 if (-e $hardcopyFilePath ) { 436 if (-e $hardcopyFilePath ) {
437 # FIXME Misuse of html tags!!!
429 $textErrorMessage.= "Some pdf output was produced and is available ". CGI::a({-href=>$hardcopyFileURL},"here.").CGI::hr(); 438 $textErrorMessage.= "<h4>Some pdf output was produced and is available ". CGI::a({-href=>$hardcopyFileURL},"here.</h4>").CGI::hr();
430 } 439 }
440 # report logfile
431 if (-e $logFile) { 441 if (-e $logFile) {
432 $textErrorMessage .= "pdflatex ran, but did not succeed. This suggests an error in the TeX\n".CGI::br(); 442 $textErrorMessage .= "pdflatex ran, but did not succeed. This suggests an error in the TeX\n".CGI::br();
433 $textErrorMessage .= "version of one of the problems, or a problem with the pdflatex system.\n".CGI::br(); 443 $textErrorMessage .= "version of one of the problems, or a problem with the pdflatex system.\n".CGI::br();
434 my $logFileContents = eval { readTexErrorLog($logFile) }; 444 my $logFileContents = eval { readTexErrorLog($logFile) };
435 if ($@) { 445 if ($@) {

Legend:
Removed from v.1155  
changed lines
  Added in v.1156

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9