3 namespace BookStack\Entities\Controllers;
5 use BookStack\Entities\Repos\PageRepo;
6 use BookStack\Entities\Tools\ExportFormatter;
7 use BookStack\Entities\Tools\PageContent;
8 use BookStack\Exceptions\NotFoundException;
9 use BookStack\Http\Controllers\Controller;
12 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;
24 $this->middleware('can:content-export');
28 * Exports a page to a PDF.
29 * https://github.com/barryvdh/laravel-dompdf.
31 * @throws NotFoundException
34 public function pdf(string $bookSlug, string $pageSlug)
36 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
37 $page->html = (new PageContent($page))->render();
38 $pdfContent = $this->exportFormatter->pageToPdf($page);
40 return $this->download()->directly($pdfContent, $pageSlug . '.pdf');
44 * Export a page to a self-contained HTML file.
46 * @throws NotFoundException
49 public function html(string $bookSlug, string $pageSlug)
51 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
52 $page->html = (new PageContent($page))->render();
53 $containedHtml = $this->exportFormatter->pageToContainedHtml($page);
55 return $this->download()->directly($containedHtml, $pageSlug . '.html');
59 * Export a page to a simple plaintext .txt file.
61 * @throws NotFoundException
63 public function plainText(string $bookSlug, string $pageSlug)
65 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
66 $pageText = $this->exportFormatter->pageToPlainText($page);
68 return $this->download()->directly($pageText, $pageSlug . '.txt');
72 * Export a page to a simple markdown .md file.
74 * @throws NotFoundException
76 public function markdown(string $bookSlug, string $pageSlug)
78 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
79 $pageText = $this->exportFormatter->pageToMarkdown($page);
81 return $this->download()->directly($pageText, $pageSlug . '.md');