]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/PageExportController.php
Added force option for update-url command
[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         $this->middleware('can:content-export');
24     }
25
26     /**
27      * Exports a page to a PDF.
28      * https://github.com/barryvdh/laravel-dompdf.
29      *
30      * @throws NotFoundException
31      * @throws Throwable
32      */
33     public function pdf(string $bookSlug, string $pageSlug)
34     {
35         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
36         $page->html = (new PageContent($page))->render();
37         $pdfContent = $this->exportFormatter->pageToPdf($page);
38
39         return $this->download()->directly($pdfContent, $pageSlug . '.pdf');
40     }
41
42     /**
43      * Export a page to a self-contained HTML file.
44      *
45      * @throws NotFoundException
46      * @throws Throwable
47      */
48     public function html(string $bookSlug, string $pageSlug)
49     {
50         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
51         $page->html = (new PageContent($page))->render();
52         $containedHtml = $this->exportFormatter->pageToContainedHtml($page);
53
54         return $this->download()->directly($containedHtml, $pageSlug . '.html');
55     }
56
57     /**
58      * Export a page to a simple plaintext .txt file.
59      *
60      * @throws NotFoundException
61      */
62     public function plainText(string $bookSlug, string $pageSlug)
63     {
64         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
65         $pageText = $this->exportFormatter->pageToPlainText($page);
66
67         return $this->download()->directly($pageText, $pageSlug . '.txt');
68     }
69
70     /**
71      * Export a page to a simple markdown .md file.
72      *
73      * @throws NotFoundException
74      */
75     public function markdown(string $bookSlug, string $pageSlug)
76     {
77         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
78         $pageText = $this->exportFormatter->pageToMarkdown($page);
79
80         return $this->download()->directly($pageText, $pageSlug . '.md');
81     }
82 }