From dcbf4a64d1f68ed483208e0672add75f0fd3fd27 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Tue, 23 Aug 2022 20:13:48 +0200 Subject: [PATCH] wip #5412 @0.5 --- app/Fluidbook/PDF.php | 200 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+) create mode 100644 app/Fluidbook/PDF.php diff --git a/app/Fluidbook/PDF.php b/app/Fluidbook/PDF.php new file mode 100644 index 000000000..40e981ff3 --- /dev/null +++ b/app/Fluidbook/PDF.php @@ -0,0 +1,200 @@ +parametres->pdfName, 0, 4) == 'http') { + return false; + } + + $cacheDir = WS_BOOKS . '/pdf/' . $book->book_id; + if (!file_exists($cacheDir)) { + mkdir($cacheDir, 0777, true); + } + + $normalPDF = $cacheDir . '/normal.pdf'; + $originalPDF = $cacheDir . '/original.pdf'; + $compressedPDF = $cacheDir . '/compressed.pdf'; + + $files = [$normalPDF, $originalPDF, $compressedPDF]; + + foreach ($files as $file) { + if (file_exists($file) && filesize($file) == 0) { + unlink($file); + } + } + + if (file_exists($originalPDF)) { + $fmtime = filemtime($originalPDF); + if ($fmtime >= $book->composition_update) { + $invalid = false; + foreach ($pages as $i => $infos) { + $doc = wsDocument::getDir($infos['document_id']) . 'crop.pdf'; + if (file_exists($doc) && filemtime($doc) > $fmtime) { + if (null !== $log) { + $log->log('PDF File invalide : one most recent ' . $infos['document_id']); + } + $invalid = true; + break; + } + } + } else { + if (null !== $log) { + $log->log('PDF File invalide : recent composition change'); + } + $invalid = true; + } + } else { + if (null !== $log) { + $log->log('PDF File invalide : original not exists'); + } + $invalid = true; + } + + if (null !== $log) { + $log->log('PDF File invalide : ' . $invalid); + } + + if ($invalid) { + + if (file_exists($originalPDF)) { + self::copy($originalPDF, $cacheDir . '/original.' . TIME . '.pdf', false); + } + + $pdfList = array(); + $pagesList = array(); + $nb_pages = array(); + $j = 0; + $k = 0; + $original = true; + + foreach ($pages as $i => $infos) { + if (!isset($firstDoc)) { + $firstDoc = $infos['document_id']; + } + + $doc = wsDocument::getDir($infos['document_id']) . 'crop.pdf'; + if (!isset($pdfList[$doc])) { + $pdfList[$doc] = $j; + $nb_pages[$doc] = $infos['nb_pages']; + $k = $j; + $j++; + } else { + $k = $pdfList[$doc]; + } + $pagesList[$i] = array($k, $infos['document_page']); + + if ($i != $infos['document_page'] || $infos['document_id'] != $firstDoc) { + $original = false; + } + } + + if ($original) { + self::copy(wsDocument::getDir($firstDoc) . 'crop.pdf', $originalPDF, false); + } else { + $args = ''; + foreach ($pdfList as $doc => $index) { + $lettre = cubeMath::toPDFLetter($index, true); + $args .= $lettre . '=' . $doc . ' '; + } + + $args .= ' cat '; + + $ranges = array(); + $currentRange = null; + + foreach ($pagesList as $p) { + $lettre = cubeMath::toPDFLetter($p[0], true); + $page = $p[1]; + + // Initialise l'intervale + if (is_null($currentRange)) { + $currentRange = array('lettre' => $lettre, 'start' => $page, 'end' => $page); + continue; + } + + // Poursuit le remplissage si la lettre est identique et si la page suivante est bien la page suivante dans le document + if ($currentRange['lettre'] == $lettre && $currentRange['end'] + 1 == $page) { + $currentRange['end'] = $page; + continue; + } + + // Ajoute l'intervale à la liste finale + $ranges[] = $currentRange; + + // Réinitialise l'intervale suivant + $currentRange = array('lettre' => $lettre, 'start' => $page, 'end' => $page); + } + + // Ajoute la dernière + if (!is_null($currentRange)) { + $ranges[] = $currentRange; + } + // Si le pdf final est constitué du document complet d'un document + if (count($ranges) == 1 && $ranges[0]['start'] == 1) { + $alldocs = array_keys($pdfList); + $doc = array_pop($alldocs); + if ($nb_pages[$doc] == $ranges[0]['end']) { + self::copy($doc, $originalPDF); + return; + } + } + + foreach ($ranges as $range) { + $args .= ' ' . $range['lettre'] . $range['start']; + if ($range['start'] == $range['end']) { + continue; + } + $args .= '-' . $range['end']; + } + + $hash = sha1($args); + + $args .= ' output ' . $originalPDF; + + $cached = WS_BOOKS . '/pdf/' . $hash . '.pdf'; + + if (file_exists($cached) && filesize($cached) > 0) { + self::copy($cached, $originalPDF, false); + } else { + $pdftk = new cubeCommandLine('pdftk'); + $pdftk->setPath(CONVERTER_PATH); + $pdftk->setManualArg($args); + $pdftk->execute(); + self::copy($normalPDF, $cached, false); + } + } + } + + + if ($book->parametres->pdfReplace) { + $replace = $book->getAssetDir() . $book->parametres->pdfReplace; + if (file_exists($replace) && filesize($replace) > 0) { + if (!file_exists($normalPDF) || filemtime($normalPDF) < filemtime($replace) || filesize($normalPDF) != filesize($replace)) { + self::copy($replace, $normalPDF, false); + } + } + } else { + self::copy($originalPDF, $normalPDF, false); + } + + if ($book->parametres->pdfCompress) { + if (!file_exists($compressedPDF) || filemtime($compressedPDF) < filemtime($normalPDF)) { + + } + return $compressedPDF; + } + return $normalPDF; + } +} -- 2.39.5