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;
12 class ChapterExportController extends Controller
14 public function __construct(
15 protected ChapterQueries $queries,
16 protected ExportFormatter $exportFormatter,
18 $this->middleware('can:content-export');
19 $this->middleware('throttle:exports');
23 * Exports a chapter to pdf.
25 * @throws NotFoundException
28 public function pdf(string $bookSlug, string $chapterSlug)
30 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
31 $pdfContent = $this->exportFormatter->chapterToPdf($chapter);
33 return $this->download()->directly($pdfContent, $chapterSlug . '.pdf');
37 * Export a chapter to a self-contained HTML file.
39 * @throws NotFoundException
42 public function html(string $bookSlug, string $chapterSlug)
44 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
45 $containedHtml = $this->exportFormatter->chapterToContainedHtml($chapter);
47 return $this->download()->directly($containedHtml, $chapterSlug . '.html');
51 * Export a chapter to a simple plaintext .txt file.
53 * @throws NotFoundException
55 public function plainText(string $bookSlug, string $chapterSlug)
57 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
58 $chapterText = $this->exportFormatter->chapterToPlainText($chapter);
60 return $this->download()->directly($chapterText, $chapterSlug . '.txt');
64 * Export a chapter to a simple markdown file.
66 * @throws NotFoundException
68 public function markdown(string $bookSlug, string $chapterSlug)
70 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
71 $chapterText = $this->exportFormatter->chapterToMarkdown($chapter);
73 return $this->download()->directly($chapterText, $chapterSlug . '.md');
77 * Export a book to a contained ZIP export file.
78 * @throws NotFoundException
80 public function zip(string $bookSlug, string $chapterSlug, ZipExportBuilder $builder)
82 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
83 $zip = $builder->buildForChapter($chapter);
85 return $this->download()->streamedFileDirectly($zip, $chapterSlug . '.zip', filesize($zip), true);