]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/ChapterExportApiController.php
ZIP Export: Continued expanding format doc types
[bookstack] / app / Entities / Controllers / ChapterExportApiController.php
1 <?php
2
3 namespace BookStack\Entities\Controllers;
4
5 use BookStack\Entities\Queries\ChapterQueries;
6 use BookStack\Entities\Tools\ExportFormatter;
7 use BookStack\Http\ApiController;
8 use Throwable;
9
10 class ChapterExportApiController extends ApiController
11 {
12     public function __construct(
13         protected ExportFormatter $exportFormatter,
14         protected ChapterQueries $queries,
15     ) {
16         $this->middleware('can:content-export');
17     }
18
19     /**
20      * Export a chapter as a PDF file.
21      *
22      * @throws Throwable
23      */
24     public function exportPdf(int $id)
25     {
26         $chapter = $this->queries->findVisibleByIdOrFail($id);
27         $pdfContent = $this->exportFormatter->chapterToPdf($chapter);
28
29         return $this->download()->directly($pdfContent, $chapter->slug . '.pdf');
30     }
31
32     /**
33      * Export a chapter as a contained HTML file.
34      *
35      * @throws Throwable
36      */
37     public function exportHtml(int $id)
38     {
39         $chapter = $this->queries->findVisibleByIdOrFail($id);
40         $htmlContent = $this->exportFormatter->chapterToContainedHtml($chapter);
41
42         return $this->download()->directly($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 = $this->queries->findVisibleByIdOrFail($id);
51         $textContent = $this->exportFormatter->chapterToPlainText($chapter);
52
53         return $this->download()->directly($textContent, $chapter->slug . '.txt');
54     }
55
56     /**
57      * Export a chapter as a markdown file.
58      */
59     public function exportMarkdown(int $id)
60     {
61         $chapter = $this->queries->findVisibleByIdOrFail($id);
62         $markdown = $this->exportFormatter->chapterToMarkdown($chapter);
63
64         return $this->download()->directly($markdown, $chapter->slug . '.md');
65     }
66 }