Skip to content

Commit

Permalink
Merge branch 'm42-mdl64431' of https://github.com/danmarsden/moodle i…
Browse files Browse the repository at this point in the history
…nto MOODLE_402_STABLE
  • Loading branch information
junpataleta committed Dec 5, 2023
2 parents 10bf4f5 + 63c92d1 commit 3f013a6
Showing 1 changed file with 4 additions and 17 deletions.
21 changes: 4 additions & 17 deletions mod/assign/feedback/editpdf/classes/pdf.php
Expand Up @@ -720,36 +720,23 @@ 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
* Flatten and convert file using ghostscript then load pdf.
*
* @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 -dSAFER -dBATCH -dNOPAUSE -sOutputFile=$tempdstarg $tempsrcarg";
$command = "$gsexec -q -sDEVICE=pdfwrite -dPreserveAnnots=false -dSAFER -dBATCH -dNOPAUSE "
. "-sOutputFile=$tempdstarg $tempsrcarg";

exec($command);
if (!file_exists($tempdst)) {
// Something has gone wrong in the conversion.
Expand Down

0 comments on commit 3f013a6

Please sign in to comment.