3 namespace BookStack\Http\Controllers;
5 use BookStack\Entities\ExportService;
6 use BookStack\Entities\Repos\PageRepo;
7 use BookStack\Exceptions\NotFoundException;
8 use Illuminate\Http\Response;
11 class PageExportController extends Controller
21 protected $exportService;
24 * PageExportController constructor.
25 * @param PageRepo $pageRepo
26 * @param ExportService $exportService
28 public function __construct(PageRepo $pageRepo, ExportService $exportService)
30 $this->pageRepo = $pageRepo;
31 $this->exportService = $exportService;
32 parent::__construct();
36 * Exports a page to a PDF.
37 * https://github.com/barryvdh/laravel-dompdf
38 * @param string $bookSlug
39 * @param string $pageSlug
41 * @throws NotFoundException
44 public function pdf(string $bookSlug, string $pageSlug)
46 $page = $this->pageRepo->getBySlug($pageSlug, $bookSlug);
47 $page->html = $this->pageRepo->renderPage($page);
48 $pdfContent = $this->exportService->pageToPdf($page);
49 return $this->downloadResponse($pdfContent, $pageSlug . '.pdf');
53 * Export a page to a self-contained HTML file.
54 * @param string $bookSlug
55 * @param string $pageSlug
57 * @throws NotFoundException
60 public function html(string $bookSlug, string $pageSlug)
62 $page = $this->pageRepo->getBySlug($pageSlug, $bookSlug);
63 $page->html = $this->pageRepo->renderPage($page);
64 $containedHtml = $this->exportService->pageToContainedHtml($page);
65 return $this->downloadResponse($containedHtml, $pageSlug . '.html');
69 * Export a page to a simple plaintext .txt file.
70 * @param string $bookSlug
71 * @param string $pageSlug
73 * @throws NotFoundException
75 public function plainText(string $bookSlug, string $pageSlug)
77 $page = $this->pageRepo->getBySlug($pageSlug, $bookSlug);
78 $pageText = $this->exportService->pageToPlainText($page);
79 return $this->downloadResponse($pageText, $pageSlug . '.txt');