3 namespace BookStack\Exports\Controllers;
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;
12 class PageExportController extends Controller
14 public function __construct(
15 protected PageQueries $queries,
16 protected ExportFormatter $exportFormatter,
18 $this->middleware('can:content-export');
22 * Exports a page to a PDF.
23 * https://github.com/barryvdh/laravel-dompdf.
25 * @throws NotFoundException
28 public function pdf(string $bookSlug, string $pageSlug)
30 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
31 $page->html = (new PageContent($page))->render();
32 $pdfContent = $this->exportFormatter->pageToPdf($page);
34 return $this->download()->directly($pdfContent, $pageSlug . '.pdf');
38 * Export a page to a self-contained HTML file.
40 * @throws NotFoundException
43 public function html(string $bookSlug, string $pageSlug)
45 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
46 $page->html = (new PageContent($page))->render();
47 $containedHtml = $this->exportFormatter->pageToContainedHtml($page);
49 return $this->download()->directly($containedHtml, $pageSlug . '.html');
53 * Export a page to a simple plaintext .txt file.
55 * @throws NotFoundException
57 public function plainText(string $bookSlug, string $pageSlug)
59 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
60 $pageText = $this->exportFormatter->pageToPlainText($page);
62 return $this->download()->directly($pageText, $pageSlug . '.txt');
66 * Export a page to a simple markdown .md file.
68 * @throws NotFoundException
70 public function markdown(string $bookSlug, string $pageSlug)
72 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
73 $pageText = $this->exportFormatter->pageToMarkdown($page);
75 return $this->download()->directly($pageText, $pageSlug . '.md');