Skip to content

Commit

Permalink
MDL-64431 assign: Ensure grading view displays PDF annotations.
Browse files Browse the repository at this point in the history
When displaying submitted files in assignment grading view, run
all PDFs through Ghostscript to merge annotation layers with
content, so that the annotations display correctly.
  • Loading branch information
royko committed Jun 17, 2019
1 parent 43c501a commit 209e20b
Showing 1 changed file with 3 additions and 17 deletions.
20 changes: 3 additions & 17 deletions mod/assign/feedback/editpdf/classes/pdf.php
Expand Up @@ -604,36 +604,22 @@ public static function ensure_pdf_compatible(\stored_file $file) {
}

/**
* Check to see if PDF is version 1.4 (or below); if not: use ghostscript to convert it
* Use ghostscript to convert PDFs of version 1.4 or less,
* and PDFs containing annotation layers into single layer PDFs.
*
* @param string $tempsrc The path to the file on disk.
* @return string path to copy or converted pdf (false == fail)
*/
public static function ensure_pdf_file_compatible($tempsrc) {
global $CFG;

$pdf = new pdf();
$pagecount = 0;
try {
$pagecount = $pdf->load_pdf($tempsrc);
} catch (\Exception $e) {
// PDF was not valid - try running it through ghostscript to clean it up.
$pagecount = 0;
}
$pdf->Close(); // PDF loaded and never saved/outputted needs to be closed.

if ($pagecount > 0) {
// PDF is already valid and can be read by tcpdf.
return $tempsrc;
}

$temparea = make_request_directory();
$tempdst = $temparea . "/target.pdf";

$gsexec = \escapeshellarg($CFG->pathtogs);
$tempdstarg = \escapeshellarg($tempdst);
$tempsrcarg = \escapeshellarg($tempsrc);
$command = "$gsexec -q -sDEVICE=pdfwrite -dBATCH -dNOPAUSE -sOutputFile=$tempdstarg $tempsrcarg";
$command = "$gsexec -q -sDEVICE=pdfimage24 -dBATCH -dNOPAUSE -sOutputFile=$tempdstarg $tempsrcarg";
exec($command);
if (!file_exists($tempdst)) {
// Something has gone wrong in the conversion.
Expand Down

0 comments on commit 209e20b

Please sign in to comment.