]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/ChapterExportController.php
Merge branch 'master' of git://github.com/almandin/BookStack into almandin-master
[bookstack] / app / Http / Controllers / ChapterExportController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use BookStack\Entities\ExportService;
4 use BookStack\Entities\Repos\ChapterRepo;
5 use BookStack\Exceptions\NotFoundException;
6 use Throwable;
7
8 class ChapterExportController extends Controller
9 {
10
11     protected $chapterRepo;
12     protected $exportService;
13
14     /**
15      * ChapterExportController constructor.
16      */
17     public function __construct(ChapterRepo $chapterRepo, ExportService $exportService)
18     {
19         $this->chapterRepo = $chapterRepo;
20         $this->exportService = $exportService;
21         parent::__construct();
22     }
23
24     /**
25      * Exports a chapter to pdf.
26      * @throws NotFoundException
27      * @throws Throwable
28      */
29     public function pdf(string $bookSlug, string $chapterSlug)
30     {
31         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
32         $pdfContent = $this->exportService->chapterToPdf($chapter);
33         return $this->downloadResponse($pdfContent, $chapterSlug . '.pdf');
34     }
35
36     /**
37      * Export a chapter to a self-contained HTML file.
38      * @throws NotFoundException
39      * @throws Throwable
40      */
41     public function html(string $bookSlug, string $chapterSlug)
42     {
43         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
44         $containedHtml = $this->exportService->chapterToContainedHtml($chapter);
45         return $this->downloadResponse($containedHtml, $chapterSlug . '.html');
46     }
47
48     /**
49      * Export a chapter to a simple plaintext .txt file.
50      * @throws NotFoundException
51      */
52     public function plainText(string $bookSlug, string $chapterSlug)
53     {
54         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
55         $chapterText = $this->exportService->chapterToPlainText($chapter);
56         return $this->downloadResponse($chapterText, $chapterSlug . '.txt');
57     }
58 }