X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/919660678bec2b94eaa84ac60d0313f5ef07dfb7..refs/pull/2023/head:/app/Entities/ExportService.php diff --git a/app/Entities/ExportService.php b/app/Entities/ExportService.php index fc0e4d114..f945dfbe4 100644 --- a/app/Entities/ExportService.php +++ b/app/Entities/ExportService.php @@ -1,154 +1,145 @@ entityRepo = $entityRepo; $this->imageService = $imageService; } /** * Convert a page to a self-contained HTML file. * Includes required CSS & image content. Images are base64 encoded into the HTML. - * @param \BookStack\Entities\Page $page - * @return mixed|string - * @throws \Throwable + * @throws Throwable */ public function pageToContainedHtml(Page $page) { - $this->entityRepo->renderPage($page); - $pageHtml = view('pages/export', [ - 'page' => $page + $page->html = (new PageContent($page))->render(); + $pageHtml = view('pages.export', [ + 'page' => $page, + 'format' => 'html', ])->render(); return $this->containHtml($pageHtml); } /** * Convert a chapter to a self-contained HTML file. - * @param \BookStack\Entities\Chapter $chapter - * @return mixed|string - * @throws \Throwable + * @throws Throwable */ public function chapterToContainedHtml(Chapter $chapter) { - $pages = $this->entityRepo->getChapterChildren($chapter); + $pages = $chapter->getVisiblePages(); $pages->each(function ($page) { - $page->html = $this->entityRepo->renderPage($page); + $page->html = (new PageContent($page))->render(); }); - $html = view('chapters/export', [ + $html = view('chapters.export', [ 'chapter' => $chapter, - 'pages' => $pages + 'pages' => $pages, + 'format' => 'html', ])->render(); return $this->containHtml($html); } /** * Convert a book to a self-contained HTML file. - * @param Book $book - * @return mixed|string - * @throws \Throwable + * @throws Throwable */ public function bookToContainedHtml(Book $book) { - $bookTree = $this->entityRepo->getBookChildren($book, true, true); - $html = view('books/export', [ + $bookTree = (new BookContents($book))->getTree(false, true); + $html = view('books.export', [ 'book' => $book, - 'bookChildren' => $bookTree + 'bookChildren' => $bookTree, + 'format' => 'html', ])->render(); return $this->containHtml($html); } /** * Convert a page to a PDF file. - * @param Page $page - * @return mixed|string - * @throws \Throwable + * @throws Throwable */ public function pageToPdf(Page $page) { - $this->entityRepo->renderPage($page); - $html = view('pages/pdf', [ - 'page' => $page + $page->html = (new PageContent($page))->render(); + $html = view('pages.export', [ + 'page' => $page, + 'format' => 'pdf', ])->render(); return $this->htmlToPdf($html); } /** * Convert a chapter to a PDF file. - * @param \BookStack\Entities\Chapter $chapter - * @return mixed|string - * @throws \Throwable + * @throws Throwable */ public function chapterToPdf(Chapter $chapter) { - $pages = $this->entityRepo->getChapterChildren($chapter); + $pages = $chapter->getVisiblePages(); $pages->each(function ($page) { - $page->html = $this->entityRepo->renderPage($page); + $page->html = (new PageContent($page))->render(); }); - $html = view('chapters/export', [ + + $html = view('chapters.export', [ 'chapter' => $chapter, - 'pages' => $pages + 'pages' => $pages, + 'format' => 'pdf', ])->render(); + return $this->htmlToPdf($html); } /** - * Convert a book to a PDF file - * @param \BookStack\Entities\Book $book - * @return string - * @throws \Throwable + * Convert a book to a PDF file. + * @throws Throwable */ public function bookToPdf(Book $book) { - $bookTree = $this->entityRepo->getBookChildren($book, true, true); - $html = view('books/export', [ + $bookTree = (new BookContents($book))->getTree(false, true); + $html = view('books.export', [ 'book' => $book, - 'bookChildren' => $bookTree + 'bookChildren' => $bookTree, + 'format' => 'pdf', ])->render(); return $this->htmlToPdf($html); } /** - * Convert normal webpage HTML to a PDF. - * @param $html - * @return string - * @throws \Exception + * Convert normal web-page HTML to a PDF. + * @throws Exception */ - protected function htmlToPdf($html) + protected function htmlToPdf(string $html): string { $containedHtml = $this->containHtml($html); $useWKHTML = config('snappy.pdf.binary') !== false; if ($useWKHTML) { - $pdf = \SnappyPDF::loadHTML($containedHtml); + $pdf = SnappyPDF::loadHTML($containedHtml); $pdf->setOption('print-media-type', true); } else { - $pdf = \DomPDF::loadHTML($containedHtml); + $pdf = DomPDF::loadHTML($containedHtml); } return $pdf->output(); } /** * Bundle of the contents of a html file to be self-contained. - * @param $htmlContent - * @return mixed|string - * @throws \Exception + * @throws Exception */ - protected function containHtml($htmlContent) + protected function containHtml(string $htmlContent): string { $imageTagsOutput = []; preg_match_all("/\/i", $htmlContent, $imageTagsOutput); @@ -190,12 +181,10 @@ class ExportService /** * Converts the page contents into simple plain text. * This method filters any bad looking content to provide a nice final output. - * @param Page $page - * @return mixed */ - public function pageToPlainText(Page $page) + public function pageToPlainText(Page $page): string { - $html = $this->entityRepo->renderPage($page); + $html = (new PageContent($page))->render(); $text = strip_tags($html); // Replace multiple spaces with single spaces $text = preg_replace('/\ {2,}/', ' ', $text); @@ -209,10 +198,8 @@ class ExportService /** * Convert a chapter into a plain text string. - * @param \BookStack\Entities\Chapter $chapter - * @return string */ - public function chapterToPlainText(Chapter $chapter) + public function chapterToPlainText(Chapter $chapter): string { $text = $chapter->name . "\n\n"; $text .= $chapter->description . "\n\n"; @@ -224,12 +211,10 @@ class ExportService /** * Convert a book into a plain text string. - * @param Book $book - * @return string */ - public function bookToPlainText(Book $book) + public function bookToPlainText(Book $book): string { - $bookTree = $this->entityRepo->getBookChildren($book, true, true); + $bookTree = (new BookContents($book))->getTree(false, true); $text = $book->name . "\n\n"; foreach ($bookTree as $bookChild) { if ($bookChild->isA('chapter')) {