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;
13 class PageExportController extends Controller
15 public function __construct(
16 protected PageQueries $queries,
17 protected ExportFormatter $exportFormatter,
19 $this->middleware('can:content-export');
20 $this->middleware('throttle:exports');
24 * Exports a page to a PDF.
25 * https://github.com/barryvdh/laravel-dompdf.
27 * @throws NotFoundException
30 public function pdf(string $bookSlug, string $pageSlug)
32 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
33 $page->html = (new PageContent($page))->render();
34 $pdfContent = $this->exportFormatter->pageToPdf($page);
36 return $this->download()->directly($pdfContent, $pageSlug . '.pdf');
40 * Export a page to a self-contained HTML file.
42 * @throws NotFoundException
45 public function html(string $bookSlug, string $pageSlug)
47 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
48 $page->html = (new PageContent($page))->render();
49 $containedHtml = $this->exportFormatter->pageToContainedHtml($page);
51 return $this->download()->directly($containedHtml, $pageSlug . '.html');
55 * Export a page to a simple plaintext .txt file.
57 * @throws NotFoundException
59 public function plainText(string $bookSlug, string $pageSlug)
61 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
62 $pageText = $this->exportFormatter->pageToPlainText($page);
64 return $this->download()->directly($pageText, $pageSlug . '.txt');
68 * Export a page to a simple markdown .md file.
70 * @throws NotFoundException
72 public function markdown(string $bookSlug, string $pageSlug)
74 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
75 $pageText = $this->exportFormatter->pageToMarkdown($page);
77 return $this->download()->directly($pageText, $pageSlug . '.md');
81 * Export a page to a contained ZIP export file.
82 * @throws NotFoundException
84 public function zip(string $bookSlug, string $pageSlug, ZipExportBuilder $builder)
86 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
87 $zip = $builder->buildForPage($page);
89 return $this->download()->streamedFileDirectly($zip, $pageSlug . '.zip', filesize($zip), true);