3 namespace BookStack\Exports\Controllers;
5 use BookStack\Entities\Queries\BookQueries;
6 use BookStack\Exports\ExportFormatter;
7 use BookStack\Exports\ZipExports\ZipExportBuilder;
8 use BookStack\Http\ApiController;
11 class BookExportApiController extends ApiController
13 public function __construct(
14 protected ExportFormatter $exportFormatter,
15 protected BookQueries $queries,
17 $this->middleware('can:content-export');
21 * Export a book as a PDF file.
25 public function exportPdf(int $id)
27 $book = $this->queries->findVisibleByIdOrFail($id);
28 $pdfContent = $this->exportFormatter->bookToPdf($book);
30 return $this->download()->directly($pdfContent, $book->slug . '.pdf');
34 * Export a book as a contained HTML file.
38 public function exportHtml(int $id)
40 $book = $this->queries->findVisibleByIdOrFail($id);
41 $htmlContent = $this->exportFormatter->bookToContainedHtml($book);
43 return $this->download()->directly($htmlContent, $book->slug . '.html');
47 * Export a book as a plain text file.
49 public function exportPlainText(int $id)
51 $book = $this->queries->findVisibleByIdOrFail($id);
52 $textContent = $this->exportFormatter->bookToPlainText($book);
54 return $this->download()->directly($textContent, $book->slug . '.txt');
58 * Export a book as a markdown file.
60 public function exportMarkdown(int $id)
62 $book = $this->queries->findVisibleByIdOrFail($id);
63 $markdown = $this->exportFormatter->bookToMarkdown($book);
65 return $this->download()->directly($markdown, $book->slug . '.md');
70 * Export a book to a contained ZIP export file.
71 * @throws NotFoundException
73 public function exportZip(int $id, ZipExportBuilder $builder)
75 $book = $this->queries->findVisibleByIdOrFail($id);
76 $bookName= $book->getShortName();
78 $zip = $builder->buildForBook($book);
80 return $this->download()->streamedFileDirectly($zip, $bookName . '.zip', filesize($zip), true);