]> _ Git - cubist_pdf.git/commitdiff
wip #4209 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 13 Jul 2022 16:13:36 +0000 (18:13 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 13 Jul 2022 16:13:36 +0000 (18:13 +0200)
src/PDFTools.php

index dc4602c7b384f40951ef1cbeb7d8ae7c91f7e178..80b20eb05b5c7bafa716a85466d2c14bbb7e9412 100644 (file)
@@ -4,6 +4,8 @@ namespace Cubist\PDF;
 
 use Cubist\Util\CommandLine;
 use Cubist\Util\Files\Files;
+use Cubist\Util\Str;
+use Cubist\Util\Text;
 use DOMDocument;
 use DOMNode;
 use DOMXPath;
@@ -41,6 +43,77 @@ class PDFTools
         }
     }
 
+    protected static function parseInfos($data)
+    {
+        $res = [];
+
+        // This function get general infos (pages sizes, boxes, number sections and
+        // bookmarks
+        // Init arrays
+        $res['infos'] = [];
+        $res['infos']['size'] = [0, 0];
+        $res['bookmarks'] = [];
+        $res['numberSections'] = '';
+        $bookmark_id = 0;
+
+        $res['size'] = array(0, 0);
+        $lines = explode("\n", $data);
+        foreach ($lines as $line) {
+            $line = trim(Text::condenseWhite($line));
+            $e = explode(':', $line, 2);
+            $k = trim($e[0]);
+            if (count($e) < 2) {
+                continue;
+            }
+            $v = trim($e[1]);
+            if ($k == 'Pages' || $k == 'NumberOfPages') {
+                $res['pages'] = $res['infos']['pages'] = $v;
+                $res['infos']['page'] = [];
+                for ($i = 1; $i <= $res['pages']; $i++) {
+                    $res['infos']['page'][$i] = [];
+                }
+            } elseif (preg_match('|Page\s+([0-9]+)\s+(.*)Box:\s+([0-9.]*)\s+([0-9.]*)\s+([0-9.]*)\s+([0-9.]*)|iu', $line, $m)) {
+                $res['infos']['page'][$m[1]][strtolower($m[2])] = new wsBox($m[3], $m[4], $m[5], $m[6]);
+            } elseif (preg_match('|Page\s+([0-9]+)\s+size:\s+([0-9.]*)[pts[:space:]]+x\s+([0-9.]*)\s+pts|iu', $line, $m)) {
+                $res['infos']['page'][$m[1]]['size'] = array($m[2], $m[3]);
+                if ($m[1] == 1) {
+                    $res['infos']['size'][0] = $m[2];
+                    $res['infos']['size'][1] = $m[3];
+                }
+            } elseif ($k == 'BookmarkTitle') {
+                $res['bookmarks'][$bookmark_id] = array('titre' => str_replace('&#13;', '', trim($v)));
+            } elseif ($k == 'BookmarkLevel') {
+                $res['bookmarks'][$bookmark_id]['level'] = $v;
+            } elseif ($k == 'BookmarkPage') {
+                $res['bookmarks'][$bookmark_id]['page'] = $v;
+                $bookmark_id++;
+            } elseif ($k == 'NumberSections') {
+                $res['numberSections'] = $v;
+            }
+        }
+        return $res;
+    }
+
+    public static function infos($pdf)
+    {
+        $fwstk = new FWSTK();
+        $fwstk->setArg('--input ' . $pdf);
+        $fwstk->setArg('--infos');
+        $fwstk->execute();
+        $out = $fwstk->getOutput();
+
+        $pdfinfo = new CommandLine('pdfinfo');
+        $pdfinfo->setArg('-box');
+        $pdfinfo->setArg('f', 1);
+        $pdfinfo->setArg('l', 100000);
+        $pdfinfo->setArg(null, $pdf);
+        $pdfinfo->execute();
+        $out .= "\n";
+        $out .= $pdfinfo->getOutput();
+
+        return self::parseInfos($out);
+    }
+
     /**
      * @param $path string
      * @return string
@@ -370,6 +443,28 @@ class PDFTools
 
     }
 
+    public static function fixPDF($in, $out)
+    {
+        if (file_exists($out)) {
+            unlink($out);
+        }
+
+        $pdftk = new CommandLine('pdftk');
+        $pdftk->setArg(null, $in);
+        $pdftk->setArg(null, 'output');
+        $pdftk->setArg(null, $out);
+        $pdftk->execute();
+
+        if (!file_exists($out)) {
+            $pdftocairo = new CommandLine('pdftocairo');
+            $pdftocairo->setPath(CONVERTER_PATH);
+            $pdftocairo->setArg(null, '-pdf');
+            $pdftocairo->setArg(null, $in);
+            $pdftocairo->setArg(null, $out);
+            $pdftocairo->execute();
+        }
+    }
+
     public static function split($pdf, $out)
     {