3 namespace BookStack\Exports\Controllers;
5 use BookStack\Entities\Queries\ChapterQueries;
6 use BookStack\Exports\ExportFormatter;
7 use BookStack\Exports\ZipExports\ZipExportBuilder;
8 use BookStack\Http\ApiController;
11 class ChapterExportApiController extends ApiController
13 public function __construct(
14 protected ExportFormatter $exportFormatter,
15 protected ChapterQueries $queries,
17 $this->middleware('can:content-export');
21 * Export a chapter as a PDF file.
25 public function exportPdf(int $id)
27 $chapter = $this->queries->findVisibleByIdOrFail($id);
28 $pdfContent = $this->exportFormatter->chapterToPdf($chapter);
30 return $this->download()->directly($pdfContent, $chapter->slug . '.pdf');
34 * Export a chapter as a contained HTML file.
38 public function exportHtml(int $id)
40 $chapter = $this->queries->findVisibleByIdOrFail($id);
41 $htmlContent = $this->exportFormatter->chapterToContainedHtml($chapter);
43 return $this->download()->directly($htmlContent, $chapter->slug . '.html');
47 * Export a chapter as a plain text file.
49 public function exportPlainText(int $id)
51 $chapter = $this->queries->findVisibleByIdOrFail($id);
52 $textContent = $this->exportFormatter->chapterToPlainText($chapter);
54 return $this->download()->directly($textContent, $chapter->slug . '.txt');
58 * Export a chapter as a markdown file.
60 public function exportMarkdown(int $id)
62 $chapter = $this->queries->findVisibleByIdOrFail($id);
63 $markdown = $this->exportFormatter->chapterToMarkdown($chapter);
65 return $this->download()->directly($markdown, $chapter->slug . '.md');
68 public function exportZip(int $id, ZipExportBuilder $builder)
70 $chapter = $this->queries->findVisibleByIdOrFail($id);
71 $chapterName= $chapter->getShortName();
72 $zip = $builder->buildForChapter($chapter);
74 return $this->download()->streamedFileDirectly($zip, $chapterName . '.zip', filesize($zip), true);