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