]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/PageExportController.php
addcc55132c218078ab903bc85777bf8805ba330
[bookstack] / app / Http / Controllers / PageExportController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Entities\ExportService;
6 use BookStack\Entities\Repos\PageRepo;
7 use BookStack\Exceptions\NotFoundException;
8 use Illuminate\Http\Response;
9 use Throwable;
10
11 class PageExportController extends Controller
12 {
13     /**
14      * @var PageRepo
15      */
16     protected $pageRepo;
17
18     /**
19      * @var ExportService
20      */
21     protected $exportService;
22
23     /**
24      * PageExportController constructor.
25      * @param PageRepo $pageRepo
26      * @param ExportService $exportService
27      */
28     public function __construct(PageRepo $pageRepo, ExportService $exportService)
29     {
30         $this->pageRepo = $pageRepo;
31         $this->exportService = $exportService;
32         parent::__construct();
33     }
34
35     /**
36      * Exports a page to a PDF.
37      * https://github.com/barryvdh/laravel-dompdf
38      * @param string $bookSlug
39      * @param string $pageSlug
40      * @return Response
41      * @throws NotFoundException
42      * @throws Throwable
43      */
44     public function pdf(string $bookSlug, string $pageSlug)
45     {
46         $page = $this->pageRepo->getBySlug($pageSlug, $bookSlug);
47         $page->html = $this->pageRepo->renderPage($page);
48         $pdfContent = $this->exportService->pageToPdf($page);
49         return $this->downloadResponse($pdfContent, $pageSlug . '.pdf');
50     }
51
52     /**
53      * Export a page to a self-contained HTML file.
54      * @param string $bookSlug
55      * @param string $pageSlug
56      * @return Response
57      * @throws NotFoundException
58      * @throws Throwable
59      */
60     public function html(string $bookSlug, string $pageSlug)
61     {
62         $page = $this->pageRepo->getBySlug($pageSlug, $bookSlug);
63         $page->html = $this->pageRepo->renderPage($page);
64         $containedHtml = $this->exportService->pageToContainedHtml($page);
65         return $this->downloadResponse($containedHtml, $pageSlug . '.html');
66     }
67
68     /**
69      * Export a page to a simple plaintext .txt file.
70      * @param string $bookSlug
71      * @param string $pageSlug
72      * @return Response
73      * @throws NotFoundException
74      */
75     public function plainText(string $bookSlug, string $pageSlug)
76     {
77         $page = $this->pageRepo->getBySlug($pageSlug, $bookSlug);
78         $pageText = $this->exportService->pageToPlainText($page);
79         return $this->downloadResponse($pageText, $pageSlug . '.txt');
80     }
81 }