]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/PageExportController.php
Played around with a new app structure
[bookstack] / app / Entities / Controllers / PageExportController.php
1 <?php
2
3 namespace BookStack\Entities\Controllers;
4
5 use BookStack\Entities\Repos\PageRepo;
6 use BookStack\Entities\Tools\ExportFormatter;
7 use BookStack\Entities\Tools\PageContent;
8 use BookStack\Exceptions\NotFoundException;
9 use BookStack\Http\Controllers\Controller;
10 use Throwable;
11
12 class PageExportController extends Controller
13 {
14     protected $pageRepo;
15     protected $exportFormatter;
16
17     /**
18      * PageExportController constructor.
19      */
20     public function __construct(PageRepo $pageRepo, ExportFormatter $exportFormatter)
21     {
22         $this->pageRepo = $pageRepo;
23         $this->exportFormatter = $exportFormatter;
24         $this->middleware('can:content-export');
25     }
26
27     /**
28      * Exports a page to a PDF.
29      * https://github.com/barryvdh/laravel-dompdf.
30      *
31      * @throws NotFoundException
32      * @throws Throwable
33      */
34     public function pdf(string $bookSlug, string $pageSlug)
35     {
36         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
37         $page->html = (new PageContent($page))->render();
38         $pdfContent = $this->exportFormatter->pageToPdf($page);
39
40         return $this->download()->directly($pdfContent, $pageSlug . '.pdf');
41     }
42
43     /**
44      * Export a page to a self-contained HTML file.
45      *
46      * @throws NotFoundException
47      * @throws Throwable
48      */
49     public function html(string $bookSlug, string $pageSlug)
50     {
51         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
52         $page->html = (new PageContent($page))->render();
53         $containedHtml = $this->exportFormatter->pageToContainedHtml($page);
54
55         return $this->download()->directly($containedHtml, $pageSlug . '.html');
56     }
57
58     /**
59      * Export a page to a simple plaintext .txt file.
60      *
61      * @throws NotFoundException
62      */
63     public function plainText(string $bookSlug, string $pageSlug)
64     {
65         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
66         $pageText = $this->exportFormatter->pageToPlainText($page);
67
68         return $this->download()->directly($pageText, $pageSlug . '.txt');
69     }
70
71     /**
72      * Export a page to a simple markdown .md file.
73      *
74      * @throws NotFoundException
75      */
76     public function markdown(string $bookSlug, string $pageSlug)
77     {
78         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
79         $pageText = $this->exportFormatter->pageToMarkdown($page);
80
81         return $this->download()->directly($pageText, $pageSlug . '.md');
82     }
83 }