From 26e91219e87435259b0cf62cc5987acf209f10e6 Mon Sep 17 00:00:00 2001 From: Dan Marsden Date: Wed, 15 Nov 2023 19:26:08 +1300 Subject: [PATCH] MDL-64431 assignfeedback_editpdf: Flatten PDF files --- mod/assign/feedback/editpdf/classes/pdf.php | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/mod/assign/feedback/editpdf/classes/pdf.php b/mod/assign/feedback/editpdf/classes/pdf.php index 1eb6453fb81..5a5dc899419 100644 --- a/mod/assign/feedback/editpdf/classes/pdf.php +++ b/mod/assign/feedback/editpdf/classes/pdf.php @@ -720,7 +720,7 @@ class pdf extends TcpdfFpdi { } /** - * 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) @@ -728,28 +728,15 @@ class pdf extends TcpdfFpdi { 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.