]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/ChapterExportController.php
Added a "skip to content" link.
[bookstack] / app / Http / Controllers / ChapterExportController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use BookStack\Entities\Tools\ExportFormatter;
4 use BookStack\Entities\Repos\ChapterRepo;
5 use BookStack\Exceptions\NotFoundException;
6 use Throwable;
7
8 class ChapterExportController extends Controller
9 {
10
11     protected $chapterRepo;
12     protected $exportFormatter;
13
14     /**
15      * ChapterExportController constructor.
16      */
17     public function __construct(ChapterRepo $chapterRepo, ExportFormatter $exportFormatter)
18     {
19         $this->chapterRepo = $chapterRepo;
20         $this->exportFormatter = $exportFormatter;
21     }
22
23     /**
24      * Exports a chapter to pdf.
25      * @throws NotFoundException
26      * @throws Throwable
27      */
28     public function pdf(string $bookSlug, string $chapterSlug)
29     {
30         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
31         $pdfContent = $this->exportFormatter->chapterToPdf($chapter);
32         return $this->downloadResponse($pdfContent, $chapterSlug . '.pdf');
33     }
34
35     /**
36      * Export a chapter to a self-contained HTML file.
37      * @throws NotFoundException
38      * @throws Throwable
39      */
40     public function html(string $bookSlug, string $chapterSlug)
41     {
42         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
43         $containedHtml = $this->exportFormatter->chapterToContainedHtml($chapter);
44         return $this->downloadResponse($containedHtml, $chapterSlug . '.html');
45     }
46
47     /**
48      * Export a chapter to a simple plaintext .txt file.
49      * @throws NotFoundException
50      */
51     public function plainText(string $bookSlug, string $chapterSlug)
52     {
53         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
54         $chapterText = $this->exportFormatter->chapterToPlainText($chapter);
55         return $this->downloadResponse($chapterText, $chapterSlug . '.txt');
56     }
57 }