]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/PageExportController.php
move zip export into exportservice
[bookstack] / app / Http / Controllers / PageExportController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Entities\ExportService;
6 use BookStack\Entities\Managers\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 $exportService;
16
17     /**
18      * PageExportController constructor.
19      * @param PageRepo $pageRepo
20      * @param ExportService $exportService
21      */
22     public function __construct(PageRepo $pageRepo, ExportService $exportService)
23     {
24         $this->pageRepo = $pageRepo;
25         $this->exportService = $exportService;
26         parent::__construct();
27     }
28
29     /**
30      * Exports a page to a PDF.
31      * https://github.com/barryvdh/laravel-dompdf
32      * @throws NotFoundException
33      * @throws Throwable
34      */
35     public function pdf(string $bookSlug, string $pageSlug)
36     {
37         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
38         $page->html = (new PageContent($page))->render();
39         $pdfContent = $this->exportService->pageToPdf($page);
40         return $this->downloadResponse($pdfContent, $pageSlug . '.pdf');
41     }
42
43     /**
44      * Export a page to a self-contained HTML file.
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->exportService->pageToContainedHtml($page);
53         return $this->downloadResponse($containedHtml, $pageSlug . '.html');
54     }
55
56     /**
57      * Export a page to a simple plaintext .txt file.
58      * @throws NotFoundException
59      */
60     public function plainText(string $bookSlug, string $pageSlug)
61     {
62         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
63         $pageText = $this->exportService->pageToPlainText($page);
64         return $this->downloadResponse($pageText, $pageSlug . '.txt');
65     }
66
67     /**
68      * Export a page to a simple markdown .md file.
69      * @throws NotFoundException
70      */
71     public function markdown(string $bookSlug, string $pageSlug)
72     {
73         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
74         $pageText = $this->exportService->pageToMarkdown($page);
75         return $this->downloadResponse($pageText, $pageSlug . '.md');
76     }
77 }