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