3 namespace BookStack\Http\Controllers;
5 use BookStack\Entities\Tools\ExportFormatter;
6 use BookStack\Entities\Tools\PageContent;
7 use BookStack\Entities\Repos\PageRepo;
8 use BookStack\Exceptions\NotFoundException;
11 class PageExportController extends Controller
15 protected $exportFormatter;
18 * PageExportController constructor.
20 public function __construct(PageRepo $pageRepo, ExportFormatter $exportFormatter)
22 $this->pageRepo = $pageRepo;
23 $this->exportFormatter = $exportFormatter;
27 * Exports a page to a PDF.
28 * 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);
37 return $this->downloadResponse($pdfContent, $pageSlug . '.pdf');
41 * Export a page to a self-contained HTML file.
42 * @throws NotFoundException
45 public function html(string $bookSlug, string $pageSlug)
47 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
48 $page->html = (new PageContent($page))->render();
49 $containedHtml = $this->exportFormatter->pageToContainedHtml($page);
50 return $this->downloadResponse($containedHtml, $pageSlug . '.html');
54 * Export a page to a simple plaintext .txt file.
55 * @throws NotFoundException
57 public function plainText(string $bookSlug, string $pageSlug)
59 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
60 $pageText = $this->exportFormatter->pageToPlainText($page);
61 return $this->downloadResponse($pageText, $pageSlug . '.txt');