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