]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/PageExportController.php
Merge branch 'markdown-export' of https://github.com/nikhiljha/BookStack-1 into nikhi...
[bookstack] / app / Http / Controllers / PageExportController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Entities\Tools\ExportFormatter;
6 use BookStack\Entities\Tools\PageContent;
7 use BookStack\Entities\Repos\PageRepo;
8 use BookStack\Exceptions\NotFoundException;
9 use Throwable;
10
11 class PageExportController extends Controller
12 {
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     }
25
26     /**
27      * Exports a page to a PDF.
28      * https://github.com/barryvdh/laravel-dompdf
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         return $this->downloadResponse($pdfContent, $pageSlug . '.pdf');
38     }
39
40     /**
41      * Export a page to a self-contained HTML file.
42      * @throws NotFoundException
43      * @throws Throwable
44      */
45     public function html(string $bookSlug, string $pageSlug)
46     {
47         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
48         $page->html = (new PageContent($page))->render();
49         $containedHtml = $this->exportFormatter->pageToContainedHtml($page);
50         return $this->downloadResponse($containedHtml, $pageSlug . '.html');
51     }
52
53     /**
54      * Export a page to a simple plaintext .txt file.
55      * @throws NotFoundException
56      */
57     public function plainText(string $bookSlug, string $pageSlug)
58     {
59         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
60         $pageText = $this->exportFormatter->pageToPlainText($page);
61         return $this->downloadResponse($pageText, $pageSlug . '.txt');
62     }
63
64     /**
65      * Export a page to a simple markdown .md file.
66      * @throws NotFoundException
67      */
68     public function markdown(string $bookSlug, string $pageSlug)
69     {
70         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
71         $pageText = $this->exportService->pageToMarkdown($page);
72         return $this->downloadResponse($pageText, $pageSlug . '.md');
73     }
74 }