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