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\Exports\ZipExports\ZipExportBuilder;
10 use BookStack\Http\Controller;
11 use BookStack\Permissions\Permission;
14 class PageExportController extends Controller
16 public function __construct(
17 protected PageQueries $queries,
18 protected ExportFormatter $exportFormatter,
20 $this->middleware(Permission::ContentExport->middleware());
21 $this->middleware('throttle:exports');
25 * Exports a page to a PDF.
26 * https://github.com/barryvdh/laravel-dompdf.
28 * @throws NotFoundException
31 public function pdf(string $bookSlug, string $pageSlug)
33 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
34 $page->html = (new PageContent($page))->render();
35 $pdfContent = $this->exportFormatter->pageToPdf($page);
37 return $this->download()->directly($pdfContent, $pageSlug . '.pdf');
41 * Export a page to a self-contained HTML file.
43 * @throws NotFoundException
46 public function html(string $bookSlug, string $pageSlug)
48 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
49 $page->html = (new PageContent($page))->render();
50 $containedHtml = $this->exportFormatter->pageToContainedHtml($page);
52 return $this->download()->directly($containedHtml, $pageSlug . '.html');
56 * Export a page to a simple plaintext .txt file.
58 * @throws NotFoundException
60 public function plainText(string $bookSlug, string $pageSlug)
62 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
63 $pageText = $this->exportFormatter->pageToPlainText($page);
65 return $this->download()->directly($pageText, $pageSlug . '.txt');
69 * Export a page to a simple markdown .md file.
71 * @throws NotFoundException
73 public function markdown(string $bookSlug, string $pageSlug)
75 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
76 $pageText = $this->exportFormatter->pageToMarkdown($page);
78 return $this->download()->directly($pageText, $pageSlug . '.md');
82 * Export a page to a contained ZIP export file.
83 * @throws NotFoundException
85 public function zip(string $bookSlug, string $pageSlug, ZipExportBuilder $builder)
87 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
88 $zip = $builder->buildForPage($page);
90 return $this->download()->streamedFileDirectly($zip, $pageSlug . '.zip', true);