]> _ Git - cubist_util.git/commitdiff
wait #6076 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Sat, 24 Jun 2023 10:47:28 +0000 (12:47 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Sat, 24 Jun 2023 10:47:28 +0000 (12:47 +0200)
src/CommandLine/Poppler.php

index 421fc61407643dc04512ca48d3fccea7677914d9..9b88f3391636685e47fb3f6dd69eb0ebf7b9fd91 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace Cubist\Util\CommandLine;
 
+use Cubist\PDF\PDFTools;
 use Cubist\Util\CommandLine;
 use Cubist\Util\Files\Files;
 use Cubist\Util\Math;
@@ -21,20 +22,18 @@ class Poppler
             'quality' => '95',
             'antialias' => true,
             'format' => 'jpeg',
-            'internalformat' => 'png');
+            'internalformat' => 'png',
+            'texts' => true,
+            'background' => true,
+        );
+
 
         $options = array_merge($defaultOptions, $options);
         $options['factor'] = $options['resolution'] / self::RESOLUTION_FACTOR;
-
-        if(isset($options['texts']) && $options['texts']){
-            unset($options['texts']);
-        }
-
-        if ($options['format'] === 'jpeg') {
-            $extension = 'jpg';
-        } else {
-            $extension = $options['format'];
+        if ($options['format'] === 'svg') {
+            $options['internalformat'] = 'svg';
         }
+        $extension = self::_format2ext($options['format']);
 
         if (null !== $to) {
             $res = $to . '.' . $extension;
@@ -69,29 +68,33 @@ class Poppler
             Files::getLock($lockFile);
         }
 
-        $pdftoppm = new CommandLine('pdftoppm');
-        $pdftoppm->setArg('r', $options['resolution']);
-        if ($extension === 'png') {
-            $pdftoppm->setArg('-png');
-        } elseif ($extension === 'jpg') {
-            $pdftoppm->setArg('-jpeg');
-            $pdftoppm->setArg('-jpegopt', '"quality=' . $options['quality'] . '"');
+        if ($options['format'] === 'svg') {
+            $pdftocairo = new CommandLine('pdftocairo');
+            $pdftocairo->setArg('-svg');
+            $pdftocairo->setArg('-paperw',$rect['width']);
+            $pdftocairo->setArg('-paperh',$rect['height']);
+            self::_crop($pdftocairo, $page, $rect, 1, 1);
+            self::_exec($pdftocairo, $file, $dest . '.' . $extension);
+            if (!$options['background']) {
+                PDFTools::makeTextSVGFile($dest . '.' . $extension, $dest . '.' . $extension);
+            }
+        } else {
+            $pdftoppm = new CommandLine('pdftoppm');
+            if ($extension === 'png') {
+                $pdftoppm->setArg('-png');
+            } elseif ($extension === 'jpg') {
+                $pdftoppm->setArg('-jpeg');
+                $pdftoppm->setArg('-jpegopt', '"quality=' . $options['quality'] . '"');
+            }
+            $aa = $options['antialias'] ? 'yes' : 'no';
+            $pdftoppm->setArg('-aa', $aa);
+            $pdftoppm->setArg('-aaVector', $aa);
+            $pdftoppm->setArg('-cropbox');
+            $pdftoppm->setArg('-freetype', (!isset($options['texts']) || $options['texts']) ? 'yes' : 'no');
+            $pdftoppm->setArg('-singlefile');
+            self::_crop($pdftoppm, $page, $rect, $options['resolution'], $options['factor']);
+            self::_exec($pdftoppm, $file, $dest);
         }
-        $aa = $options['antialias'] ? 'yes' : 'no';
-        $pdftoppm->setArg('-aa', $aa);
-        $pdftoppm->setArg('-aaVector', $aa);
-        $pdftoppm->setArg('-cropbox');
-        $pdftoppm->setArg('-freetype', (!isset($options['texts']) || $options['texts']) ? 'yes' : 'no');
-        $pdftoppm->setArg('-singlefile');
-        $pdftoppm->setArg('-f', $page);
-        $pdftoppm->setArg('-l', $page);
-        $pdftoppm->setArg('-x', round($rect['x'] * $options['factor']));
-        $pdftoppm->setArg('-y', round($rect['y'] * $options['factor']));
-        $pdftoppm->setArg('-W', round($rect['width'] * $options['factor']));
-        $pdftoppm->setArg('-H', round($rect['height'] * $options['factor']));
-        $pdftoppm->setArg(null, $file);
-        $pdftoppm->setArg(null, $dest);
-        $pdftoppm->execute();
 
         if (isset($lockFile)) {
             Files::releaseLock($lockFile);
@@ -106,6 +109,42 @@ class Poppler
         return $res;
     }
 
+    /**
+     * @param $cli CommandLine
+     * @param $page int
+     * @param $rect array
+     * @param $resolution float
+     * @param $factor float
+     * @return void
+     */
+    protected static function _crop($cli, $page, $rect, $resolution, $factor = 1)
+    {
+        $cli->setArg('-f', $page);
+        $cli->setArg('-l', $page);
+        $cli->setArg('-r', $resolution);
+        $cli->setArg('-x', round($rect['x'] * $factor));
+        $cli->setArg('-y', round($rect['y'] * $factor));
+        $cli->setArg('-W', round($rect['width'] * $factor));
+        $cli->setArg('-H', round($rect['height'] * $factor));
+    }
+
+
+    /**
+     * @param $cli CommandLine
+     * @param $file string
+     * @param $dest string
+     * @return void
+     */
+    protected static function _exec($cli, $file, $dest, $debug = false)
+    {
+        $cli->setArg(null, $file);
+        $cli->setArg(null, $dest);
+        $cli->execute();
+        if ($debug) {
+            $cli->debug();
+        }
+    }
+
     protected static function _pdftojpeg($file, $to, $nbpages, $options = array())
     {
         /*