From: Vincent Vanwaelscappel Date: Thu, 18 Jan 2024 12:10:11 +0000 (+0100) Subject: wait #6149 @0.25 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=1b061d94700f97b82f4719cb2edc9ef43e33ab0c;p=cubist_pdf.git wait #6149 @0.25 --- diff --git a/.idea/cubist_pdf.iml b/.idea/cubist_pdf.iml index 5fb2ac8..a252244 100644 --- a/.idea/cubist_pdf.iml +++ b/.idea/cubist_pdf.iml @@ -74,6 +74,7 @@ + diff --git a/.idea/deployment.xml b/.idea/deployment.xml index 6b97873..43d3d7b 100644 --- a/.idea/deployment.xml +++ b/.idea/deployment.xml @@ -1,8 +1,22 @@ - + diff --git a/.idea/php.xml b/.idea/php.xml index 64d41f3..bae7fde 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -71,6 +71,7 @@ + diff --git a/src/PDFTools.php b/src/PDFTools.php index fe384a2..280ae5d 100644 --- a/src/PDFTools.php +++ b/src/PDFTools.php @@ -12,7 +12,6 @@ use DOMElement; use DOMNode; use DOMXPath; use Cubist\PDF\CommandLine\FWSTK; -use Illuminate\Support\Facades\Log; class PDFTools { @@ -231,7 +230,7 @@ class PDFTools } - public static function makeBaseSVGFile($in, $out, $page) + public static function makeBaseSVGFile($in, $out, $page, $attempts = 3) { $pdftocairo = new CommandLine('pdftocairo'); $pdftocairo->setArg('f', $page); @@ -242,6 +241,14 @@ class PDFTools $pdftocairo->setArg(null, $in); $pdftocairo->setArg(null, $out); $pdftocairo->execute(); + + if (self::hasPopplerError($pdftocairo->getOutput())) { + if ($attempts > 0) { + static::makeBaseSVGFile($in, $out, $page, $attempts - 1); + } else { + unlink($out); + } + } } public static function makeTextSVGFile($in, $out) @@ -371,7 +378,7 @@ class PDFTools $antialiasing = $antialiasing ? 'yes' : 'no'; $freetype = $texts ? 'yes' : 'no'; // Exporte les fichiers - $pdftoppm = new CommandLine('pdftoppm', null, true); + $pdftoppm = new CommandLine('pdftoppm', null, true, 'pdftoppm_' . ($texts ? 't' : 'h') . '_' . (round($resolution)) . '_'); $pdftoppm->setArg('f', $page); $pdftoppm->setArg('l', $page); $pdftoppm->setArg('-cropbox'); @@ -388,14 +395,34 @@ class PDFTools if (null !== $height) { $pdftoppm->setArg('-scale-to-y ' . $height); } + if ($format === 'jpg') { + $pdftoppm->setArg('-jpeg'); + $pdftoppm->setArg('-jpegopt "quality=' . $quality . '"'); + } else if ($format === 'png') { + $pdftoppm->setArg('-png'); + } $pdftoppm->setArg(null, $in); $pdftoppm->setArg(null, $tmp); $pdftoppm->execute(); - $tmp .= '.ppm'; + $tmp .= '.' . $format; + + if (self::hasPopplerError($pdftoppm->getOutput())) { + unlink($tmp); + $pdftoppm->error_log(); + return; + } + + if (file_exists($tmp)) { + rename($tmp, $out); + } else { + $pdftoppm->error_log(); + } + } - $output = $pdftoppm->getOutput(); + protected static function hasPopplerError($output) + { $errors = [ - //'end of file inside dictionary', + 'end of file inside', 'leftover args in content stream', 'document stream is empty', //'premature end of data segment', @@ -403,36 +430,16 @@ class PDFTools //'couldn\'t find trailer dictionary', //'invalid xref entry', //'incorrect stream length', - //'try to reconstruct', + 'try to reconstruct', //'error opening pdf file', ]; foreach ($errors as $error) { if (stristr($output, $error)) { - unlink($tmp); - $pdftoppm->debug(); - return; + return true; } } - - - if (file_exists($tmp)) { - if ($format === 'jpg') { - $cjpeg = new CommandLine('cjpeg', null, true); - $cjpeg->setArg('-quality ' . ($quality + 6)); - $cjpeg->setArg('-outfile ' . $out); - $cjpeg->setArg(null, $tmp); - $cjpeg->execute(); - } else if ($format === 'png') { - $pnmtopng = new CommandLine('pnmtopng', $out, false); - $pnmtopng->setArg('-background white'); - $pnmtopng->setArg(null, $tmp); - $pnmtopng->execute(); - } - unlink($tmp); - } else { - $pdftoppm->debug(); - } + return false; } public static function getThumbFromPDF($pdf, $page, $format = 'jpg')