]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/ChapterExportApiController.php
Fixed some mis-refactoring and split search service
[bookstack] / app / Http / Controllers / Api / ChapterExportApiController.php
1 <?php namespace BookStack\Http\Controllers\Api;
2
3 use BookStack\Entities\Models\Chapter;
4 use BookStack\Entities\Tools\ExportFormatter;
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, ExportFormatter $exportService)
17     {
18         $this->chapterRepo = $chapterRepo;
19         $this->exportService = $exportService;
20     }
21
22     /**
23      * Export a chapter as a PDF file.
24      * @throws Throwable
25      */
26     public function exportPdf(int $id)
27     {
28         $chapter = Chapter::visible()->findOrFail($id);
29         $pdfContent = $this->exportService->chapterToPdf($chapter);
30         return $this->downloadResponse($pdfContent, $chapter->slug . '.pdf');
31     }
32
33     /**
34      * Export a chapter as a contained HTML file.
35      * @throws Throwable
36      */
37     public function exportHtml(int $id)
38     {
39         $chapter = Chapter::visible()->findOrFail($id);
40         $htmlContent = $this->exportService->chapterToContainedHtml($chapter);
41         return $this->downloadResponse($htmlContent, $chapter->slug . '.html');
42     }
43
44     /**
45      * Export a chapter as a plain text file.
46      */
47     public function exportPlainText(int $id)
48     {
49         $chapter = Chapter::visible()->findOrFail($id);
50         $textContent = $this->exportService->chapterToPlainText($chapter);
51         return $this->downloadResponse($textContent, $chapter->slug . '.txt');
52     }
53 }