]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookExportController.php
Split out export actions into own controllers
[bookstack] / app / Http / Controllers / BookExportController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Entities\ExportService;
6 use BookStack\Entities\Repos\EntityRepo;
7 use BookStack\Exceptions\NotFoundException;
8 use Throwable;
9
10 class BookExportController extends Controller
11 {
12     /**
13      * @var EntityRepo
14      */
15     protected $entityRepo;
16
17     /**
18      * @var ExportService
19      */
20     protected $exportService;
21
22     /**
23      * BookExportController constructor.
24      * @param EntityRepo $entityRepo
25      * @param ExportService $exportService
26      */
27     public function __construct(EntityRepo $entityRepo, ExportService $exportService)
28     {
29         $this->entityRepo = $entityRepo;
30         $this->exportService = $exportService;
31         parent::__construct();
32     }
33
34     /**
35      * Export a book as a PDF file.
36      * @param string $bookSlug
37      * @return mixed
38      * @throws NotFoundException
39      * @throws Throwable
40      */
41     public function pdf(string $bookSlug)
42     {
43         $book = $this->entityRepo->getBySlug('book', $bookSlug);
44         $pdfContent = $this->exportService->bookToPdf($book);
45         return $this->downloadResponse($pdfContent, $bookSlug . '.pdf');
46     }
47
48     /**
49      * Export a book as a contained HTML file.
50      * @param string $bookSlug
51      * @return mixed
52      * @throws NotFoundException
53      * @throws Throwable
54      */
55     public function html(string $bookSlug)
56     {
57         $book = $this->entityRepo->getBySlug('book', $bookSlug);
58         $htmlContent = $this->exportService->bookToContainedHtml($book);
59         return $this->downloadResponse($htmlContent, $bookSlug . '.html');
60     }
61
62     /**
63      * Export a book as a plain text file.
64      * @param $bookSlug
65      * @return mixed
66      * @throws NotFoundException
67      */
68     public function plainText(string $bookSlug)
69     {
70         $book = $this->entityRepo->getBySlug('book', $bookSlug);
71         $textContent = $this->exportService->bookToPlainText($book);
72         return $this->downloadResponse($textContent, $bookSlug . '.txt');
73     }
74 }