From abf046cbfc76c5c18fb88f8da26dd2e46720426c Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 6 Apr 2023 11:20:12 +0200 Subject: [PATCH] wait #5844 @2 --- src/Compiler/Compiler.php | 12 +++++++ src/Compiler/CompilerInterface.php | 4 +++ src/Compiler/DummyCompiler.php | 8 +++++ src/Compiler/FluidbookCompiler.php | 1 + src/Links/ArticlePDFLink.php | 56 ++++++++++++++++++++++++++++++ src/Links/Link.php | 6 +++- src/Links/PDFPopupLink.php | 11 +++++- 7 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 src/Links/ArticlePDFLink.php diff --git a/src/Compiler/Compiler.php b/src/Compiler/Compiler.php index eaeb746..366b2cb 100644 --- a/src/Compiler/Compiler.php +++ b/src/Compiler/Compiler.php @@ -380,4 +380,16 @@ class Compiler implements ShouldQueue, ShouldBeUnique, CompilerInterface public function isOnePage(): bool { // TODO: Implement isOnePage() method. } + + public function addPDFJS($force = false) { + // TODO: Implement addPDFJS() method. + } + + public function addSEOArticle($page, $title, $intro, $image, $id = null, $url = null, $content = '') { + // TODO: Implement addSEOArticle() method. + } + + public function pushSetting($key, $value) { + // TODO: Implement pushSetting() method. + } } diff --git a/src/Compiler/CompilerInterface.php b/src/Compiler/CompilerInterface.php index 36a79f9..96e3f0b 100644 --- a/src/Compiler/CompilerInterface.php +++ b/src/Compiler/CompilerInterface.php @@ -51,4 +51,8 @@ interface CompilerInterface { public function simpleCopyLinkFile($source, $dest); public function unzipFile($file, $moveAssets = false, $baseDir = null, $junkPaths = false); + + public function addPDFJS($force = false); + + public function addSEOArticle($page, $title, $intro, $image, $id = null, $url = null, $content = ''); } diff --git a/src/Compiler/DummyCompiler.php b/src/Compiler/DummyCompiler.php index f5e15d6..393ffee 100644 --- a/src/Compiler/DummyCompiler.php +++ b/src/Compiler/DummyCompiler.php @@ -107,4 +107,12 @@ class DummyCompiler implements CompilerInterface { public function unzipFile($file, $moveAssets = false, $baseDir = null, $junkPaths = false) { // TODO: Implement unzipFile() method. } + + public function addPDFJS($force = false) { + // TODO: Implement addPDFJS() method. + } + + public function addSEOArticle($page, $title, $intro, $image, $id = null, $url = null, $content = '') { + // TODO: Implement addSEOArticle() method. + } } diff --git a/src/Compiler/FluidbookCompiler.php b/src/Compiler/FluidbookCompiler.php index 7b6d28c..b4917aa 100644 --- a/src/Compiler/FluidbookCompiler.php +++ b/src/Compiler/FluidbookCompiler.php @@ -64,4 +64,5 @@ trait FluidbookCompiler + } diff --git a/src/Links/ArticlePDFLink.php b/src/Links/ArticlePDFLink.php new file mode 100644 index 0000000..7d68949 --- /dev/null +++ b/src/Links/ArticlePDFLink.php @@ -0,0 +1,56 @@ +compiler->addPDFJS(true); + + $e = explode('.', $this->to); + array_pop($e); + $name = implode('.', $e); + $pdffile = $this->compiler->getWorkingDir() . $this->to; + $mtime = filemtime($pdffile); + + $htmlfile = $this->compiler->getWorkingDir() . $name . '.html'; + if (!file_exists($htmlfile) || filemtime($htmlfile) < $mtime) { + $cmd = "pdftotext -f 1 -l 1 -htmlmeta $pdffile $htmlfile"; + `$cmd`; + } + + $cut = $this->wdir . $name . '.c.pdf'; + $size = $this->wdir . $name . '.s.pdf'; + + $dim = Image::getimagesize($pdffile); + $infos = ['width' => $dim[0], 'height' => $dim[1]]; + if (!file_exists($cut) || !file_exists($size) || filemtime($size) < $mtime) { + // Cut the pdf in many pages + $ratio = $infos['width'] / $infos['height']; + $nb = max(1, ceil(0.707071 / $ratio)); + + if ($nb == 1) { + copy($pdffile, $cut); + } else { + `mutool poster -y $nb $pdffile $cut`; + } + + // Resize to 210mm width + `pdfposter $cut $size`; + } else { + + } + + $this->copyExternalFile($name . '.s.pdf'); + $this->copyExternalFile($name . '.c.pdf'); + $text = file_get_contents($htmlfile); + $this->article = ['page' => $this->page, 'type' => 'pdf', 'id' => $name, 'url' => $name, 'infos' => $infos, 'printcontent' => $name . '.s.pdf', 'content' => $name . '.c.pdf', 'textcontent' => $text]; + + $this->compiler->pushSetting('articlesList', $this->article); + $this->compiler->addSEOArticle('#/article/' . $name, $this->title, $this->description, '', $name, $name . '.html', $text); + } +} diff --git a/src/Links/Link.php b/src/Links/Link.php index 91bc09b..3ca77e1 100644 --- a/src/Links/Link.php +++ b/src/Links/Link.php @@ -204,7 +204,11 @@ class Link { case 35: return new TextLink($id, $init, $compiler); case 36: - return new ArticleLink($id, $init, $compiler); + if (stripos($init['to'], '.pdf') !== false) { + return new ArticlePDFLink($id, $init, $compiler); + } else { + return new ArticleLink($id, $init, $compiler); + } case 37: return new DownloadPortionLink($id, $init, $compiler); case 38: diff --git a/src/Links/PDFPopupLink.php b/src/Links/PDFPopupLink.php index 4f03f02..9bb73c7 100644 --- a/src/Links/PDFPopupLink.php +++ b/src/Links/PDFPopupLink.php @@ -2,13 +2,22 @@ namespace Fluidbook\Tools\Links; +use Cubist\Util\Graphics\Image; +use Cubist\PDF\PDFTools; + class PDFPopupLink extends NormalLink { public function getDefaultTooltip() { return 'click to open the document'; } + /** + * @throws \Exception + */ public function getURL() { + $this->compiler->addPDFJS(true); + $dim = PDFTools::getDimensions($this->compiler->getWorkingDir() . $this->to); + $this->compiler->config->set('pdfLinks.' . $this->uid, ['width' => $dim['size'][0], 'height' => $dim['size'][1], 'totalHeight' => $dim['totalHeight'], 'file' => $this->to, 'interface' => $this->pdfjs]); $this->copyExternalFile($this->to); - return '#/pdf/' . $this->to; + return '#/pdf/' . $this->uid; } } -- 2.39.5