MDL-55762 assignpdf: Handle errored pages better

This commit is contained in:
Ankit Agarwal 2017-02-13 13:31:39 +05:30
parent b6d06a5f78
commit 1f3556b010
4 changed files with 46 additions and 1 deletions

View File

@ -448,7 +448,12 @@ EOD;
$files = array();
for ($i = 0; $i < $pagecount; $i++) {
$image = $pdf->get_image($i);
try {
$image = $pdf->get_image($i);
} catch (\moodle_exception $e) {
// We catch only moodle_exception here as other exceptions indicate issue with setup not the pdf.
$image = pdf::get_error_image($tmpdir, $i);
}
$record->filename = basename($image);
$files[$i] = $fs->create_file_from_pathname($record, $tmpdir . '/' . $image);
@unlink($tmpdir . '/' . $image);

View File

@ -68,6 +68,8 @@ class pdf extends \FPDI {
const MIN_ANNOTATION_WIDTH = 5;
/** Min. height an annotation should have */
const MIN_ANNOTATION_HEIGHT = 5;
/** Blank PDF file used during error. */
const BLANK_PDF = '/mod/assign/feedback/editpdf/fixtures/blank.pdf';
/**
* Combine the given PDF files into a single PDF. Optionally add a coversheet and coversheet fields.
@ -522,6 +524,43 @@ class pdf extends \FPDI {
return $tempdst;
}
/**
* Generate an localised error image for the given pagenumber.
*
* @param string $errorimagefolder path of the folder where error image needs to be created.
* @param int $pageno page number for which error image needs to be created.
*
* @return string File name
* @throws \coding_exception
*/
public static function get_error_image($errorimagefolder, $pageno) {
global $CFG;
$errorfile = $CFG->dirroot . self::BLANK_PDF;
if (!file_exists($errorfile)) {
throw new \coding_exception("Blank PDF not found", "File path" . $errorfile);
}
$tmperrorimagefolder = make_request_directory();
$pdf = new pdf();
$pdf->set_pdf($errorfile);
$pdf->copy_page();
$pdf->add_comment(get_string('errorpdfpage', 'assignfeedback_editpdf'), 250, 300, 200, "red");
$generatedpdf = $tmperrorimagefolder . '/' . 'error.pdf';
$pdf->save_pdf($generatedpdf);
$pdf = new pdf();
$pdf->set_pdf($generatedpdf);
$pdf->set_image_folder($tmperrorimagefolder);
$image = $pdf->get_image(0);
$pdf->Close(); // PDF loaded and never saved/outputted needs to be closed.
$newimg = 'image_page' . $pageno . '.png';
copy($tmperrorimagefolder . '/' . $image, $errorimagefolder . '/' . $newimg);
return $newimg;
}
/**
* Test that the configured path to ghostscript is correct and working.
* @param bool $generateimage - If true - a test image will be generated to verify the install.

Binary file not shown.

View File

@ -44,6 +44,7 @@ $string['downloadablefilename'] = 'feedback.pdf';
$string['downloadfeedback'] = 'Download feedback PDF';
$string['drag'] = 'Drag';
$string['errorgenerateimage'] = 'Error generating image with ghostscript, debugging info: {$a}';
$string['errorpdfpage'] = 'There was an error while generating this page.';
$string['editpdf'] = 'Annotate PDF';
$string['editpdf_help'] = 'Annotate students submissions directly in the browser and produce an edited downloadable PDF.';
$string['enabled'] = 'Annotate PDF';