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');
22 * Export a book as a PDF file.
26 public function pdf(string $bookSlug)
28 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
29 $pdfContent = $this->exportFormatter->bookToPdf($book);
31 return $this->download()->directly($pdfContent, $bookSlug . '.pdf');
35 * Export a book as a contained HTML file.
39 public function html(string $bookSlug)
41 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
42 $htmlContent = $this->exportFormatter->bookToContainedHtml($book);
44 return $this->download()->directly($htmlContent, $bookSlug . '.html');
48 * Export a book as a plain text file.
50 public function plainText(string $bookSlug)
52 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
53 $textContent = $this->exportFormatter->bookToPlainText($book);
55 return $this->download()->directly($textContent, $bookSlug . '.txt');
59 * Export a book as a markdown file.
61 public function markdown(string $bookSlug)
63 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
64 $textContent = $this->exportFormatter->bookToMarkdown($book);
66 return $this->download()->directly($textContent, $bookSlug . '.md');
70 * Export a book to a contained ZIP export file.
71 * @throws NotFoundException
73 public function zip(string $bookSlug, ZipExportBuilder $builder)
75 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
76 $zip = $builder->buildForBook($book);
78 return $this->download()->streamedDirectly(fopen($zip, 'r'), $bookSlug . '.zip', filesize($zip));