3 namespace BookStack\Http\Controllers;
5 use BookStack\Entities\Repos\ChapterRepo;
6 use BookStack\Entities\Tools\ExportFormatter;
7 use BookStack\Exceptions\NotFoundException;
10 class ChapterExportController extends Controller
12 protected $chapterRepo;
13 protected $exportFormatter;
16 * ChapterExportController constructor.
18 public function __construct(ChapterRepo $chapterRepo, ExportFormatter $exportFormatter)
20 $this->chapterRepo = $chapterRepo;
21 $this->exportFormatter = $exportFormatter;
25 * Exports a chapter to pdf.
27 * @throws NotFoundException
30 public function pdf(string $bookSlug, string $chapterSlug)
32 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
33 $pdfContent = $this->exportFormatter->chapterToPdf($chapter);
35 return $this->downloadResponse($pdfContent, $chapterSlug . '.pdf');
39 * Export a chapter to a self-contained HTML file.
41 * @throws NotFoundException
44 public function html(string $bookSlug, string $chapterSlug)
46 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
47 $containedHtml = $this->exportFormatter->chapterToContainedHtml($chapter);
49 return $this->downloadResponse($containedHtml, $chapterSlug . '.html');
53 * Export a chapter to a simple plaintext .txt file.
55 * @throws NotFoundException
57 public function plainText(string $bookSlug, string $chapterSlug)
59 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
60 $chapterText = $this->exportFormatter->chapterToPlainText($chapter);
62 return $this->downloadResponse($chapterText, $chapterSlug . '.txt');
66 * Export a chapter to a simple markdown file.
68 * @throws NotFoundException
70 public function markdown(string $bookSlug, string $chapterSlug)
72 // TODO: This should probably export to a zip file.
73 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
74 $chapterText = $this->exportFormatter->chapterToMarkdown($chapter);
76 return $this->downloadResponse($chapterText, $chapterSlug . '.md');