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