]> _ Git - cubist_pdf.git/commitdiff
wip #7340 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 18 Feb 2025 18:35:39 +0000 (19:35 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 18 Feb 2025 18:35:39 +0000 (19:35 +0100)
src/PDFTools.php

index 0f92e5742b492f8f123c71538ca99ca674019b9a..410296abfe7d8832e21267218ae2ca87653ac83d 100644 (file)
@@ -264,7 +264,6 @@ class PDFTools
 
     public static function filter($in, $out, $keepImages = true, $keepVectors = true, $keepText = true)
     {
-        // Fabrication des thumbnails avec ghostscript
         $gs = new CommandLine('gs', null, true);
         $gs->setArg('-dBATCH');
         $gs->setArg('-dNOPAUSE');
@@ -286,6 +285,18 @@ class PDFTools
         $gs->execute();
     }
 
+    public static function getFilteredPDF($in, $keepImages = true, $keepVectors = true, $keepText = true)
+    {
+        if ($keepVectors && $keepText && $keepImages) {
+            return $in;
+        }
+        $name = str_replace('.pdf', '.' . ($keepImages ? '1' : '0') . ($keepVectors ? '1' : '0') . ($keepText ? '1' : '0') . '.pdf', $in);
+        if (!file_exists($name) || filemtime($name) < filemtime($in) || filemtime($name) > filemtime(__FILE__)) {
+            self::filter($in, $name, $keepImages, $keepVectors, $keepText);
+        }
+        return $name;
+    }
+
     public static function makeTextSVGFile($in, $out)
     {
         set_time_limit(0);
@@ -406,51 +417,77 @@ class PDFTools
         $gs->execute();
     }
 
-    public static function makeShotPNM($in, $out, $page, $prefix = '', $resolution = 72, $quality = 90, $antialiasing = 4, $texts = true, $width = null, $height = null, $format = 'jpg')
+    public static function makeShotPNM($in, $out, $page, $prefix = '', $resolution = 72, $quality = 90, $antialiasing = 4, $texts = true, $transparent = false, $width = null, $height = null, $format = 'jpg')
     {
         $tmp = Files::tempnam();
 
-        $antialiasing = $antialiasing ? 'yes' : 'no';
-        $freetype = $texts ? 'yes' : 'no';
-        // Exporte les fichiers
-        $pdftoppm = new CommandLine('pdftoppm', null, true, 'pdftoppm_' . ($texts ? 't' : 'h') . '_' . (round($resolution)) . '_');
-        $pdftoppm->setArg('f', $page);
-        $pdftoppm->setArg('l', $page);
-        $pdftoppm->setArg('-cropbox');
-        $pdftoppm->setArg('-freetype ' . $freetype);
-        $pdftoppm->setArg('-singlefile');
-        $pdftoppm->setArg('-aa ' . $antialiasing);
-        $pdftoppm->setArg('-aaVector ' . $antialiasing);
-        if (null !== $resolution) {
-            $pdftoppm->setArg('r', $resolution);
-        }
-        if (null !== $width) {
-            $pdftoppm->setArg('-scale-to-x ' . $width);
-        }
-        if (null !== $height) {
-            $pdftoppm->setArg('-scale-to-y ' . $height);
+        if ($transparent) {
+            $format = 'png';
+
+            $input = static::getFilteredPDF($in, true, true, $texts);
+            $exe = new CommandLine('pdftocairo');
+            $exe->setArg('f', $page);
+            $exe->setArg('l', $page);
+            $exe->setArg('-cropbox');
+            if (null !== $resolution) {
+                $exe->setArg('r', $resolution);
+            }
+            if (null !== $width) {
+                $exe->setArg('-scale-to-x ' . $width);
+            }
+            if (null !== $height) {
+                $exe->setArg('-scale-to-y ' . $height);
+            }
+            $exe->setArg(null, '-transparent');
+            $exe->setArg(null, '-' . $format);
+            $exe->setArg(null, $input);
+            $exe->setArg(null, $tmp);
+            $exe->execute();
+
+        } else {
+            $antialiasing = $antialiasing ? 'yes' : 'no';
+            $freetype = $texts ? 'yes' : 'no';
+            // Exporte les fichiers
+            $exe = new CommandLine('pdftoppm', null, true, 'pdftoppm_' . ($texts ? 't' : 'h') . '_' . (round($resolution)) . '_');
+            $exe->setArg('f', $page);
+            $exe->setArg('l', $page);
+            $exe->setArg('-cropbox');
+            $exe->setArg('-freetype ' . $freetype);
+            $exe->setArg('-singlefile');
+            $exe->setArg('-aa ' . $antialiasing);
+            $exe->setArg('-aaVector ' . $antialiasing);
+            if (null !== $resolution) {
+                $exe->setArg('r', $resolution);
+            }
+            if (null !== $width) {
+                $exe->setArg('-scale-to-x ' . $width);
+            }
+            if (null !== $height) {
+                $exe->setArg('-scale-to-y ' . $height);
+            }
+            if ($format === 'jpg') {
+                $exe->setArg('-jpeg');
+                $exe->setArg('-jpegopt "quality=' . $quality . '"');
+            } else if ($format === 'png') {
+                $exe->setArg('-png');
+            }
+            $exe->setArg(null, $in);
+            $exe->setArg(null, $tmp);
+            $exe->execute();
         }
-        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 .= '.' . $format;
 
-        if (self::hasPopplerError($pdftoppm->getOutput())) {
+        if (self::hasPopplerError($exe->getOutput())) {
             unlink($tmp);
-            $pdftoppm->error_log();
+            $exe->error_log();
             return;
         }
 
         if (file_exists($tmp)) {
             rename($tmp, $out);
         } else {
-            $pdftoppm->error_log();
+            $exe->error_log();
         }
     }