]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/ChapterExportApiController.php
Merge pull request #1 from BookStackApp/master
[bookstack] / app / Http / Controllers / Api / ChapterExportApiController.php
1 <?php namespace BookStack\Http\Controllers\Api;
2
3 use BookStack\Entities\Chapter;
4 use BookStack\Entities\ExportService;
5 use BookStack\Entities\Repos\BookRepo;
6 use Throwable;
7
8 class ChapterExportApiController extends ApiController
9 {
10     protected $chapterRepo;
11     protected $exportService;
12
13     /**
14      * ChapterExportController constructor.
15      */
16     public function __construct(BookRepo $chapterRepo, ExportService $exportService)
17     {
18         $this->chapterRepo = $chapterRepo;
19         $this->exportService = $exportService;
20         parent::__construct();
21     }
22
23     /**
24      * Export a chapter as a PDF file.
25      * @throws Throwable
26      */
27     public function exportPdf(int $id)
28     {
29         $chapter = Chapter::visible()->findOrFail($id);
30         $pdfContent = $this->exportService->chapterToPdf($chapter);
31         return $this->downloadResponse($pdfContent, $chapter->slug . '.pdf');
32     }
33
34     /**
35      * Export a chapter as a contained HTML file.
36      * @throws Throwable
37      */
38     public function exportHtml(int $id)
39     {
40         $chapter = Chapter::visible()->findOrFail($id);
41         $htmlContent = $this->exportService->chapterToContainedHtml($chapter);
42         return $this->downloadResponse($htmlContent, $chapter->slug . '.html');
43     }
44
45     /**
46      * Export a chapter as a plain text file.
47      */
48     public function exportPlainText(int $id)
49     {
50         $chapter = Chapter::visible()->findOrFail($id);
51         $textContent = $this->exportService->chapterToPlainText($chapter);
52         return $this->downloadResponse($textContent, $chapter->slug . '.txt');
53     }
54 }