--- /dev/null
+<?php
+
+namespace App\Http\Controllers\Admin\Operations\FluidbookPublication\Services;
+
+use App\Http\Middleware\CheckIfAdmin;
+use App\Models\FluidbookPublication;
+use Cubist\Backpack\Http\Controllers\Base\XSendFileController;
+use Cubist\Util\ArrayUtil;
+use Cubist\Util\CommandLine;
+use Cubist\Util\Files\Files;
+use Illuminate\Support\Facades\Route;
+
+trait ExportPdfOperation
+{
+ protected function setupExportpdfRoutes($segment, $routeName, $controller)
+ {
+ foreach (['services', 's'] as $s) {
+ Route::match(['get'], $s . '/e/{cid}/{range}', $controller . '@pdfdownload')->withoutMiddleware([CheckIfAdmin::class]);
+ Route::match(['get'], $s . '/p/{cid}/{range}', $controller . '@pdfprint')->withoutMiddleware([CheckIfAdmin::class]);
+ Route::match(['get'], $s . '/ep/{cid}/{range}', $controller . '@pdfprintcontainer')->withoutMiddleware([CheckIfAdmin::class]);
+ }
+ }
+
+ public function pdfdownload($cid, $range)
+ {
+ return $this->pdf($cid, $range);
+ }
+
+
+ public function pdfprint($cid, $range)
+ {
+ return $this->pdf($cid, $range, true);
+ }
+
+ protected function pdf($cid, $range, $print = false)
+ {
+ set_time_limit(0);
+ /** @var FluidbookPublication $book */
+ $book = FluidbookPublication::withoutGlobalScopes()->where('cid', $cid)->first();
+ // Return the file
+ return XSendFileController::sendfileNoCache($this->getPDFComplex($book, $range, $print));
+ }
+
+ public function pdfprintcontainer($cid, $range)
+ {
+ // Prepare the file and wait for it before showing the iframe
+ $this->pdf($cid, $range, true);
+
+
+ return response('<!DOCTYPE html>
+<html>
+<head>
+ <script type="text/javascript">
+ document.addEventListener("DOMContentLoaded", function() {
+ document.getElementById(\'pdf\').addEventListener("readystatechange", function(event){printPDF();});
+ });
+ function printPDF() {
+ var iframe = document.getElementById(\'pdf\');
+ setTimeout(function() {
+ try {
+ iframe.contentWindow.print();
+ } catch (err) {
+ }
+ }, 2000);
+ }
+ </script>
+ <style>
+ *{
+ padding:0;
+ margin:0;
+ }
+ html,body{
+ height:100%;
+ }
+
+ body{
+ overflow: hidden;
+ }
+ </style>
+</head>
+<body>
+ <iframe id="pdf" name="pdff" src="/s/p/' . $cid . '/' . $range . '" width="100%" height="100%" onload="printPDF();"></iframe>
+</body>
+</html>');
+ }
+
+ /**
+ * @param $book FluidbookPublication
+ * @param $range string
+ * @param $print bool
+ * @return array|false
+ */
+ public function getPDFComplex($book, $range = null, $print = false)
+ {
+ // Normalize range
+ $range = ArrayUtil::parseRange($range, 1, $book->getPagesNumber());
+ if (!count($range)) {
+ return;
+ }
+
+ // Paths init
+ $baseDocument = $this->getPDFComplexBaseDocument($book);
+ $destDir = Files::mkdir(protected_path('fluidbookpublication/cache/exportpdf/' . $book->id));
+ $fname = md5($baseDocument) . '-' . md5(implode(',%ù', $range) . ($print ? '1' : '0')) . '.pdf';
+ $destFile = $destDir . '/' . $fname;
+
+ // If result exists, don't make the pdf again
+ if (!file_exists($destFile) || filemtime($destFile) < filemtime($baseDocument)) {
+ if ($print) {
+ $memoDest = $destFile;
+ $destFile = Files::tempnam();
+ }
+
+ if ($range[0] == 1 && count($range) == $book->getPagesNumber() && $range[$book->getPagesNumber() - 1] == $book->getPagesNumber()) {
+ // The request range is the whole document
+ $cp = new CommandLine('cp');
+ $cp->setArg(null, $baseDocument);
+ $cp->setArg(null, $destFile);
+ $cp->execute();
+// $cp->debug();
+ } else {
+ // Prepare the command line
+ $l = array('A="' . $baseDocument . '"', 'cat');
+ foreach ($range as $page) {
+ $l[] = 'A' . $page;
+ }
+ $l[] = 'output';
+ $l[] = $destFile;
+
+ $args = implode(' ', $l);
+ // Execute the command line
+ $pdftk = new CommandLine('pdftk');
+ $pdftk->setManualArg($args);
+ $pdftk->execute();
+// $pdftk->debug();
+ }
+
+ if ($print) {
+ $gs = new CommandLine('gs');
+ $gs->setArg('-dNoOutputFonts');
+ $gs->setManualArg('-sDEVICE=pdfwrite');
+ $gs->setArg('o', $memoDest);
+ $gs->setArg(null, $destFile);
+ $gs->execute();
+// $gs->debug();
+ }
+ }
+
+ return $destFile;
+ }
+
+ public function getPDFComplexBaseDocument($book, $force = false)
+ {
+ global $core;
+ $mode = $force ? $force : $book->bookmarkUsePDF;
+ /** @var $book FluidbookPublication */
+ $wid = $book->getAssetDir();
+ $res = false;
+ switch ($mode) {
+ case 'download':
+ $res = $wid . $book->pdfReplace;
+ break;
+ case 'thumbnails':
+ $res = $wid . $book->pdfThumbnails;
+ break;
+ default:
+ break;
+ }
+ if (!$res || !file_exists($res) || is_dir($res)) {
+ $res = Files::firstThatExists($book->getPDFDir() . 'original.pdf', $book->getPDFDir() . 'normal.pdf');
+ }
+ if (!file_exists($res) || is_dir($res)) {
+ return false;
+ }
+ return $res;
+ }
+
+}
use App\Http\Controllers\Admin\Operations\FluidbookPublication\EditOperation;
use App\Http\Controllers\Admin\Operations\FluidbookPublication\LinksOperation;
use App\Http\Controllers\Admin\Operations\FluidbookPublication\PreviewOperation;
+use App\Http\Controllers\Admin\Operations\FluidbookPublication\Services\ExportPdfOperation;
+use App\Http\Controllers\Admin\Operations\FluidbookPublication\Services\SocialImageOperation;
use App\Http\Controllers\Admin\Operations\FluidbookPublication\StatsOperation;
use App\Jobs\FluidbookImagesPreprocess;
use App\Models\Base\ToolboxSettingsModel;
protected static $_docs = [];
- protected $_operations = [CreateOperation::class, PreviewOperation::class, LinksOperation::class, CompositionOperation::class, StatsOperation::class, DownloadOperation::class, CloneOperation::class, DeletefbOperation::class, EditOperation::class, ChangeownerOperation::class];
+ protected $_operations = [CreateOperation::class,
+ PreviewOperation::class,
+ LinksOperation::class,
+ CompositionOperation::class, StatsOperation::class, DownloadOperation::class, CloneOperation::class, DeletefbOperation::class, EditOperation::class, ChangeownerOperation::class, SocialImageOperation::class, ExportPdfOperation::class];
protected $casts = ['composition' => 'array'];
*/
protected function _getFreeFileBaseDirectory()
{
- return '/application/protected/fluidbookpublication/working/' . $this->id;
+ return $this->getAssetDir();
}
/**
return protected_path('fluidbookpublication/final/' . $dir . '/' . ($scormVariant ? 'scorm' : 'online'));
}
+
public function getAssetDir()
{
- return Files::mkdir(protected_path('fluidbookpublication/working/' . $this->id));
+ return Files::mkdir(protected_path('fluidbookpublication/working/' . $this->getAssetDirId()));
+ }
+
+ public function getPDFDir()
+ {
+ return Files::mkdir(protected_path('fluidbookpublication/pdf/' . $this->id));
}
public function asset_path($path)
}
}
}
+
+ public function getAssetDirId()
+ {
+ return !isset($this->assetsDir) || !$this->assetsDir ? $this->id : trim($this->assetsDir);
+ }
}