]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/ChapterExportController.php
Show bookshelves that a book belongs to on a book view
[bookstack] / app / Http / Controllers / ChapterExportController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Entities\ExportService;
6 use BookStack\Entities\Repos\EntityRepo;
7 use BookStack\Exceptions\NotFoundException;
8 use Illuminate\Http\Response;
9 use Throwable;
10
11 class ChapterExportController extends Controller
12 {
13     /**
14      * @var EntityRepo
15      */
16     protected $entityRepo;
17
18     /**
19      * @var ExportService
20      */
21     protected $exportService;
22
23     /**
24      * ChapterExportController constructor.
25      * @param EntityRepo $entityRepo
26      * @param ExportService $exportService
27      */
28     public function __construct(EntityRepo $entityRepo, ExportService $exportService)
29     {
30         $this->entityRepo = $entityRepo;
31         $this->exportService = $exportService;
32         parent::__construct();
33     }
34
35     /**
36      * Exports a chapter to pdf .
37      * @param string $bookSlug
38      * @param string $chapterSlug
39      * @return Response
40      * @throws NotFoundException
41      * @throws Throwable
42      */
43     public function pdf(string $bookSlug, string $chapterSlug)
44     {
45         $chapter = $this->entityRepo->getEntityBySlug('chapter', $chapterSlug, $bookSlug);
46         $pdfContent = $this->exportService->chapterToPdf($chapter);
47         return $this->downloadResponse($pdfContent, $chapterSlug . '.pdf');
48     }
49
50     /**
51      * Export a chapter to a self-contained HTML file.
52      * @param string $bookSlug
53      * @param string $chapterSlug
54      * @return Response
55      * @throws NotFoundException
56      * @throws Throwable
57      */
58     public function html(string $bookSlug, string $chapterSlug)
59     {
60         $chapter = $this->entityRepo->getEntityBySlug('chapter', $chapterSlug, $bookSlug);
61         $containedHtml = $this->exportService->chapterToContainedHtml($chapter);
62         return $this->downloadResponse($containedHtml, $chapterSlug . '.html');
63     }
64
65     /**
66      * Export a chapter to a simple plaintext .txt file.
67      * @param string $bookSlug
68      * @param string $chapterSlug
69      * @return Response
70      * @throws NotFoundException
71      */
72     public function plainText(string $bookSlug, string $chapterSlug)
73     {
74         $chapter = $this->entityRepo->getEntityBySlug('chapter', $chapterSlug, $bookSlug);
75         $chapterText = $this->exportService->chapterToPlainText($chapter);
76         return $this->downloadResponse($chapterText, $chapterSlug . '.txt');
77     }
78 }