{{$chapter->name}}
+ +{{ $chapter->description }}
+ + @if(count($pages) > 0) +-
+ @foreach($pages as $page)
+
- {{ $page->name }} + @endforeach +
diff --git a/app/Http/Controllers/ChapterController.php b/app/Http/Controllers/ChapterController.php index 1760ee5c6..ceeb2a3ef 100644 --- a/app/Http/Controllers/ChapterController.php +++ b/app/Http/Controllers/ChapterController.php @@ -3,6 +3,7 @@ use Activity; use BookStack\Repos\EntityRepo; use BookStack\Repos\UserRepo; +use BookStack\Services\ExportService; use Illuminate\Http\Request; use Illuminate\Http\Response; use Views; @@ -12,16 +13,19 @@ class ChapterController extends Controller protected $userRepo; protected $entityRepo; + protected $exportService; /** * ChapterController constructor. * @param EntityRepo $entityRepo * @param UserRepo $userRepo + * @param ExportService $exportService */ - public function __construct(EntityRepo $entityRepo, UserRepo $userRepo) + public function __construct(EntityRepo $entityRepo, UserRepo $userRepo, ExportService $exportService) { $this->entityRepo = $entityRepo; $this->userRepo = $userRepo; + $this->exportService = $exportService; parent::__construct(); } @@ -236,4 +240,52 @@ class ChapterController extends Controller session()->flash('success', trans('entities.chapters_permissions_success')); return redirect($chapter->getUrl()); } + + /** + * Exports a chapter to pdf . + * @param string $bookSlug + * @param string $chapterSlug + * @return \Illuminate\Http\Response + */ + public function exportPdf($bookSlug, $chapterSlug) + { + $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug); + $pdfContent = $this->exportService->chapterToPdf($chapter); + return response()->make($pdfContent, 200, [ + 'Content-Type' => 'application/octet-stream', + 'Content-Disposition' => 'attachment; filename="' . $chapterSlug . '.pdf' + ]); + } + + /** + * Export a chapter to a self-contained HTML file. + * @param string $bookSlug + * @param string $chapterSlug + * @return \Illuminate\Http\Response + */ + public function exportHtml($bookSlug, $chapterSlug) + { + $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug); + $containedHtml = $this->exportService->chapterToContainedHtml($chapter); + return response()->make($containedHtml, 200, [ + 'Content-Type' => 'application/octet-stream', + 'Content-Disposition' => 'attachment; filename="' . $chapterSlug . '.html' + ]); + } + + /** + * Export a chapter to a simple plaintext .txt file. + * @param string $bookSlug + * @param string $chapterSlug + * @return \Illuminate\Http\Response + */ + public function exportPlainText($bookSlug, $chapterSlug) + { + $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug); + $containedHtml = $this->exportService->chapterToPlainText($chapter); + return response()->make($containedHtml, 200, [ + 'Content-Type' => 'application/octet-stream', + 'Content-Disposition' => 'attachment; filename="' . $chapterSlug . '.txt' + ]); + } } diff --git a/app/Http/Controllers/PageController.php b/app/Http/Controllers/PageController.php index 4a29c20d6..c97597bc4 100644 --- a/app/Http/Controllers/PageController.php +++ b/app/Http/Controllers/PageController.php @@ -429,7 +429,7 @@ class PageController extends Controller } /** - * Exports a page to pdf format using barryvdh/laravel-dompdf wrapper. + * Exports a page to a PDF. * https://github.com/barryvdh/laravel-dompdf * @param string $bookSlug * @param string $pageSlug diff --git a/app/Services/ExportService.php b/app/Services/ExportService.php index 3ac698718..78cef41a4 100644 --- a/app/Services/ExportService.php +++ b/app/Services/ExportService.php @@ -1,6 +1,7 @@ containHtml($pageHtml); } + /** + * Convert a chapter to a self-contained HTML file. + * @param Chapter $chapter + * @return mixed|string + */ + public function chapterToContainedHtml(Chapter $chapter) + { + $pages = $this->entityRepo->getChapterChildren($chapter); + $pages->each(function($page) { + $page->html = $this->entityRepo->renderPage($page); + }); + $html = view('chapters/export', [ + 'chapter' => $chapter, + 'pages' => $pages + ])->render(); + return $this->containHtml($html); + } + /** * Convert a book to a self-contained HTML file. * @param Book $book @@ -62,6 +81,24 @@ class ExportService return $this->htmlToPdf($html); } + /** + * Convert a chapter to a PDF file. + * @param Chapter $chapter + * @return mixed|string + */ + public function chapterToPdf(Chapter $chapter) + { + $pages = $this->entityRepo->getChapterChildren($chapter); + $pages->each(function($page) { + $page->html = $this->entityRepo->renderPage($page); + }); + $html = view('chapters/export', [ + 'chapter' => $chapter, + 'pages' => $pages + ])->render(); + return $this->htmlToPdf($html); + } + /** * Convert a book to a PDF file * @param Book $book @@ -168,6 +205,21 @@ class ExportService return $text; } + /** + * Convert a chapter into a plain text string. + * @param Chapter $chapter + * @return string + */ + public function chapterToPlainText(Chapter $chapter) + { + $text = $chapter->name . "\n\n"; + $text .= $chapter->description . "\n\n"; + foreach ($chapter->pages as $page) { + $text .= $this->pageToPlainText($page); + } + return $text; + } + /** * Convert a book into a plain text string. * @param Book $book @@ -179,11 +231,7 @@ class ExportService $text = $book->name . "\n\n"; foreach ($bookTree as $bookChild) { if ($bookChild->isA('chapter')) { - $text .= $bookChild->name . "\n\n"; - $text .= $bookChild->description . "\n\n"; - foreach ($bookChild->pages as $page) { - $text .= $this->pageToPlainText($page); - } + $text .= $this->chapterToPlainText($bookChild); } else { $text .= $this->pageToPlainText($bookChild); } diff --git a/resources/views/chapters/export.blade.php b/resources/views/chapters/export.blade.php new file mode 100644 index 000000000..57fcd1649 --- /dev/null +++ b/resources/views/chapters/export.blade.php @@ -0,0 +1,52 @@ + + +
+ +{{ $chapter->description }}
+ + @if(count($pages) > 0) +