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;
22 $this->middleware('can:content-export');
26 * Exports a chapter to pdf.
28 * @throws NotFoundException
31 public function pdf(string $bookSlug, string $chapterSlug)
33 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
34 $pdfContent = $this->exportFormatter->chapterToPdf($chapter);
36 return $this->downloadResponse($pdfContent, $chapterSlug . '.pdf');
40 * Export a chapter to a self-contained HTML file.
42 * @throws NotFoundException
45 public function html(string $bookSlug, string $chapterSlug)
47 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
48 $containedHtml = $this->exportFormatter->chapterToContainedHtml($chapter);
50 return $this->downloadResponse($containedHtml, $chapterSlug . '.html');
54 * Export a chapter to a simple plaintext .txt file.
56 * @throws NotFoundException
58 public function plainText(string $bookSlug, string $chapterSlug)
60 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
61 $chapterText = $this->exportFormatter->chapterToPlainText($chapter);
63 return $this->downloadResponse($chapterText, $chapterSlug . '.txt');
67 * Export a chapter to a simple markdown file.
69 * @throws NotFoundException
71 public function markdown(string $bookSlug, string $chapterSlug)
73 // TODO: This should probably export to a zip file.
74 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
75 $chapterText = $this->exportFormatter->chapterToMarkdown($chapter);
77 return $this->downloadResponse($chapterText, $chapterSlug . '.md');