]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/BookExportApiController.php
Apply fixes from StyleCI
[bookstack] / app / Http / Controllers / Api / BookExportApiController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Api;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Tools\ExportFormatter;
7 use Throwable;
8
9 class BookExportApiController extends ApiController
10 {
11     protected $exportFormatter;
12
13     public function __construct(ExportFormatter $exportFormatter)
14     {
15         $this->exportFormatter = $exportFormatter;
16     }
17
18     /**
19      * Export a book as a PDF file.
20      *
21      * @throws Throwable
22      */
23     public function exportPdf(int $id)
24     {
25         $book = Book::visible()->findOrFail($id);
26         $pdfContent = $this->exportFormatter->bookToPdf($book);
27
28         return $this->downloadResponse($pdfContent, $book->slug . '.pdf');
29     }
30
31     /**
32      * Export a book as a contained HTML file.
33      *
34      * @throws Throwable
35      */
36     public function exportHtml(int $id)
37     {
38         $book = Book::visible()->findOrFail($id);
39         $htmlContent = $this->exportFormatter->bookToContainedHtml($book);
40
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
52         return $this->downloadResponse($textContent, $book->slug . '.txt');
53     }
54
55     /**
56      * Export a book as a markdown file.
57      */
58     public function exportMarkdown(int $id)
59     {
60         $book = Book::visible()->findOrFail($id);
61         $markdown = $this->exportFormatter->bookToMarkdown($book);
62
63         return $this->downloadResponse($markdown, $book->slug . '.md');
64     }
65 }