]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/BooksExportApiController.php
move zip export into exportservice
[bookstack] / app / Http / Controllers / Api / BooksExportApiController.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 BooksExportApiController extends ApiController
9 {
10
11     protected $bookRepo;
12     protected $exportService;
13
14     /**
15      * BookExportController constructor.
16      */
17     public function __construct(BookRepo $bookRepo, ExportService $exportService)
18     {
19         $this->bookRepo = $bookRepo;
20         $this->exportService = $exportService;
21         parent::__construct();
22     }
23
24     /**
25      * Export a book as a PDF file.
26      * @throws Throwable
27      */
28     public function exportPdf(int $id)
29     {
30         $book = Book::visible()->findOrFail($id);
31         $pdfContent = $this->exportService->bookToPdf($book);
32         return $this->downloadResponse($pdfContent, $book->slug . '.pdf');
33     }
34
35     /**
36      * Export a book as a contained HTML file.
37      * @throws Throwable
38      */
39     public function exportHtml(int $id)
40     {
41         $book = Book::visible()->findOrFail($id);
42         $htmlContent = $this->exportService->bookToContainedHtml($book);
43         return $this->downloadResponse($htmlContent, $book->slug . '.html');
44     }
45
46     /**
47      * Export a book as a plain text file.
48      */
49     public function exportPlainText(int $id)
50     {
51         $book = Book::visible()->findOrFail($id);
52         $textContent = $this->exportService->bookToPlainText($book);
53         return $this->downloadResponse($textContent, $book->slug . '.txt');
54     }
55 }