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;
12 class BookExportController extends Controller
14 public function __construct(
15 protected BookQueries $queries,
16 protected ExportFormatter $exportFormatter,
18 $this->middleware('can:content-export');
19 $this->middleware('throttle:exports');
23 * Export a book as a PDF file.
27 public function pdf(string $bookSlug)
29 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
30 $pdfContent = $this->exportFormatter->bookToPdf($book);
32 return $this->download()->directly($pdfContent, $bookSlug . '.pdf');
36 * Export a book as a contained HTML file.
40 public function html(string $bookSlug)
42 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
43 $htmlContent = $this->exportFormatter->bookToContainedHtml($book);
45 return $this->download()->directly($htmlContent, $bookSlug . '.html');
49 * Export a book as a plain text file.
51 public function plainText(string $bookSlug)
53 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
54 $textContent = $this->exportFormatter->bookToPlainText($book);
56 return $this->download()->directly($textContent, $bookSlug . '.txt');
60 * Export a book as a markdown file.
62 public function markdown(string $bookSlug)
64 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
65 $textContent = $this->exportFormatter->bookToMarkdown($book);
67 return $this->download()->directly($textContent, $bookSlug . '.md');
71 * Export a book to a contained ZIP export file.
72 * @throws NotFoundException
74 public function zip(string $bookSlug, ZipExportBuilder $builder)
76 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
77 $zip = $builder->buildForBook($book);
79 return $this->download()->streamedFileDirectly($zip, $bookSlug . '.zip', filesize($zip), true);