]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/BookExportApiController.php
Merge pull request #2382 from BookStackApp/pages_api
[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 Throwable;
6
7 class BookExportApiController extends ApiController
8 {
9     protected $exportFormatter;
10
11     public function __construct(ExportFormatter $exportFormatter)
12     {
13         $this->exportFormatter = $exportFormatter;
14     }
15
16     /**
17      * Export a book as a PDF file.
18      * @throws Throwable
19      */
20     public function exportPdf(int $id)
21     {
22         $book = Book::visible()->findOrFail($id);
23         $pdfContent = $this->exportFormatter->bookToPdf($book);
24         return $this->downloadResponse($pdfContent, $book->slug . '.pdf');
25     }
26
27     /**
28      * Export a book as a contained HTML file.
29      * @throws Throwable
30      */
31     public function exportHtml(int $id)
32     {
33         $book = Book::visible()->findOrFail($id);
34         $htmlContent = $this->exportFormatter->bookToContainedHtml($book);
35         return $this->downloadResponse($htmlContent, $book->slug . '.html');
36     }
37
38     /**
39      * Export a book as a plain text file.
40      */
41     public function exportPlainText(int $id)
42     {
43         $book = Book::visible()->findOrFail($id);
44         $textContent = $this->exportFormatter->bookToPlainText($book);
45         return $this->downloadResponse($textContent, $book->slug . '.txt');
46     }
47 }