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;
26 * Exports a page to a PDF.
27 * https://github.com/barryvdh/laravel-dompdf.
29 * @throws NotFoundException
32 public function pdf(string $bookSlug, string $pageSlug)
34 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
35 $page->html = (new PageContent($page))->render();
36 $pdfContent = $this->exportFormatter->pageToPdf($page);
38 return $this->downloadResponse($pdfContent, $pageSlug . '.pdf');
42 * Export a page to a self-contained HTML file.
44 * @throws NotFoundException
47 public function html(string $bookSlug, string $pageSlug)
49 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
50 $page->html = (new PageContent($page))->render();
51 $containedHtml = $this->exportFormatter->pageToContainedHtml($page);
53 return $this->downloadResponse($containedHtml, $pageSlug . '.html');
57 * Export a page to a simple plaintext .txt file.
59 * @throws NotFoundException
61 public function plainText(string $bookSlug, string $pageSlug)
63 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
64 $pageText = $this->exportFormatter->pageToPlainText($page);
66 return $this->downloadResponse($pageText, $pageSlug . '.txt');
70 * Export a page to a simple markdown .md file.
72 * @throws NotFoundException
74 public function markdown(string $bookSlug, string $pageSlug)
76 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
77 $pageText = $this->exportFormatter->pageToMarkdown($page);
79 return $this->downloadResponse($pageText, $pageSlug . '.md');