3 namespace BookStack\Http\Controllers;
5 use BookStack\Entities\Managers\BookContents;
6 use BookStack\Entities\ExportService;
7 use BookStack\Entities\Repos\BookRepo;
11 class BookExportController extends Controller
15 protected $exportService;
18 * BookExportController constructor.
20 public function __construct(BookRepo $bookRepo, ExportService $exportService)
22 $this->bookRepo = $bookRepo;
23 $this->exportService = $exportService;
24 parent::__construct();
28 * Export a book as a PDF file.
31 public function pdf(string $bookSlug)
33 $book = $this->bookRepo->getBySlug($bookSlug);
34 $pdfContent = $this->exportService->bookToPdf($book);
35 return $this->downloadResponse($pdfContent, $bookSlug . '.pdf');
39 * Export a book as a contained HTML file.
42 public function html(string $bookSlug)
44 $book = $this->bookRepo->getBySlug($bookSlug);
45 $htmlContent = $this->exportService->bookToContainedHtml($book);
46 return $this->downloadResponse($htmlContent, $bookSlug . '.html');
50 * Export a book as a plain text file.
52 public function plainText(string $bookSlug)
54 $book = $this->bookRepo->getBySlug($bookSlug);
55 $textContent = $this->exportService->bookToPlainText($book);
56 return $this->downloadResponse($textContent, $bookSlug . '.txt');
60 * Export a book as a markdown file.
62 public function markdown(string $bookSlug)
64 $book = $this->bookRepo->getBySlug($bookSlug);
65 $textContent = $this->exportService->bookToMarkdown($book);
66 return $this->downloadResponse($textContent, $bookSlug . '.md');
70 * Export a book as a zip file, made of markdown files.
72 public function zip(string $bookSlug)
74 $book = $this->bookRepo->getBySlug($bookSlug);
75 $z = new ZipArchive();
76 $z->open("book.zip", \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
77 $bookTree = (new BookContents($book))->getTree(false, true);
78 foreach ($bookTree as $bookChild) {
79 if ($bookChild->isA('chapter')) {
80 $z->addEmptyDir($bookChild->name);
81 foreach ($bookChild->pages as $page) {
82 $z->addFromString($bookChild->name . "/" . $page->name . ".md", $this->exportService->pageToMarkdown($page));
85 $z->addFromString($bookChild->name . ".md", $this->exportService->pageToMarkdown($bookChild));
88 return response()->download('book.zip');
89 // TODO: Is not unlinking it a security issue?