From: Vincent Vanwaelscappel Date: Wed, 15 Sep 2021 15:24:08 +0000 (+0200) Subject: wip #4666 @1 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=469da40df3de230c47987ba14ed1c937f1533712;p=fluidbook_tools.git wip #4666 @1 --- diff --git a/src/Jobs/ProcessFile.php b/src/Jobs/ProcessFile.php index a9b58fe..8dfed35 100644 --- a/src/Jobs/ProcessFile.php +++ b/src/Jobs/ProcessFile.php @@ -160,6 +160,7 @@ class ProcessFile public function setJob(ProcessPage $job) { $this->job = $job; + return $this; } public function getPath($force = false) @@ -182,27 +183,16 @@ class ProcessFile $res = $dir . 'p' . $this->getPage() . '.' . $this->getFormat(); } else { $res = $dir . $prefix . $this->getPage() . '-' . $this->getResolution() . '.' . $this->getFormat(); - $alt = $dir . $prefix . $this->getResolution() . '-' . $this->getPage() . '.' . $this->getFormat(); } } else if ($this->getFormat() === 'swf') { $res = $dir . 'p' . $this->getPage() . '.' . $this->getFormat(); } $do = false; - if (!file_exists($res)) { - if (isset($alt) && file_exists($alt) && filesize($alt) > $minsize) { - rename($alt, $res); - $do = false; - } else { - $do = true; - } - } else if (filesize($res) < $minsize) { + if (!file_exists($res) || filesize($res) < $minsize) { $do = true; } else if (isset($reffile) && filemtime($res) < filemtime($reffile)) { $do = true; - } else if ($this->getFormat() === 'svg') { - $t = filemtime($res); - $do = $t < 1603181003 && $t > 1602662603; } if ($do || $force) { @@ -237,6 +227,9 @@ class ProcessFile PDFTools::makeSWF($this->getSplittedPDFPage(), $file, 1, $this->getResolution(), 80); } unlink($lock); + if (!file_exists($file)) { + throw new \Exception(sprintf('Failed to generate %s', $file)); + } } public function makeSVGFile($force = false) diff --git a/src/Jobs/ProcessPage.php b/src/Jobs/ProcessPage.php index d333f62..89ec3a6 100644 --- a/src/Jobs/ProcessPage.php +++ b/src/Jobs/ProcessPage.php @@ -52,6 +52,14 @@ class ProcessPage implements ShouldQueue $this->files = $files; } + /** + * @param int $page + */ + public function setPage(int $page): void + { + $this->page = $page; + } + /** * @return int */ diff --git a/src/PDF/Document.php b/src/PDF/Document.php index bd2cc95..7f1d39a 100644 --- a/src/PDF/Document.php +++ b/src/PDF/Document.php @@ -3,6 +3,7 @@ namespace Fluidbook\Tools\PDF; use Cubist\Util\CommandLine; +use Cubist\Util\Text; use Fluidbook\Tools\CommandLine\FWSTK; use Fluidbook\Tools\FluidbookTools; use Fluidbook\Tools\Jobs\ProcessFile; @@ -31,6 +32,16 @@ class Document */ protected $height; + /** + * @var array + */ + protected $pageNumbers = []; + + /** + * @var array + */ + protected $chapters = []; + public function __construct($in) { $this->in = $in; @@ -60,6 +71,7 @@ class Document $infos = $fwstk->getOutput(); + if (preg_match('/Pages:\s*(\d+)/', $infos, $matches)) { $this->pages = (int)$matches[1]; } @@ -67,6 +79,32 @@ class Document $this->width = (float)$matches[1]; $this->height = (float)$matches[2]; } + + if (preg_match('/NumberSections:\s*(.*)/', $infos, $matches)) { + $this->pageNumbers = explode(',', $matches[1]); + } else { + $this->pageNumbers = range(1, $this->pages); + } + + $lines = Text::explodeNewLines($infos); + $bookmark_id = -1; + + + foreach ($lines as $line) { + $line = trim($line); + [$k, $v] = explode(':', $line); + $k = trim($k); + $v = trim($v); + + if ($k === 'BookmarkTitle') { + $bookmark_id++; + $this->chapters[$bookmark_id] = array('label' => str_replace(' ', '', $v)); + } elseif ($k === 'BookmarkLevel') { + $this->chapters[$bookmark_id]['level'] = (int)$v - 1; + } elseif ($k === 'BookmarkPage') { + $this->chapters[$bookmark_id]['page'] = $v; + } + } } /** @@ -139,10 +177,24 @@ class Document stop_measure('Compile search index'); } - public function compileSearchIndex($dest){ + public function getPageNumbers() + { + $this->getInfos(); + return $this->pageNumbers; + } + + public function getChapters() + { + $this->getInfos(); + return $this->chapters; + } + + public function compileSearchIndex($dest) + { } + public function getResolutionRatio() { $a4surface = 500990; // en pt²