]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookExportController.php
6d334ca737d62a3079d0695ee79a2601f252d923
[bookstack] / app / Http / Controllers / BookExportController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Entities\Tools\ExportFormatter;
6 use BookStack\Entities\Repos\BookRepo;
7 use Throwable;
8
9 class BookExportController extends Controller
10 {
11
12     protected $bookRepo;
13     protected $exportFormatter;
14
15     /**
16      * BookExportController constructor.
17      */
18     public function __construct(BookRepo $bookRepo, ExportFormatter $exportFormatter)
19     {
20         $this->bookRepo = $bookRepo;
21         $this->exportFormatter = $exportFormatter;
22     }
23
24     /**
25      * Export a book as a PDF file.
26      * @throws Throwable
27      */
28     public function pdf(string $bookSlug)
29     {
30         $book = $this->bookRepo->getBySlug($bookSlug);
31         $pdfContent = $this->exportFormatter->bookToPdf($book);
32         return $this->downloadResponse($pdfContent, $bookSlug . '.pdf');
33     }
34
35     /**
36      * Export a book as a contained HTML file.
37      * @throws Throwable
38      */
39     public function html(string $bookSlug)
40     {
41         $book = $this->bookRepo->getBySlug($bookSlug);
42         $htmlContent = $this->exportFormatter->bookToContainedHtml($book);
43         return $this->downloadResponse($htmlContent, $bookSlug . '.html');
44     }
45
46     /**
47      * Export a book as a plain text file.
48      */
49     public function plainText(string $bookSlug)
50     {
51         $book = $this->bookRepo->getBySlug($bookSlug);
52         $textContent = $this->exportFormatter->bookToPlainText($book);
53         return $this->downloadResponse($textContent, $bookSlug . '.txt');
54     }
55
56     /**
57      * Export a book as a markdown file.
58      */
59     public function markdown(string $bookSlug)
60     {
61         $book = $this->bookRepo->getBySlug($bookSlug);
62         $textContent = $this->exportFormatter->bookToMarkdown($book);
63         return $this->downloadResponse($textContent, $bookSlug . '.md');
64     }
65 }