]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/ChapterExportApiController.php
Apply fixes from StyleCI
[bookstack] / app / Http / Controllers / Api / ChapterExportApiController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Api;
4
5 use BookStack\Entities\Models\Chapter;
6 use BookStack\Entities\Tools\ExportFormatter;
7 use Throwable;
8
9 class ChapterExportApiController extends ApiController
10 {
11     protected $exportFormatter;
12
13     /**
14      * ChapterExportController constructor.
15      */
16     public function __construct(ExportFormatter $exportFormatter)
17     {
18         $this->exportFormatter = $exportFormatter;
19     }
20
21     /**
22      * Export a chapter as a PDF file.
23      *
24      * @throws Throwable
25      */
26     public function exportPdf(int $id)
27     {
28         $chapter = Chapter::visible()->findOrFail($id);
29         $pdfContent = $this->exportFormatter->chapterToPdf($chapter);
30
31         return $this->downloadResponse($pdfContent, $chapter->slug . '.pdf');
32     }
33
34     /**
35      * Export a chapter as a contained HTML file.
36      *
37      * @throws Throwable
38      */
39     public function exportHtml(int $id)
40     {
41         $chapter = Chapter::visible()->findOrFail($id);
42         $htmlContent = $this->exportFormatter->chapterToContainedHtml($chapter);
43
44         return $this->downloadResponse($htmlContent, $chapter->slug . '.html');
45     }
46
47     /**
48      * Export a chapter as a plain text file.
49      */
50     public function exportPlainText(int $id)
51     {
52         $chapter = Chapter::visible()->findOrFail($id);
53         $textContent = $this->exportFormatter->chapterToPlainText($chapter);
54
55         return $this->downloadResponse($textContent, $chapter->slug . '.txt');
56     }
57
58     /**
59      * Export a chapter as a markdown file.
60      */
61     public function exportMarkdown(int $id)
62     {
63         $chapter = Chapter::visible()->findOrFail($id);
64         $markdown = $this->exportFormatter->chapterToMarkdown($chapter);
65
66         return $this->downloadResponse($markdown, $chapter->slug . '.md');
67     }
68 }