3 namespace BookStack\Entities\Controllers;
5 use BookStack\Entities\Repos\ChapterRepo;
6 use BookStack\Entities\Tools\ExportFormatter;
7 use BookStack\Exceptions\NotFoundException;
8 use BookStack\Http\Controllers\Controller;
11 class ChapterExportController extends Controller
13 protected $chapterRepo;
14 protected $exportFormatter;
17 * ChapterExportController constructor.
19 public function __construct(ChapterRepo $chapterRepo, ExportFormatter $exportFormatter)
21 $this->chapterRepo = $chapterRepo;
22 $this->exportFormatter = $exportFormatter;
23 $this->middleware('can:content-export');
27 * Exports a chapter to pdf.
29 * @throws NotFoundException
32 public function pdf(string $bookSlug, string $chapterSlug)
34 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
35 $pdfContent = $this->exportFormatter->chapterToPdf($chapter);
37 return $this->download()->directly($pdfContent, $chapterSlug . '.pdf');
41 * Export a chapter to a self-contained HTML file.
43 * @throws NotFoundException
46 public function html(string $bookSlug, string $chapterSlug)
48 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
49 $containedHtml = $this->exportFormatter->chapterToContainedHtml($chapter);
51 return $this->download()->directly($containedHtml, $chapterSlug . '.html');
55 * Export a chapter to a simple plaintext .txt file.
57 * @throws NotFoundException
59 public function plainText(string $bookSlug, string $chapterSlug)
61 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
62 $chapterText = $this->exportFormatter->chapterToPlainText($chapter);
64 return $this->download()->directly($chapterText, $chapterSlug . '.txt');
68 * Export a chapter to a simple markdown file.
70 * @throws NotFoundException
72 public function markdown(string $bookSlug, string $chapterSlug)
74 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
75 $chapterText = $this->exportFormatter->chapterToMarkdown($chapter);
77 return $this->download()->directly($chapterText, $chapterSlug . '.md');