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