]> BookStack Code Mirror - bookstack/blob - app/Exports/Controllers/PageExportController.php
Merge pull request #5917 from BookStackApp/copy_references
[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\Exports\ZipExports\ZipExportBuilder;
10 use BookStack\Http\Controller;
11 use BookStack\Permissions\Permission;
12 use Throwable;
13
14 class PageExportController extends Controller
15 {
16     public function __construct(
17         protected PageQueries $queries,
18         protected ExportFormatter $exportFormatter,
19     ) {
20         $this->middleware(Permission::ContentExport->middleware());
21         $this->middleware('throttle:exports');
22     }
23
24     /**
25      * Exports a page to a PDF.
26      * https://github.com/barryvdh/laravel-dompdf.
27      *
28      * @throws NotFoundException
29      * @throws Throwable
30      */
31     public function pdf(string $bookSlug, string $pageSlug)
32     {
33         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
34         $page->html = (new PageContent($page))->render();
35         $pdfContent = $this->exportFormatter->pageToPdf($page);
36
37         return $this->download()->directly($pdfContent, $pageSlug . '.pdf');
38     }
39
40     /**
41      * Export a page to a self-contained HTML file.
42      *
43      * @throws NotFoundException
44      * @throws Throwable
45      */
46     public function html(string $bookSlug, string $pageSlug)
47     {
48         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
49         $page->html = (new PageContent($page))->render();
50         $containedHtml = $this->exportFormatter->pageToContainedHtml($page);
51
52         return $this->download()->directly($containedHtml, $pageSlug . '.html');
53     }
54
55     /**
56      * Export a page to a simple plaintext .txt file.
57      *
58      * @throws NotFoundException
59      */
60     public function plainText(string $bookSlug, string $pageSlug)
61     {
62         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
63         $pageText = $this->exportFormatter->pageToPlainText($page);
64
65         return $this->download()->directly($pageText, $pageSlug . '.txt');
66     }
67
68     /**
69      * Export a page to a simple markdown .md file.
70      *
71      * @throws NotFoundException
72      */
73     public function markdown(string $bookSlug, string $pageSlug)
74     {
75         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
76         $pageText = $this->exportFormatter->pageToMarkdown($page);
77
78         return $this->download()->directly($pageText, $pageSlug . '.md');
79     }
80
81     /**
82      * Export a page to a contained ZIP export file.
83      * @throws NotFoundException
84      */
85     public function zip(string $bookSlug, string $pageSlug, ZipExportBuilder $builder)
86     {
87         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
88         $zip = $builder->buildForPage($page);
89
90         return $this->download()->streamedFileDirectly($zip, $pageSlug . '.zip', true);
91     }
92 }