]> BookStack Code Mirror - bookstack/blob - app/Exports/Controllers/ChapterExportController.php
ZIP Exports: Finished up format doc, move files, started builder
[bookstack] / app / Exports / Controllers / ChapterExportController.php
1 <?php
2
3 namespace BookStack\Exports\Controllers;
4
5 use BookStack\Entities\Queries\ChapterQueries;
6 use BookStack\Exceptions\NotFoundException;
7 use BookStack\Exports\ExportFormatter;
8 use BookStack\Http\Controller;
9 use Throwable;
10
11 class ChapterExportController extends Controller
12 {
13     public function __construct(
14         protected ChapterQueries $queries,
15         protected ExportFormatter $exportFormatter,
16     ) {
17         $this->middleware('can:content-export');
18     }
19
20     /**
21      * Exports a chapter to pdf.
22      *
23      * @throws NotFoundException
24      * @throws Throwable
25      */
26     public function pdf(string $bookSlug, string $chapterSlug)
27     {
28         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
29         $pdfContent = $this->exportFormatter->chapterToPdf($chapter);
30
31         return $this->download()->directly($pdfContent, $chapterSlug . '.pdf');
32     }
33
34     /**
35      * Export a chapter to a self-contained HTML file.
36      *
37      * @throws NotFoundException
38      * @throws Throwable
39      */
40     public function html(string $bookSlug, string $chapterSlug)
41     {
42         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
43         $containedHtml = $this->exportFormatter->chapterToContainedHtml($chapter);
44
45         return $this->download()->directly($containedHtml, $chapterSlug . '.html');
46     }
47
48     /**
49      * Export a chapter to a simple plaintext .txt file.
50      *
51      * @throws NotFoundException
52      */
53     public function plainText(string $bookSlug, string $chapterSlug)
54     {
55         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
56         $chapterText = $this->exportFormatter->chapterToPlainText($chapter);
57
58         return $this->download()->directly($chapterText, $chapterSlug . '.txt');
59     }
60
61     /**
62      * Export a chapter to a simple markdown file.
63      *
64      * @throws NotFoundException
65      */
66     public function markdown(string $bookSlug, string $chapterSlug)
67     {
68         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
69         $chapterText = $this->exportFormatter->chapterToMarkdown($chapter);
70
71         return $this->download()->directly($chapterText, $chapterSlug . '.md');
72     }
73 }