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