3 namespace BookStack\Exports\Controllers;
5 use BookStack\Entities\Queries\BookQueries;
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 BookExportController extends Controller
15 public function __construct(
16 protected BookQueries $queries,
17 protected ExportFormatter $exportFormatter,
19 $this->middleware(Permission::ContentExport->middleware());
20 $this->middleware('throttle:exports');
24 * Export a book as a PDF file.
28 public function pdf(string $bookSlug)
30 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
31 $pdfContent = $this->exportFormatter->bookToPdf($book);
33 return $this->download()->directly($pdfContent, $bookSlug . '.pdf');
37 * Export a book as a contained HTML file.
41 public function html(string $bookSlug)
43 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
44 $htmlContent = $this->exportFormatter->bookToContainedHtml($book);
46 return $this->download()->directly($htmlContent, $bookSlug . '.html');
50 * Export a book as a plain text file.
52 public function plainText(string $bookSlug)
54 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
55 $textContent = $this->exportFormatter->bookToPlainText($book);
57 return $this->download()->directly($textContent, $bookSlug . '.txt');
61 * Export a book as a markdown file.
63 public function markdown(string $bookSlug)
65 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
66 $textContent = $this->exportFormatter->bookToMarkdown($book);
68 return $this->download()->directly($textContent, $bookSlug . '.md');
72 * Export a book to a contained ZIP export file.
73 * @throws NotFoundException
75 public function zip(string $bookSlug, ZipExportBuilder $builder)
77 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
78 $zip = $builder->buildForBook($book);
80 return $this->download()->streamedFileDirectly($zip, $bookSlug . '.zip', true);