2019-09-15 22:33:27 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Http\Controllers;
|
|
|
|
|
2019-09-15 23:28:23 +01:00
|
|
|
use BookStack\Entities\Repos\BookRepo;
|
2021-06-26 15:23:15 +00:00
|
|
|
use BookStack\Entities\Tools\ExportFormatter;
|
2019-09-15 22:33:27 +01:00
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
class BookExportController extends Controller
|
|
|
|
{
|
2019-10-05 12:55:01 +01:00
|
|
|
protected $bookRepo;
|
2020-11-22 01:26:14 +00:00
|
|
|
protected $exportFormatter;
|
2019-09-15 22:33:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* BookExportController constructor.
|
|
|
|
*/
|
2020-11-22 01:26:14 +00:00
|
|
|
public function __construct(BookRepo $bookRepo, ExportFormatter $exportFormatter)
|
2019-09-15 22:33:27 +01:00
|
|
|
{
|
2019-09-15 23:28:23 +01:00
|
|
|
$this->bookRepo = $bookRepo;
|
2020-11-22 01:26:14 +00:00
|
|
|
$this->exportFormatter = $exportFormatter;
|
2021-08-28 21:48:17 +01:00
|
|
|
$this->middleware('can:content-export');
|
2019-09-15 22:33:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export a book as a PDF file.
|
2021-06-26 15:23:15 +00:00
|
|
|
*
|
2019-09-15 22:33:27 +01:00
|
|
|
* @throws Throwable
|
|
|
|
*/
|
|
|
|
public function pdf(string $bookSlug)
|
|
|
|
{
|
2019-09-15 23:28:23 +01:00
|
|
|
$book = $this->bookRepo->getBySlug($bookSlug);
|
2020-11-22 01:26:14 +00:00
|
|
|
$pdfContent = $this->exportFormatter->bookToPdf($book);
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2022-06-08 23:50:42 +01:00
|
|
|
return $this->download()->directly($pdfContent, $bookSlug . '.pdf');
|
2019-09-15 22:33:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export a book as a contained HTML file.
|
2021-06-26 15:23:15 +00:00
|
|
|
*
|
2019-09-15 22:33:27 +01:00
|
|
|
* @throws Throwable
|
|
|
|
*/
|
|
|
|
public function html(string $bookSlug)
|
|
|
|
{
|
2019-09-15 23:28:23 +01:00
|
|
|
$book = $this->bookRepo->getBySlug($bookSlug);
|
2020-11-22 01:26:14 +00:00
|
|
|
$htmlContent = $this->exportFormatter->bookToContainedHtml($book);
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2022-06-08 23:50:42 +01:00
|
|
|
return $this->download()->directly($htmlContent, $bookSlug . '.html');
|
2019-09-15 22:33:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export a book as a plain text file.
|
|
|
|
*/
|
|
|
|
public function plainText(string $bookSlug)
|
|
|
|
{
|
2019-09-15 23:28:23 +01:00
|
|
|
$book = $this->bookRepo->getBySlug($bookSlug);
|
2020-11-22 01:26:14 +00:00
|
|
|
$textContent = $this->exportFormatter->bookToPlainText($book);
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2022-06-08 23:50:42 +01:00
|
|
|
return $this->download()->directly($textContent, $bookSlug . '.txt');
|
2019-09-15 22:33:27 +01:00
|
|
|
}
|
2020-05-12 21:12:26 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Export a book as a markdown file.
|
|
|
|
*/
|
|
|
|
public function markdown(string $bookSlug)
|
|
|
|
{
|
|
|
|
$book = $this->bookRepo->getBySlug($bookSlug);
|
2021-06-22 21:02:18 +01:00
|
|
|
$textContent = $this->exportFormatter->bookToMarkdown($book);
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2022-06-08 23:50:42 +01:00
|
|
|
return $this->download()->directly($textContent, $bookSlug . '.md');
|
2020-05-12 21:12:26 -07:00
|
|
|
}
|
2019-09-15 22:33:27 +01:00
|
|
|
}
|