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