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