]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/BookExportApiController.php
Merge pull request #1 from BookStackApp/master
[bookstack] / app / Http / Controllers / Api / BookExportApiController.php
1 <?php namespace BookStack\Http\Controllers\Api;
2
3 use BookStack\Entities\Book;
4 use BookStack\Entities\ExportService;
5 use BookStack\Entities\Repos\BookRepo;
6 use Throwable;
7
8 class BookExportApiController extends ApiController
9 {
10     protected $bookRepo;
11     protected $exportService;
12
13     /**
14      * BookExportController constructor.
15      */
16     public function __construct(BookRepo $bookRepo, ExportService $exportService)
17     {
18         $this->bookRepo = $bookRepo;
19         $this->exportService = $exportService;
20         parent::__construct();
21     }
22
23     /**
24      * Export a book as a PDF file.
25      * @throws Throwable
26      */
27     public function exportPdf(int $id)
28     {
29         $book = Book::visible()->findOrFail($id);
30         $pdfContent = $this->exportService->bookToPdf($book);
31         return $this->downloadResponse($pdfContent, $book->slug . '.pdf');
32     }
33
34     /**
35      * Export a book as a contained HTML file.
36      * @throws Throwable
37      */
38     public function exportHtml(int $id)
39     {
40         $book = Book::visible()->findOrFail($id);
41         $htmlContent = $this->exportService->bookToContainedHtml($book);
42         return $this->downloadResponse($htmlContent, $book->slug . '.html');
43     }
44
45     /**
46      * Export a book as a plain text file.
47      */
48     public function exportPlainText(int $id)
49     {
50         $book = Book::visible()->findOrFail($id);
51         $textContent = $this->exportService->bookToPlainText($book);
52         return $this->downloadResponse($textContent, $book->slug . '.txt');
53     }
54 }