MDL-64431 assignfeedback_editpdf: Flatten PDF files

This commit is contained in:
Dan Marsden 2023-11-15 19:26:08 +13:00
parent 8ad9114050
commit 9dd88a4988

View File

@ -710,7 +710,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)
@ -718,28 +718,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.