3 namespace BookStack\Exports\Controllers;
5 use BookStack\Entities\Queries\ChapterQueries;
6 use BookStack\Exceptions\NotFoundException;
7 use BookStack\Exports\ExportFormatter;
8 use BookStack\Exports\ZipExports\ZipExportBuilder;
9 use BookStack\Http\Controller;
10 use BookStack\Permissions\Permission;
13 class ChapterExportController extends Controller
15 public function __construct(
16 protected ChapterQueries $queries,
17 protected ExportFormatter $exportFormatter,
19 $this->middleware(Permission::ContentExport->middleware());
20 $this->middleware('throttle:exports');
24 * Exports a chapter to pdf.
26 * @throws NotFoundException
29 public function pdf(string $bookSlug, string $chapterSlug)
31 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
32 $pdfContent = $this->exportFormatter->chapterToPdf($chapter);
34 return $this->download()->directly($pdfContent, $chapterSlug . '.pdf');
38 * Export a chapter to a self-contained HTML file.
40 * @throws NotFoundException
43 public function html(string $bookSlug, string $chapterSlug)
45 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
46 $containedHtml = $this->exportFormatter->chapterToContainedHtml($chapter);
48 return $this->download()->directly($containedHtml, $chapterSlug . '.html');
52 * Export a chapter to a simple plaintext .txt file.
54 * @throws NotFoundException
56 public function plainText(string $bookSlug, string $chapterSlug)
58 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
59 $chapterText = $this->exportFormatter->chapterToPlainText($chapter);
61 return $this->download()->directly($chapterText, $chapterSlug . '.txt');
65 * Export a chapter to a simple markdown file.
67 * @throws NotFoundException
69 public function markdown(string $bookSlug, string $chapterSlug)
71 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
72 $chapterText = $this->exportFormatter->chapterToMarkdown($chapter);
74 return $this->download()->directly($chapterText, $chapterSlug . '.md');
78 * Export a book to a contained ZIP export file.
79 * @throws NotFoundException
81 public function zip(string $bookSlug, string $chapterSlug, ZipExportBuilder $builder)
83 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
84 $zip = $builder->buildForChapter($chapter);
86 return $this->download()->streamedFileDirectly($zip, $chapterSlug . '.zip', true);