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');
22 * Exports a chapter to pdf.
24 * @throws NotFoundException
27 public function pdf(string $bookSlug, string $chapterSlug)
29 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
30 $pdfContent = $this->exportFormatter->chapterToPdf($chapter);
32 return $this->download()->directly($pdfContent, $chapterSlug . '.pdf');
36 * Export a chapter to a self-contained HTML file.
38 * @throws NotFoundException
41 public function html(string $bookSlug, string $chapterSlug)
43 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
44 $containedHtml = $this->exportFormatter->chapterToContainedHtml($chapter);
46 return $this->download()->directly($containedHtml, $chapterSlug . '.html');
50 * Export a chapter to a simple plaintext .txt file.
52 * @throws NotFoundException
54 public function plainText(string $bookSlug, string $chapterSlug)
56 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
57 $chapterText = $this->exportFormatter->chapterToPlainText($chapter);
59 return $this->download()->directly($chapterText, $chapterSlug . '.txt');
63 * Export a chapter to a simple markdown file.
65 * @throws NotFoundException
67 public function markdown(string $bookSlug, string $chapterSlug)
69 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
70 $chapterText = $this->exportFormatter->chapterToMarkdown($chapter);
72 return $this->download()->directly($chapterText, $chapterSlug . '.md');
76 * Export a book to a contained ZIP export file.
77 * @throws NotFoundException
79 public function zip(string $bookSlug, string $chapterSlug, ZipExportBuilder $builder)
81 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
82 $zip = $builder->buildForChapter($chapter);
84 return $this->download()->streamedDirectly(fopen($zip, 'r'), $chapterSlug . '.zip', filesize($zip));