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');
23 * Exports a page to a PDF.
24 * https://github.com/barryvdh/laravel-dompdf.
26 * @throws NotFoundException
29 public function pdf(string $bookSlug, string $pageSlug)
31 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
32 $page->html = (new PageContent($page))->render();
33 $pdfContent = $this->exportFormatter->pageToPdf($page);
35 return $this->download()->directly($pdfContent, $pageSlug . '.pdf');
39 * Export a page to a self-contained HTML file.
41 * @throws NotFoundException
44 public function html(string $bookSlug, string $pageSlug)
46 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
47 $page->html = (new PageContent($page))->render();
48 $containedHtml = $this->exportFormatter->pageToContainedHtml($page);
50 return $this->download()->directly($containedHtml, $pageSlug . '.html');
54 * Export a page to a simple plaintext .txt file.
56 * @throws NotFoundException
58 public function plainText(string $bookSlug, string $pageSlug)
60 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
61 $pageText = $this->exportFormatter->pageToPlainText($page);
63 return $this->download()->directly($pageText, $pageSlug . '.txt');
67 * Export a page to a simple markdown .md file.
69 * @throws NotFoundException
71 public function markdown(string $bookSlug, string $pageSlug)
73 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
74 $pageText = $this->exportFormatter->pageToMarkdown($page);
76 return $this->download()->directly($pageText, $pageSlug . '.md');
80 * Export a page to a contained ZIP export file.
81 * @throws NotFoundException
83 public function zip(string $bookSlug, string $pageSlug, ZipExportBuilder $builder)
85 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
86 $zip = $builder->buildForPage($page);
88 return $this->download()->streamedDirectly(fopen($zip, 'r'), $pageSlug . '.zip', filesize($zip));