3 namespace BookStack\Http\Controllers;
5 use BookStack\Entities\Repos\PageRepo;
6 use BookStack\Entities\Tools\ExportFormatter;
7 use BookStack\Entities\Tools\PageContent;
8 use BookStack\Exceptions\NotFoundException;
11 class PageExportController extends Controller
14 protected $exportFormatter;
17 * PageExportController constructor.
19 public function __construct(PageRepo $pageRepo, ExportFormatter $exportFormatter)
21 $this->pageRepo = $pageRepo;
22 $this->exportFormatter = $exportFormatter;
23 $this->middleware('can:content-export');
27 * Exports a page to a PDF.
28 * https://github.com/barryvdh/laravel-dompdf.
30 * @throws NotFoundException
33 public function pdf(string $bookSlug, string $pageSlug)
35 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
36 $page->html = (new PageContent($page))->render();
37 $pdfContent = $this->exportFormatter->pageToPdf($page);
39 return $this->download()->directly($pdfContent, $pageSlug . '.pdf');
43 * Export a page to a self-contained HTML file.
45 * @throws NotFoundException
48 public function html(string $bookSlug, string $pageSlug)
50 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
51 $page->html = (new PageContent($page))->render();
52 $containedHtml = $this->exportFormatter->pageToContainedHtml($page);
54 return $this->download()->directly($containedHtml, $pageSlug . '.html');
58 * Export a page to a simple plaintext .txt file.
60 * @throws NotFoundException
62 public function plainText(string $bookSlug, string $pageSlug)
64 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
65 $pageText = $this->exportFormatter->pageToPlainText($page);
67 return $this->download()->directly($pageText, $pageSlug . '.txt');
71 * Export a page to a simple markdown .md file.
73 * @throws NotFoundException
75 public function markdown(string $bookSlug, string $pageSlug)
77 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
78 $pageText = $this->exportFormatter->pageToMarkdown($page);
80 return $this->download()->directly($pageText, $pageSlug . '.md');